dorisbot/models/discord/rt_table.go

184 lines
4.1 KiB
Go
Raw Normal View History

2019-07-13 09:44:31 +00:00
package discord
2019-07-14 07:04:14 +00:00
import (
2019-08-26 09:45:17 +00:00
"database/sql"
2019-07-14 07:04:14 +00:00
"dorisbot/models"
"errors"
"time"
)
2019-07-13 09:44:31 +00:00
// 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 -
2019-07-14 07:04:14 +00:00
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
}
2019-07-13 09:44:31 +00:00
return
}
2019-07-22 12:54:25 +00:00
// 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
}
2019-08-20 09:29:06 +00:00
// 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
}
2019-08-26 09:45:17 +00:00
// 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
}