mtfosbot/model/twitch_channel.go

126 lines
3.2 KiB
Go
Raw Normal View History

2018-09-07 08:33:37 +00:00
package model
import (
"errors"
"time"
)
2018-09-07 08:33:37 +00:00
2018-09-12 04:03:44 +00:00
// TwitchGroup -
type TwitchGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
2018-09-07 08:33:37 +00:00
// TwitchChannel - struct
type TwitchChannel struct {
2018-09-12 04:03:44 +00:00
ID string `db:"id" cc:"id"`
Name string `db:"name" cc:"name"`
LastStream string `db:"laststream" cc:"laststream"`
Join bool `db:"join" cc:"join"`
OpayID string `db:"opayid" cc:"opayid"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
2018-09-17 16:35:48 +00:00
Groups []*TwitchGroup `db:"-" cc:"-"`
2018-09-07 08:33:37 +00:00
}
2018-09-11 16:29:38 +00:00
2018-09-12 04:03:44 +00:00
// GetAllTwitchChannel -
func GetAllTwitchChannel() (channels []*TwitchChannel, err error) {
2018-09-17 16:35:48 +00:00
err = x.Select(&channels, `select * from "public"."twitch_channel" order by "name" desc`)
2018-09-11 16:29:38 +00:00
return
}
2018-09-12 04:03:44 +00:00
2018-09-13 10:18:59 +00:00
// GetJoinChatChannel -
func GetJoinChatChannel() (channels []*TwitchChannel, err error) {
err = x.Select(&channels, `select * from "public"."twitch_channel" where "join" = true`)
return
}
// GetTwitchChannelWithName -
func GetTwitchChannelWithName(name string) (ch *TwitchChannel, err error) {
if len(name) == 0 {
return nil, errors.New("name empty")
}
2018-09-20 17:14:08 +00:00
ch = &TwitchChannel{}
err = x.Get(ch, `select * from "public"."twitch_channel" where "name" = $1`, name)
return
}
// GetTwitchChannelWithID -
func GetTwitchChannelWithID(id string) (ch *TwitchChannel, err error) {
if len(id) == 0 {
return nil, errors.New("id empty")
}
2018-09-20 17:14:08 +00:00
ch = &TwitchChannel{}
err = x.Get(ch, `select * from "public"."twitch_channel" where "id" = $1`, id)
return
}
2018-09-15 15:09:41 +00:00
// GetWithName -
func (p *TwitchChannel) GetWithName() (err error) {
stmt, err := x.PrepareNamed(`select * from "public"."twitch_channel" where "name" = :name`)
if err != nil {
return err
}
err = stmt.Get(p, p)
return
}
2018-09-14 16:01:36 +00:00
// Add -
func (p *TwitchChannel) Add() (err error) {
stmt, err := x.PrepareNamed(`insert into "public"."twitch_channel" ("id", "name", "laststream", "join", "opayid") values (:id, :name, :laststream, :join, :opayid) returning *`)
2018-09-14 16:01:36 +00:00
if err != nil {
return err
}
2018-09-15 15:09:41 +00:00
err = stmt.Get(p, p)
2018-09-14 16:01:36 +00:00
return
}
2018-09-12 04:03:44 +00:00
// UpdateStream -
func (p *TwitchChannel) UpdateStream(streamID string) (err error) {
query := `update "public"."twitch_channel" set "laststream" = $1 where "id" = $2`
_, err = x.Exec(query, streamID, p.ID)
if err != nil {
return
}
p.LastStream = streamID
return
}
// UpdateName -
func (p *TwitchChannel) UpdateName(name string) (err error) {
_, err = x.Exec(`update "public"."twitch_channel" set "name" = $1 where "id" = $2`, name, p.ID)
if err != nil {
return
}
p.Name = name
return
}
2018-09-17 16:35:48 +00:00
// UpdateJoin -
func (p *TwitchChannel) UpdateJoin(join bool) (err error) {
p.Join = join
_, err = x.NamedExec(`update "public"."twitch_channel" set "join" = :join where "id" = :id`, p)
return
}
// UpdateOpayID -
func (p *TwitchChannel) UpdateOpayID(id string) (err error) {
p.OpayID = id
_, err = x.NamedExec(`update "public"."twitch_channel" set "opayid" = :opayid where "id" = :id`, p)
return
}
2018-09-12 04:03:44 +00:00
// GetGroups -
func (p *TwitchChannel) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."twitch_channel" tw
left join "public"."line_twitch_rt" rt
on rt.twitch = tw.id
left join "public"."line_group" g
on g.id = rt.line
where
tw.id = $1`
err = x.Select(&p.Groups, query, p.ID)
return
}