mtfosbot/model/twitch_channel.go
jay e8db6fdab5 update
1. add youtube command
2. add google webhook
3. add twitch oauth route
4. add api checkSession middleware
2018-09-16 23:39:15 +08:00

110 lines
2.8 KiB
Go

package model
import (
"errors"
"time"
)
// TwitchGroup -
type TwitchGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
// TwitchChannel - struct
type TwitchChannel struct {
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:"-"`
}
// GetAllTwitchChannel -
func GetAllTwitchChannel() (channels []*TwitchChannel, err error) {
err = x.Select(&channels, `select * from "public"."twitch_channel"`)
return
}
// 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")
}
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
}
// 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
}
// 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 *`)
if err != nil {
return err
}
err = stmt.Get(p, p)
return
}
// 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
}
// 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
}