113 lines
2.1 KiB
Go
113 lines
2.1 KiB
Go
package public
|
|
|
|
import (
|
|
"database/sql"
|
|
dsmodel "dorisbot/database/models/discord"
|
|
pubmodel "dorisbot/database/models/public"
|
|
"errors"
|
|
)
|
|
|
|
// FacebookImpl -
|
|
type FacebookImpl struct{}
|
|
|
|
// Create new Facebook Page
|
|
func (p FacebookImpl) Create(fb *pubmodel.FacebookPage) (err error) {
|
|
query := `
|
|
insert into "public"."facebook_page" ("id") values ($1) returnint *
|
|
`
|
|
|
|
err = x.Get(fb, query, fb.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdatePost -
|
|
func (p FacebookImpl) UpdatePost(fb *pubmodel.FacebookPage, postID string) (err error) {
|
|
query := `
|
|
update "public"."facebook_page" set
|
|
"last_post" = $1,
|
|
"mtime" = now()
|
|
where
|
|
"id" = $2
|
|
`
|
|
|
|
err = x.Get(fb, query, postID, fb.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetByID -
|
|
func (p FacebookImpl) GetByID(id string) (fb pubmodel.FacebookPage, exists bool, err error) {
|
|
exists = true
|
|
fb = pubmodel.FacebookPage{}
|
|
query := `
|
|
select "id", "last_post", "ctime", "mtime"
|
|
from "public"."facebook_page"
|
|
where
|
|
"id" = $1
|
|
limit 1
|
|
`
|
|
|
|
err = x.Get(&fb, query, id)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
exists = false
|
|
err = nil
|
|
}
|
|
return fb, exists, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetAll -
|
|
func (p FacebookImpl) GetAll() (list []pubmodel.FacebookPage, err error) {
|
|
query := `
|
|
select "id", "last_post", "ctime", "mtime"
|
|
from "public"."facebook_page"
|
|
order by "ctime" asc
|
|
`
|
|
err = x.Select(&list, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// Delete -
|
|
func (p FacebookImpl) Delete(id string) (err error) {
|
|
query := `
|
|
delete from "public"."facebook_page"
|
|
where "id" = $1
|
|
`
|
|
|
|
_, err = x.Exec(query, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetChannels -
|
|
func (p FacebookImpl) GetChannels(id string) (list []dsmodel.Channel, err error) {
|
|
query := `
|
|
select
|
|
ch."id", ch."server", ch."enable_cmd", ch."enable_notify", ch."ctime", ch."mtime"
|
|
from "discord"."channel" ch
|
|
left join "discord"."channel_facebook_rt" rt
|
|
on rt.channel = ch.id
|
|
left join "public"."facebook_page" fb
|
|
on fb.id = rt.facebook
|
|
where
|
|
fb.id = $1
|
|
`
|
|
err = x.Select(&list, query, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|