mtfosbot/model/facebook_page.go

76 lines
1.7 KiB
Go
Raw Normal View History

2018-09-07 08:33:37 +00:00
package model
2018-09-24 11:57:12 +00:00
import (
"database/sql"
"time"
)
2018-09-07 08:33:37 +00:00
2018-09-12 04:03:44 +00:00
// FBGroup -
type FBGroup struct {
2018-09-10 15:12:18 +00:00
*LineGroup
Tmpl string `db:"tmpl"`
}
2018-09-07 08:33:37 +00:00
// FacebookPage - struct
type FacebookPage struct {
2018-09-12 04:03:44 +00:00
ID string `db:"id" cc:"id"`
LastPost string `db:"lastpost" cc:"lastpost"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
Groups []*FBGroup `db:"-"`
2018-09-07 08:33:37 +00:00
}
2018-09-10 10:13:27 +00:00
// GetAllFacebookPage -
func GetAllFacebookPage() (pages []*FacebookPage, err error) {
err = x.Select(&pages, `select * from "public"."facebook_page"`)
if err != nil {
return nil, err
}
return
}
2018-09-10 15:12:18 +00:00
2018-09-14 16:01:36 +00:00
// GetFacebookPage -
func GetFacebookPage(id string) (page *FacebookPage, err error) {
2018-09-20 17:14:08 +00:00
page = &FacebookPage{}
err = x.Get(page, `select * from "public"."facebook_page" where "id" = $1`, id)
2018-09-24 12:19:14 +00:00
if err == sql.ErrNoRows {
return nil, nil
2018-09-24 11:57:12 +00:00
}
2018-09-24 12:19:14 +00:00
return
2018-09-14 16:01:36 +00:00
}
// AddPage -
func (p *FacebookPage) AddPage() (err error) {
2018-09-15 15:09:41 +00:00
stmt, err := x.PrepareNamed(`insert into "public"."facebook_page" ("id", "lastpost") values (:id, :lastpost) returning *`)
2018-09-14 16:01:36 +00:00
if err != nil {
return err
}
2018-09-15 15:09:41 +00:00
err = stmt.Get(p, p)
2018-09-14 16:01:36 +00:00
return
}
2018-09-12 04:03:44 +00:00
// UpdatePost -
2018-09-10 15:12:18 +00:00
func (p *FacebookPage) UpdatePost(postID string) (err error) {
2018-10-15 08:12:11 +00:00
query := `update "public"."facebook_page" set "lastpost" = $1, "mtime" = now() where id = $2`
2018-09-10 15:12:18 +00:00
_, err = x.Exec(query, postID, p.ID)
if err != nil {
return
}
p.LastPost = postID
return
}
2018-09-12 04:03:44 +00:00
// GetGroups -
2018-09-10 15:12:18 +00:00
func (p *FacebookPage) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p
left join "public"."line_fb_rt" rt
on rt."facebook" = p.id
left join "public"."line_group" g
on g.id = rt."line"
where
2018-09-21 10:36:09 +00:00
p.id = $1
and rt.facebook is not null`
2018-09-10 15:12:18 +00:00
err = x.Select(&p.Groups, query, p.ID)
return
}