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
+9 -1
View File
@@ -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
+117
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
}