2018-09-07 08:33:37 +00:00
|
|
|
package model
|
|
|
|
|
2018-09-16 15:39:15 +00:00
|
|
|
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"`
|
|
|
|
Groups []*TwitchGroup `db:"-"`
|
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-11 16:29:38 +00:00
|
|
|
err = x.Select(&channels, `select * from "public"."twitch_channel"`)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-16 15:39:15 +00:00
|
|
|
// GetTwitchChannelWithName -
|
|
|
|
func GetTwitchChannelWithName(name string) (ch *TwitchChannel, err error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, errors.New("name empty")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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) {
|
2018-09-16 15:39:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-16 15:39:15 +00:00
|
|
|
// 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-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
|
|
|
|
}
|