dorisbot/database/impl/public/facebook.go

113 lines
2.1 KiB
Go
Raw Permalink Normal View History

2019-09-16 14:06:23 +00:00
package public
import (
2019-09-17 10:08:41 +00:00
"database/sql"
dsmodel "dorisbot/database/models/discord"
2019-09-16 14:06:23 +00:00
pubmodel "dorisbot/database/models/public"
2019-09-17 10:08:41 +00:00
"errors"
2019-09-16 14:06:23 +00:00
)
// FacebookImpl -
type FacebookImpl struct{}
// Create new Facebook Page
func (p FacebookImpl) Create(fb *pubmodel.FacebookPage) (err error) {
2019-09-17 10:08:41 +00:00
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
2019-09-29 14:46:01 +00:00
fb = pubmodel.FacebookPage{}
2019-09-17 10:08:41 +00:00
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 {
2019-09-29 14:46:01 +00:00
return nil, err
2019-09-17 10:08:41 +00:00
}
2019-09-16 14:06:23 +00:00
return
}