update discord model

This commit is contained in:
Jay 2019-06-28 15:11:58 +00:00
parent 49a0876051
commit 1e2026e21a
3 changed files with 117 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/config.yml

113
models/discord/discord.go Normal file
View File

@ -0,0 +1,113 @@
package discord
import (
"dorisbot/models"
"errors"
"time"
)
// DiscordServerModel -
type DiscordServerModel struct {
ID string `db:"id"`
Name string `db:"name"`
Permission int `db:"permission"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// DiscordChannelModel -
type DiscordChannelModel 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"`
}
// NewDiscordServer -
func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel, err error) {
if len(id) == 0 || len(name) == 0 {
return nil, errors.New("id or name empty")
}
ds = &DiscordServerModel{
ID: id,
Name: name,
Permission: permission,
}
return
}
// GetDiscordServerByID -
func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
db := models.GetConn()
ds = &DiscordServerModel{}
query := `select "id", "name", "permission", "ctime", "mtime" from discord_server where "id" = $1`
err = db.Get(ds, query, id)
return
}
// Write DiscordServer data to database
func (p *DiscordServerModel) Write() error {
db := models.GetConn()
query := `insert into discord_server ("id", "name", "permission") values (:id, :name, :permission) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
err = row.StructScan(p)
if err != nil {
return err
}
return nil
}
// ChangeName - change discord server name
func (p *DiscordServerModel) ChangeName(name string) (err error) {
if len(name) == 0 {
return errors.New("name is empty")
}
db := models.GetConn()
query := `update discord_server set "name" = $1, "mtime" = now() where "id" = $2`
_, err = db.Exec(query, name, p.ID)
if err != nil {
return err
}
p.Name = name
return
}
// GetChannelList -
func (p *DiscordServerModel) GetChannelList() (list []*DiscordChannelModel, err error) {
db := models.GetConn()
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord_channel where "server" = $1`
err = db.Select(&list, query, p.ID)
if err != nil {
return nil, err
}
return
}
// GetChannel -
func (p *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
// TODO: not finish
return
}
// AddChannel -
func (p *DiscordServerModel) AddChannel(id string, enableCMD, enableNotify bool) (err error) {
db := models.GetConn()
query := `insert into discord_channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())`
_, err = db.Exec(query, id, p.ID, enableCMD, enableNotify)
if err != nil {
return err
}
return
}

View File

@ -45,3 +45,6 @@ func Ping() error {
} }
return x.Ping() return x.Ping()
} }
// GetConn database connection
func GetConn() *sqlx.DB { return x }