add ig model

This commit is contained in:
Jay 2019-08-26 09:45:17 +00:00
parent b6aba699c9
commit f4ceb9ac21
4 changed files with 236 additions and 1 deletions

View File

@ -7,6 +7,8 @@ import (
"time" "time"
) )
var nerr = errors.New
// ChannelModel - // ChannelModel -
type ChannelModel struct { type ChannelModel struct {
ID string `db:"id"` ID string `db:"id"`
@ -129,6 +131,9 @@ func (p *ChannelModel) GetFacebookPageTmpl(fbid string) (tmpl string, err error)
if err != nil { if err != nil {
return "", err return "", err
} }
if rt == nil {
return "", errors.New("rt not found")
}
tmpl = rt.Tmpl tmpl = rt.Tmpl
rt = nil rt = nil
return tmpl, nil return tmpl, nil
@ -138,3 +143,30 @@ func (p *ChannelModel) GetFacebookPageTmpl(fbid string) (tmpl string, err error)
func (p *ChannelModel) DeleteFacebookPage(fbid string) (err error) { func (p *ChannelModel) DeleteFacebookPage(fbid string) (err error) {
return DeleteFacebookRT(p.ID, fbid) return DeleteFacebookRT(p.ID, fbid)
} }
// GetInstagramByID -
func (p *ChannelModel) GetInstagramByID(id string) (ig *pubmodel.InstagramModel, err error) {
return pubmodel.GetInstagramByChannelAndID(id, p.ID)
}
// GetInstagramTmpl -
func (p *ChannelModel) GetInstagramTmpl(id string) (tmpl string, err error) {
if len(id) == 0 {
return "", nerr("id is empty")
}
rt, err := GetInstagramRT(p.ID, id)
if err != nil {
return "", err
}
if rt == nil {
return "", nerr("rt not found")
}
tmpl = rt.Tmpl
rt = nil
return tmpl, nil
}
// DeleteInstagram -
func (p *ChannelModel) DeleteInstagram(id string) (err error) {
return DeleteInstagramRT(p.ID, id)
}

View File

@ -1,6 +1,7 @@
package discord package discord
import ( import (
"database/sql"
"dorisbot/models" "dorisbot/models"
"errors" "errors"
"time" "time"
@ -103,3 +104,80 @@ func DeleteFacebookRT(channel, facebook string) (err error) {
} }
return return
} }
// CreateInstagramRT -
func CreateInstagramRT(channel, instagram, tmpl string) (rt *ChannelInstagramRT, err error) {
if len(channel) == 0 || len(instagram) == 0 {
return nil, errors.New("channel or ig is empty")
}
rt = &ChannelInstagramRT{
Channel: channel,
Instagram: instagram,
Tmpl: tmpl,
}
return rt, nil
}
// Write -
func (p *ChannelInstagramRT) Write() (err error) {
db := models.GetConn()
query := `insert into "discord"."channel_instagram_rt"
("channel", "instagram", "tmpl") values (:channel, :instagram, :tmpl) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
defer row.Close()
if row.Next() {
err = row.StructScan(p)
if err != nil {
return err
}
}
return
}
// GetInstagramRT -
func GetInstagramRT(channel, instagram string) (rt *ChannelInstagramRT, err error) {
if len(channel) == 0 || len(instagram) == 0 {
return nil, errors.New("channel or ig is empty")
}
db := models.GetConn()
query := `
select
"channel", "instagram", "ctime", "mtime"
from "discord"."channel_instagram_rt"
where
"channel" = $1
and "instagram" = $2
`
rt = &ChannelInstagramRT{}
err = db.Get(rt, query, channel, instagram)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return
}
// DeleteInstagramRT -
func DeleteInstagramRT(channel, instagram string) (err error) {
if len(channel) == 0 || len(instagram) == 0 {
return errors.New("channel or ig is empty")
}
db := models.GetConn()
query := `
delete from "discord"."channel_instagram_rt"
where
"channel" = $1
and "instagram" = $2
`
_, err = db.Exec(query, channel, instagram)
if err != nil {
return err
}
return
}

View File

@ -50,7 +50,15 @@ func GetFacebookPageListByChannel(channel string) (pages []*FacebookPageModel, e
return nil, errors.New("channel is empty") return nil, errors.New("channel is empty")
} }
db := models.GetConn() db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page where channel = $1` 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
left join "discord"."channel" ch
on ch.id = rt.channel
where ch.channel = $1`
err = db.Select(&pages, query, channel) err = db.Select(&pages, query, channel)
if err != nil { if err != nil {
return nil, err return nil, err

117
models/public/instagram.go Normal file
View File

@ -0,0 +1,117 @@
package public
import (
"database/sql"
"dorisbot/models"
"errors"
"time"
)
// InstagramModel -
type InstagramModel struct {
ID string `db:"id"`
LastPost string `db:"lastpost"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewInstagram -
func NewInstagram(id string) (ig *InstagramModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
ig = &InstagramModel{ID: id}
return ig, nil
}
// Write -
func (p *InstagramModel) Write() (err error) {
db := models.GetConn()
query := `
insert into "public"."instagram" ("id", "lastpost") values (:id, :lastpost) returning *
`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
defer row.Close()
if row.Next() {
err = row.StructScan(p)
if err != nil {
return err
}
}
return
}
// UpdatePost -
func (p *InstagramModel) UpdatePost(postid string) (err error) {
db := models.GetConn()
query := `update "public"."instagram" set
"lastpost" = $1,
"mtime" = now()
where "id" = $2`
_, err = db.Exec(query, postid, p.ID)
if err != nil {
return err
}
p.LastPost = postid
return
}
// GetInstagramByID -
func GetInstagramByID(id string) (ig *InstagramModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
ig = &InstagramModel{}
db := models.GetConn()
query := `
select
"id", "lastpost", "ctime", "mtime"
from "public"."instagram"
where
"id" = $1
`
err = db.Get(ig, query, id)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return
}
// GetInstagramByChannelAndID -
func GetInstagramByChannelAndID(id, channel string) (ig *InstagramModel, err error) {
if len(channel) == 0 || len(id) == 0 {
return nil, errors.New("channel or ig is empty")
}
ig = &InstagramModel{}
db := models.GetConn()
query := `
select
ig.id, ig.lastpost, ig.ctime, ig.mtime
from "public"."instagram" ig
left join "discord"."channel_instagram_rt" rt
on rt.instagram = ig.id
left join "discord"."channel" ch
on ch.id = rt.channel
where
ch.id = $1
and ig.id = $2
`
err = db.Get(ig, query, channel, ig)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return
}