184 lines
4.1 KiB
Go
184 lines
4.1 KiB
Go
package discord
|
|
|
|
import (
|
|
"database/sql"
|
|
"dorisbot/models"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// ChannelFacebookRT -
|
|
type ChannelFacebookRT struct {
|
|
Channel string `db:"channel"`
|
|
Facebook string `db:"facebook"`
|
|
Tmpl string `db:"tmpl"`
|
|
Ctime time.Time `db:"ctime"`
|
|
Mtime time.Time `db:"mtime"`
|
|
}
|
|
|
|
// ChannelInstagramRT -
|
|
type ChannelInstagramRT struct {
|
|
Channel string `db:"channel"`
|
|
Instagram string `db:"instagram"`
|
|
Tmpl string `db:"tmpl"`
|
|
Ctime time.Time `db:"ctime"`
|
|
Mtime time.Time `db:"mtime"`
|
|
}
|
|
|
|
// ChannelYoutubeRT -
|
|
type ChannelYoutubeRT struct {
|
|
Channel string `db:"channel"`
|
|
Youtube string `db:"youtube"`
|
|
Tmpl string `db:"tmpl"`
|
|
Ctime time.Time `db:"ctime"`
|
|
Mtime time.Time `db:"mtime"`
|
|
}
|
|
|
|
// ChannelTwitchRT -
|
|
type ChannelTwitchRT struct {
|
|
Channel string `db:"channel"`
|
|
Twitch string `db:"twitch"`
|
|
Tmpl string `db:"tmpl"`
|
|
Ctime time.Time `db:"ctime"`
|
|
Mtime time.Time `db:"mtime"`
|
|
}
|
|
|
|
// CreateFacebookRT -
|
|
func CreateFacebookRT(channel, facebook, tmpl string) (rt *ChannelFacebookRT, err error) {
|
|
if len(channel) == 0 || len(facebook) == 0 {
|
|
return nil, errors.New("channel or fbid is empty")
|
|
}
|
|
rt = &ChannelFacebookRT{
|
|
Channel: channel,
|
|
Facebook: facebook,
|
|
Tmpl: tmpl,
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetFacebookRT -
|
|
func GetFacebookRT(channel, facebook string) (rt *ChannelFacebookRT, err error) {
|
|
if len(channel) == 0 || len(facebook) == 0 {
|
|
return nil, errors.New("channel or fbid is empty")
|
|
}
|
|
rt = &ChannelFacebookRT{}
|
|
db := models.GetConn()
|
|
query := `select channel, facebook, tmpl, ctime, mtime where channel = $1 and facebook = $2`
|
|
err = db.Get(rt, query, channel, facebook)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// Write -
|
|
func (p *ChannelFacebookRT) Write() (err error) {
|
|
db := models.GetConn()
|
|
query := `insert into discord.channel_facebook_rt ("channel", "facebook", "tmpl") values (:channel, :facebook, :tmpl) returning *`
|
|
row, err := db.NamedQuery(query, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer row.Close()
|
|
if row.Next() {
|
|
err = row.StructScan(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// DeleteFacebookRT -
|
|
func DeleteFacebookRT(channel, facebook string) (err error) {
|
|
if len(channel) == 0 || len(facebook) == 0 {
|
|
return errors.New("channel or fbid is empty")
|
|
}
|
|
|
|
db := models.GetConn()
|
|
|
|
query := `delete from "discord"."channel_facebook_rt" where "channel" = $1 and "facebook" = $2`
|
|
_, err = db.Exec(query, channel, facebook)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|
|
|
|
// CreateInstagramRT -
|
|
func CreateInstagramRT(channel, instagram, tmpl string) (rt *ChannelInstagramRT, err error) {
|
|
if len(channel) == 0 || len(instagram) == 0 {
|
|
return nil, errors.New("channel or ig is empty")
|
|
}
|
|
|
|
rt = &ChannelInstagramRT{
|
|
Channel: channel,
|
|
Instagram: instagram,
|
|
Tmpl: tmpl,
|
|
}
|
|
return rt, nil
|
|
}
|
|
|
|
// Write -
|
|
func (p *ChannelInstagramRT) Write() (err error) {
|
|
db := models.GetConn()
|
|
query := `insert into "discord"."channel_instagram_rt"
|
|
("channel", "instagram", "tmpl") values (:channel, :instagram, :tmpl) returning *`
|
|
row, err := db.NamedQuery(query, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer row.Close()
|
|
if row.Next() {
|
|
err = row.StructScan(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetInstagramRT -
|
|
func GetInstagramRT(channel, instagram string) (rt *ChannelInstagramRT, err error) {
|
|
if len(channel) == 0 || len(instagram) == 0 {
|
|
return nil, errors.New("channel or ig is empty")
|
|
}
|
|
db := models.GetConn()
|
|
query := `
|
|
select
|
|
"channel", "instagram", "ctime", "mtime"
|
|
from "discord"."channel_instagram_rt"
|
|
where
|
|
"channel" = $1
|
|
and "instagram" = $2
|
|
`
|
|
rt = &ChannelInstagramRT{}
|
|
err = db.Get(rt, query, channel, instagram)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// DeleteInstagramRT -
|
|
func DeleteInstagramRT(channel, instagram string) (err error) {
|
|
if len(channel) == 0 || len(instagram) == 0 {
|
|
return errors.New("channel or ig is empty")
|
|
}
|
|
db := models.GetConn()
|
|
query := `
|
|
delete from "discord"."channel_instagram_rt"
|
|
where
|
|
"channel" = $1
|
|
and "instagram" = $2
|
|
`
|
|
_, err = db.Exec(query, channel, instagram)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|