Encapsulate SQL statements and refactor Save functions as transactionable.

This commit is contained in:
Jeremy Latt 2013-05-24 21:39:53 -07:00
parent f24bb5ee7d
commit 48ca57c43d
11 changed files with 347 additions and 12 deletions

View file

@ -9,6 +9,7 @@ const (
)
type Channel struct {
id *RowId
server *Server
commands chan<- ChannelCommand
replies chan<- Reply
@ -30,6 +31,16 @@ func (set ChannelSet) Remove(channel *Channel) {
delete(set, channel)
}
func (set ChannelSet) Ids() (ids []RowId) {
ids = []RowId{}
for channel := range set {
if channel.id != nil {
ids = append(ids, *channel.id)
}
}
return ids
}
type ChannelCommand interface {
Command
HandleChannel(channel *Channel)
@ -52,6 +63,24 @@ func NewChannel(s *Server, name string) *Channel {
return channel
}
func (channel *Channel) Save(q Queryable) bool {
if channel.id == nil {
if err := InsertChannel(q, channel); err != nil {
return false
}
channelId, err := FindChannelIdByName(q, channel.name)
if err != nil {
return false
}
channel.id = &channelId
} else {
if err := UpdateChannel(q, channel); err != nil {
return false
}
}
return true
}
// Forward `Reply`s to all `User`s of the `Channel`.
func (channel *Channel) receiveReplies(replies <-chan Reply) {
for reply := range replies {