diff --git a/models/discord/channel.go b/models/discord/channel.go index 6ce6765..510a3be 100644 --- a/models/discord/channel.go +++ b/models/discord/channel.go @@ -7,6 +7,8 @@ import ( "time" ) +var nerr = errors.New + // ChannelModel - type ChannelModel struct { ID string `db:"id"` @@ -129,6 +131,9 @@ func (p *ChannelModel) GetFacebookPageTmpl(fbid string) (tmpl string, err error) if err != nil { return "", err } + if rt == nil { + return "", errors.New("rt not found") + } tmpl = rt.Tmpl rt = 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) { 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) +} diff --git a/models/discord/rt_table.go b/models/discord/rt_table.go index d78a6cb..881565e 100644 --- a/models/discord/rt_table.go +++ b/models/discord/rt_table.go @@ -1,6 +1,7 @@ package discord import ( + "database/sql" "dorisbot/models" "errors" "time" @@ -103,3 +104,80 @@ func DeleteFacebookRT(channel, facebook string) (err error) { } 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 +} diff --git a/models/public/facebook.go b/models/public/facebook.go index bce936d..1046ca5 100644 --- a/models/public/facebook.go +++ b/models/public/facebook.go @@ -50,7 +50,15 @@ func GetFacebookPageListByChannel(channel string) (pages []*FacebookPageModel, e return nil, errors.New("channel is empty") } 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) if err != nil { return nil, err diff --git a/models/public/instagram.go b/models/public/instagram.go new file mode 100644 index 0000000..28c46de --- /dev/null +++ b/models/public/instagram.go @@ -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 +}