This commit is contained in:
Jay 2019-07-13 17:44:31 +08:00
parent e8b918f3f0
commit c74b8f8e4d
7 changed files with 258 additions and 39 deletions

66
models/discord/channel.go Normal file
View File

@ -0,0 +1,66 @@
package discord
import (
"dorisbot/models"
"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
}
// 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
}

View File

@ -8,8 +8,8 @@ import (
"time" "time"
) )
// DiscordServerModel - // ServerModel -
type DiscordServerModel struct { type ServerModel struct {
ID string `db:"id"` ID string `db:"id"`
Name string `db:"name"` Name string `db:"name"`
Permission int `db:"permission"` Permission int `db:"permission"`
@ -18,18 +18,8 @@ type DiscordServerModel struct {
Mtime time.Time `db:"mtime"` Mtime time.Time `db:"mtime"`
} }
// DiscordChannelModel - // ManageRoleModel -
type DiscordChannelModel struct { type ManageRoleModel 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"`
}
// DiscordManageRoleModel -
type DiscordManageRoleModel struct {
Server string `db:"server"` Server string `db:"server"`
RoleID string `db:"role_id"` RoleID string `db:"role_id"`
RoleName string `db:"role_name"` RoleName string `db:"role_name"`
@ -37,12 +27,12 @@ type DiscordManageRoleModel struct {
Mtime time.Time `db:"mtime"` Mtime time.Time `db:"mtime"`
} }
// NewDiscordServer - // NewServer -
func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel, err error) { func NewServer(id, name string, permission int) (ds *ServerModel, err error) {
if len(id) == 0 || len(name) == 0 { if len(id) == 0 || len(name) == 0 {
return nil, errors.New("id or name empty") return nil, errors.New("id or name empty")
} }
ds = &DiscordServerModel{ ds = &ServerModel{
ID: id, ID: id,
Name: name, Name: name,
Permission: permission, Permission: permission,
@ -50,8 +40,8 @@ func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel,
return return
} }
// GetDiscordServerList - // GetServerList -
func GetDiscordServerList() (list []*DiscordServerModel, err error) { func GetServerList() (list []*ServerModel, err error) {
db := models.GetConn() db := models.GetConn()
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server` query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server`
err = db.Select(&list, query) err = db.Select(&list, query)
@ -61,13 +51,13 @@ func GetDiscordServerList() (list []*DiscordServerModel, err error) {
return return
} }
// GetDiscordServerByID - // GetServerByID -
func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) { func GetServerByID(id string) (ds *ServerModel, err error) {
if len(id) == 0 { if len(id) == 0 {
return nil, errors.New("id is empty") return nil, errors.New("id is empty")
} }
db := models.GetConn() db := models.GetConn()
ds = &DiscordServerModel{} ds = &ServerModel{}
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server where "id" = $1` query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server where "id" = $1`
err = db.Get(ds, query, id) err = db.Get(ds, query, id)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -76,24 +66,27 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
return return
} }
// Write DiscordServer data to database // Write Server data to database
func (p *DiscordServerModel) Write() error { func (p *ServerModel) Write() error {
db := models.GetConn() db := models.GetConn()
query := `insert into discord.server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *` query := `insert into discord.server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *`
row, err := db.NamedQuery(query, p) row, err := db.NamedQuery(query, p)
if err != nil { if err != nil {
return err return err
} }
row.Next() defer row.Close()
if row.Next() {
err = row.StructScan(p) err = row.StructScan(p)
if err != nil { if err != nil {
return err return err
} }
}
return nil return nil
} }
// ChangeName - change discord server name // ChangeName - change discord server name
func (p *DiscordServerModel) ChangeName(name string) (err error) { func (p *ServerModel) ChangeName(name string) (err error) {
if len(name) == 0 { if len(name) == 0 {
return errors.New("name is empty") return errors.New("name is empty")
} }
@ -108,7 +101,7 @@ func (p *DiscordServerModel) ChangeName(name string) (err error) {
} }
// GetChannelList - // GetChannelList -
func (p *DiscordServerModel) GetChannelList() (list []*DiscordChannelModel, err error) { func (p *ServerModel) GetChannelList() (list []*ChannelModel, err error) {
db := models.GetConn() db := models.GetConn()
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "server" = $1` query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "server" = $1`
err = db.Select(&list, query, p.ID) err = db.Select(&list, query, p.ID)
@ -119,19 +112,19 @@ func (p *DiscordServerModel) GetChannelList() (list []*DiscordChannelModel, err
} }
// GetChannel - // GetChannel -
func (p *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err error) { func (p *ServerModel) GetChannel(id string) (ch *ChannelModel, err error) {
if len(id) == 0 { if len(id) == 0 {
return nil, errors.New("id is empty") return nil, errors.New("id is empty")
} }
db := models.GetConn() db := models.GetConn()
ch = &DiscordChannelModel{} ch = &ChannelModel{}
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "id" = $1 and "server" = $2` query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "id" = $1 and "server" = $2`
err = db.Get(ch, query, id, p.ID) err = db.Get(ch, query, id, p.ID)
return return
} }
// GetCommandList - // GetCommandList -
func (p *DiscordServerModel) GetCommandList() (cmds []*pubmodel.CommandModel, err error) { func (p *ServerModel) GetCommandList() (cmds []*pubmodel.CommandModel, err error) {
db := models.GetConn() db := models.GetConn()
query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from public."command" where "server" = $1` query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from public."command" where "server" = $1`
err = db.Select(&cmds, query, p.ID) err = db.Select(&cmds, query, p.ID)
@ -142,7 +135,7 @@ func (p *DiscordServerModel) GetCommandList() (cmds []*pubmodel.CommandModel, er
} }
// GetRoleList - // GetRoleList -
func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err error) { func (p *ServerModel) GetRoleList() (list []*ManageRoleModel, err error) {
db := models.GetConn() db := models.GetConn()
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1` query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1`
err = db.Select(&list, query, p.ID) err = db.Select(&list, query, p.ID)
@ -153,12 +146,12 @@ func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err
} }
// GetRole - // GetRole -
func (p *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleModel, err error) { func (p *ServerModel) GetRole(roleID string) (role *ManageRoleModel, err error) {
if len(roleID) == 0 { if len(roleID) == 0 {
return nil, errors.New("id is empty") return nil, errors.New("id is empty")
} }
db := models.GetConn() db := models.GetConn()
role = &DiscordManageRoleModel{} role = &ManageRoleModel{}
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1 and "rold_id" = $2` query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1 and "rold_id" = $2`
err = db.Get(role, query, p.ID, roleID) err = db.Get(role, query, p.ID, roleID)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
@ -168,7 +161,7 @@ func (p *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleMode
} }
// AddChannel - // AddChannel -
func (p *DiscordServerModel) AddChannel(id string, enableCMD, enableNotify bool) (err error) { func (p *ServerModel) AddChannel(id string, enableCMD, enableNotify bool) (err error) {
db := models.GetConn() db := models.GetConn()
query := `insert into discord.channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values query := `insert into discord.channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())` ($1, $2, $3, $4, now(), now())`

View File

@ -0,0 +1,44 @@
package discord
import "time"
// ChannelFacebookRT -
type ChannelFacebookRT struct {
Channel string `db:"channel"`
Facebook string `db:"facebook"`
Tmpl string `db:"tmpl"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// ChannelInstagramRT -
type ChannelInstagramRT struct {
Channel string `db:"channel"`
Instagram string `db:"instagram"`
Tmpl string `db:"tmpl"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// ChannelYoutubeRT -
type ChannelYoutubeRT struct {
Channel string `db:"channel"`
Youtube string `db:"youtube"`
Tmpl string `db:"tmpl"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// ChannelTwitchRT -
type ChannelTwitchRT struct {
Channel string `db:"channel"`
Twitch string `db:"twitch"`
Tmpl string `db:"tmpl"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// CreateFacebookRT -
func CreateFacebookRT(channel, facebook string) (rt *ChannelFacebookRT, err error) {
return
}

View File

@ -45,4 +45,9 @@ func Ping() error {
} }
// GetConn database connection // GetConn database connection
func GetConn() *sqlx.DB { return x } func GetConn() *sqlx.DB {
if x == nil {
panic(errors.New("database object not init"))
}
return x
}

View File

@ -122,7 +122,14 @@ func (p *CommandModel) Write() (err error) {
if err != nil { if err != nil {
return err return err
} }
defer row.Close()
if row.Next() {
err = row.StructScan(p) err = row.StructScan(p)
if err != nil {
return err
}
}
return return
} }

99
models/public/facebook.go Normal file
View File

@ -0,0 +1,99 @@
package public
import (
"database/sql"
"dorisbot/models"
"errors"
"time"
)
// FacebookPageModel -
type FacebookPageModel struct {
ID string `db:"id"`
LastPost string `db:"lastpost"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewFacebookPage -
func NewFacebookPage(id string) (page *FacebookPageModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
page = &FacebookPageModel{
ID: id,
}
return
}
// GetFacebookPageByID -
func GetFacebookPageByID(id string) (page *FacebookPageModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
page = &FacebookPageModel{}
db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1`
err = db.Get(page, query, id)
if err != nil {
return
}
return
}
// GetAllFacebookPage -
func GetAllFacebookPage() (pages []*FacebookPageModel, err error) {
db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page order by ctime`
err = db.Select(&pages, query)
if err != nil && err == sql.ErrNoRows {
return nil, nil
}
return
}
// GetFacebookPageRange -
func GetFacebookPageRange(limit, offset int) (pages []*FacebookPageModel, err error) {
if limit < 1 || offset < 0 {
return nil, errors.New("input arg format error")
}
db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page order by ctime limit $1 offset $2`
err = db.Select(&pages, query, limit, offset)
if err != nil && err == sql.ErrNoRows {
return nil, nil
}
return
}
// Write -
func (p *FacebookPageModel) Write() (err error) {
db := models.GetConn()
query := `insert into public.facebook_page (id) values (:id) 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
}
// UpdatePost -
func (p *FacebookPageModel) UpdatePost(post string) (err error) {
db := models.GetConn()
query := `update public.facebook_page set lastpost = $1, mtime = now() where id = $2`
_, err = db.Exec(query, post, p.ID)
if err != nil {
return err
}
p.LastPost = post
return
}

View File

@ -37,3 +37,8 @@ func addServer(info EvtInfo, name string) string {
return "add server success" return "add server success"
} }
func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
return ""
}