120 lines
2.7 KiB
Go
120 lines
2.7 KiB
Go
package public
|
|
|
|
import (
|
|
"database/sql"
|
|
dsmodel "dorisbot/database/models/discord"
|
|
pubmodel "dorisbot/database/models/public"
|
|
"errors"
|
|
|
|
sq "github.com/elgris/sqrl"
|
|
)
|
|
|
|
// InstagramImpl -
|
|
type InstagramImpl struct{}
|
|
|
|
// Create -
|
|
func (i InstagramImpl) Create(ig *pubmodel.Instagram) (err error) {
|
|
query, args, err := sq.Insert(`"public"."instagram"`).Columns("id").Values(ig.ID).Suffix("returning *").ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
err = x.Get(ig, query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// UpdatePost -
|
|
func (i InstagramImpl) UpdatePost(ig *pubmodel.Instagram, postID string) (err error) {
|
|
query, args, err := sq.Update(`"public"."instagram"`).Set("last_post", postID).Set("mtime", sq.Expr("now()")).Where(sq.Eq{"id": ig.ID}).ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
err = x.Get(ig, query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetByID -
|
|
func (i *InstagramImpl) GetByID(id string) (pubmodel.Instagram, bool, error) {
|
|
ig := pubmodel.Instagram{}
|
|
query, args, err := sq.Select("id", "last_post", "ctime", "mtime").From(`"public"."instagram"`).Where(sq.Eq{"id": id}).ToSql()
|
|
if err != nil {
|
|
return ig, false, err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
err = x.Get(&ig, query, args...)
|
|
if err != nil {
|
|
exists := true
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
exists = false
|
|
err = nil
|
|
}
|
|
return ig, exists, err
|
|
}
|
|
|
|
return ig, true, nil
|
|
}
|
|
|
|
// GetAll -
|
|
func (i *InstagramImpl) GetAll() ([]pubmodel.Instagram, error) {
|
|
query, args, err := sq.Select("id", "last_post", "ctime", "mtime").From(`"public"."instagram"`).OrderBy("ctime").ToSql()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
list := make([]pubmodel.Instagram, 0)
|
|
err = x.Select(&list, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return list, nil
|
|
}
|
|
|
|
// Delete -
|
|
func (i *InstagramImpl) Delete(id string) error {
|
|
query, args, err := sq.Delete(`"public"."instagram"`).Where(sq.Eq{"id": id}).ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
_, err = x.Exec(query, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetChannels -
|
|
func (i *InstagramImpl) GetChannels(id string) ([]dsmodel.Channel, error) {
|
|
query, args, err := sq.Select("ch.id", "ch.server", "ch.enable_cmd", "ch.enable_notify", "ch.ctime", "ch.mtime").
|
|
From(`"discord"."channel" ch`).
|
|
LeftJoin(`"discord"."channel_instagram_rt" rt on rt.channel = ch.id`).
|
|
LeftJoin(`"public"."instagram" ig on ig.id = rt.instagram`).
|
|
Where(sq.Eq{"ig.id": id}).OrderBy("ch.ctime").ToSql()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query = x.Rebind(query)
|
|
|
|
list := make([]dsmodel.Channel, 0)
|
|
err = x.Select(&list, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|