fix query error

This commit is contained in:
Jay 2019-08-25 19:27:00 +08:00
parent 4acc78cd41
commit 703ffe34b2
4 changed files with 56 additions and 6 deletions

View File

@ -38,7 +38,7 @@ func GetChannelByID(id string) (ch *ChannelModel, err error) {
}
ch = &ChannelModel{}
db := models.GetConn()
query := `select id, server, enable_cmd, enable_notify, ctime, mtime where id = $1`
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where id = $1`
err = db.Get(ch, query, id)
if err != nil {
return nil, err
@ -52,7 +52,7 @@ func GetChannelListByServer(server string) (list []*ChannelModel, err error) {
return nil, errors.New("server is empty")
}
db := models.GetConn()
query := `select id, server, enable_cmd, enable_notify, ctime, mtime where server = $1`
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where server = $1`
err = db.Select(&list, query, server)
if err != nil {
return nil, err

View File

@ -61,12 +61,26 @@ func GetFacebookPageByChannelAndID(id, channel string) (page *FacebookPageModel,
return nil, errors.New("channel or id is empty")
}
db := models.GetConn()
page = &FacebookPageModel{}
query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1 and channel = $2`
err = db.Get(page, query, channel)
if err != nil && err != sql.ErrNoRows {
query := `
select
fb.id, fb.lastpost, fb.ctime, fb.mtime
from public.facebook_page fb
left join "discord"."channel_facebook_rt" rt
on rt.facebook = fb.id
where fb.id = $1 and rt.channel = $2`
result, err := db.Queryx(query, id, channel)
if err != nil {
return nil, err
}
if result.Next() {
page = &FacebookPageModel{}
err = result.StructScan(page)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
}
return
}

View File

@ -39,10 +39,34 @@ func addServer(info EvtInfo, name string) string {
return "add server success"
}
func addChannel(info EvtInfo, cmd, notify string) string {
server, err := dsmodel.GetServerByID(info.Server)
if err != nil {
return "Get server data fail"
}
_ = server
if server == nil {
return "Server not init"
}
ch, err := dsmodel.NewChannel(info.Channel, info.Server, cmd == "1", notify == "1")
if err != nil {
return "init channel data fail"
}
err = ch.Write()
if err != nil {
return "write channel data fail"
}
return "Success"
}
func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
log.Printf("add facebook ::: id : %s ::: tmpl : %s \n", fbid, tmpl)
ch, err := dsmodel.GetChannelByID(info.Channel)
if err != nil {
log.Println("get channel fail", err)
return ""
}
@ -64,14 +88,17 @@ func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
} else {
fbrt, err := ch.GetFacebookPageByID(fbid)
if err != nil {
log.Println("get fb rt fail :: ", err)
return "check data fail"
}
if fbrt != nil {
return "facebook page already linked"
}
rt, err := dsmodel.CreateFacebookRT(info.Channel, page.ID, tmpl)
if err != nil {
log.Println("create fail :: ", err)
return "create data fail"
}

View File

@ -153,8 +153,17 @@ func selectAction(info EvtInfo, cmdKey, cmdValue, payload string) string {
switch cmdKey {
case "ds_addserver":
return addServer(info, payload)
case "ds_addchannel":
strs := strings.Split(payload, " ")
if len(strs) != 2 {
return "!ds_addchennel {enable_cmd} {enable_notify}"
}
return addChannel(info, strs[0], strs[1])
case "ds_addfacebook":
strs := strings.Split(payload, " ")
if len(strs) < 2 {
return "!ds_addfacebook {facebook_id} {template}"
}
return addFacebookPage(info, strs[0], strings.Join(strs[1:], " "))
case "ds_delfacebook":
return delFacebookPage(info, payload)