dorisbot/models/discord/channel.go

122 lines
2.9 KiB
Go
Raw Normal View History

2019-07-13 09:44:31 +00:00
package discord
import (
"dorisbot/models"
2019-07-14 07:04:14 +00:00
pubmodel "dorisbot/models/public"
2019-07-13 09:44:31 +00:00
"errors"
"time"
)
// ChannelModel -
type ChannelModel struct {
ID string `db:"id"`
Server string `db:"server"`
EnableCMD bool `db:"enable_cmd"`
EnableNotify bool `db:"enable_notify"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewChannel -
func NewChannel(id, server string, enableCMD, enableNotify bool) (ch *ChannelModel, err error) {
if len(id) == 0 || len(server) == 0 {
return nil, errors.New("id or server is empty")
}
ch = &ChannelModel{
ID: id,
Server: server,
EnableCMD: enableCMD,
EnableNotify: enableNotify,
}
return
}
2019-07-14 07:04:14 +00:00
// GetChannelByID -
func GetChannelByID(id string) (ch *ChannelModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
ch = &ChannelModel{}
db := models.GetConn()
query := `select id, server, enable_cmd, enable_notify, ctime, mtime where id = $1`
err = db.Get(ch, query, id)
if err != nil {
return nil, err
}
return
}
// GetChannelListByServer -
func GetChannelListByServer(server string) (list []*ChannelModel, err error) {
if len(server) == 0 {
return nil, errors.New("server is empty")
}
db := models.GetConn()
query := `select id, server, enable_cmd, enable_notify, ctime, mtime where server = $1`
err = db.Select(&list, query, server)
if err != nil {
return nil, err
}
return
}
2019-07-13 09:44:31 +00:00
// Write -
func (p *ChannelModel) Write() (err error) {
db := models.GetConn()
query := `insert into "discord"."channel" (id, server, enable_cmd, enable_notify) values
(:id, :server, :enable_cmd, :enable_notify) returning *`
rows, err := db.NamedQuery(query, p)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
err = rows.StructScan(p)
if err != nil {
return err
}
}
return
}
// SetCMDState -
func (p *ChannelModel) SetCMDState(state bool) (err error) {
if state == p.EnableCMD {
return nil
}
db := models.GetConn()
query := `update "discord"."channel" set enable_cmd = $1, mtime = now() where id = $2`
_, err = db.Exec(query, state, p.ID)
if err != nil {
return err
}
p.EnableCMD = state
return
}
2019-07-14 07:04:14 +00:00
// SetNotifyState -
func (p *ChannelModel) SetNotifyState(state bool) (err error) {
if state == p.EnableNotify {
return nil
}
db := models.GetConn()
query := `update "discord"."channel" set enable_notify = $1, mtime = now() where id = $2`
_, err = db.Exec(query, state, p.ID)
if err != nil {
return err
}
p.EnableNotify = state
return
}
// GetFacebookPageList -
2019-07-21 01:41:06 +00:00
func (p *ChannelModel) GetFacebookPageList() (pages []*pubmodel.FacebookPageModel, err error) {
return pubmodel.GetFacebookPageListByChannel(p.ID)
}
// GetFacebookPageByID -
func (p *ChannelModel) GetFacebookPageByID(id string) (page *pubmodel.FacebookPageModel, err error) {
return pubmodel.GetFacebookPageByChannelAndID(id, p.ID)
2019-07-14 07:04:14 +00:00
}