mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-21 10:31:59 -08:00
Encapsulate SQL statements and refactor Save functions as transactionable.
This commit is contained in:
parent
f24bb5ee7d
commit
48ca57c43d
11 changed files with 347 additions and 12 deletions
|
|
@ -16,6 +16,7 @@ type UserCommand interface {
|
|||
}
|
||||
|
||||
type User struct {
|
||||
id *RowId
|
||||
nick string
|
||||
hash []byte
|
||||
server *Server
|
||||
|
|
@ -56,12 +57,46 @@ func NewUser(nick string, password string, server *Server) *User {
|
|||
replies: replies,
|
||||
}
|
||||
user.SetPassword(password)
|
||||
|
||||
go user.receiveCommands(commands)
|
||||
go user.receiveReplies(replies)
|
||||
server.users[nick] = user
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func (user *User) Save(q Queryable) bool {
|
||||
if user.id == nil {
|
||||
if err := InsertUser(q, user); err != nil {
|
||||
return false
|
||||
}
|
||||
userId, err := FindUserIdByNick(q, user.nick)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
user.id = &userId
|
||||
} else {
|
||||
if err := UpdateUser(q, user); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
channelIds := user.channels.Ids()
|
||||
if len(channelIds) == 0 {
|
||||
if err := DeleteAllUserChannels(q, *(user.id)); err != nil {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if err := DeleteOtherUserChannels(q, *(user.id), channelIds); err != nil {
|
||||
return false
|
||||
}
|
||||
if err := InsertUserChannels(q, *(user.id), channelIds); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (user *User) SetPassword(password string) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue