dorisbot/models/public/facebook.go

146 lines
3.3 KiB
Go
Raw Normal View History

2019-07-13 09:44:31 +00:00
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)
2019-07-22 12:54:25 +00:00
if err != nil && err != sql.ErrNoRows {
2019-07-13 09:44:31 +00:00
return
}
2019-08-26 04:04:46 +00:00
if err == sql.ErrNoRows {
page = nil
}
2019-07-22 12:54:25 +00:00
return page, nil
2019-07-13 09:44:31 +00:00
}
2019-07-21 01:41:06 +00:00
// GetFacebookPageListByChannel -
func GetFacebookPageListByChannel(channel string) (pages []*FacebookPageModel, err error) {
if len(channel) == 0 {
return nil, errors.New("channel is empty")
}
db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page where channel = $1`
err = db.Select(&pages, query, channel)
if err != nil {
return nil, err
}
return
}
// GetFacebookPageByChannelAndID -
func GetFacebookPageByChannelAndID(id, channel string) (page *FacebookPageModel, err error) {
if len(channel) == 0 || len(id) == 0 {
return nil, errors.New("channel or id is empty")
}
db := models.GetConn()
2019-08-25 11:27:00 +00:00
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 {
2019-07-21 01:41:06 +00:00
return nil, err
}
2019-08-25 11:27:00 +00:00
if result.Next() {
page = &FacebookPageModel{}
err = result.StructScan(page)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
}
2019-07-21 01:41:06 +00:00
return
}
2019-07-13 09:44:31 +00:00
// 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
}