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