mtfosbot/model/youtube_channel.go

87 lines
2.2 KiB
Go
Raw Normal View History

2018-09-12 10:07:49 +00:00
package model
2018-09-24 11:57:12 +00:00
import (
"database/sql"
"time"
)
2018-09-12 10:07:49 +00:00
2018-09-15 15:09:41 +00:00
// YoutubeGroup -
type YoutubeGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
2018-09-12 10:07:49 +00:00
// YoutubeChannel -
type YoutubeChannel struct {
2018-09-15 15:09:41 +00:00
ID string `db:"id" cc:"id"`
Name string `db:"name" cc:"name"`
LastVideo string `db:"lastvideo" cc:"lastvideo"`
Expire int64 `db:"expire" cc:"expire"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"mtime"`
Groups []*YoutubeGroup `db:"-"`
}
2018-09-17 06:30:49 +00:00
// GetAllYoutubeChannels -
func GetAllYoutubeChannels() (yt []*YoutubeChannel, err error) {
err = x.Select(&yt, `select * from "public"."youtube_channel"`)
return
}
// GetYoutubeChannelsWithExpire -
func GetYoutubeChannelsWithExpire(e int64) (yt []*YoutubeChannel, err error) {
err = x.Select(&yt, `select * from "public"."youtube_channel" where "expire" = -1 or "expire" < $1`, e)
return
}
2018-09-15 15:09:41 +00:00
// GetYoutubeChannelWithID -
func GetYoutubeChannelWithID(id string) (yt *YoutubeChannel, err error) {
2018-09-20 17:14:08 +00:00
yt = &YoutubeChannel{}
err = x.Get(yt, `select * from "public"."youtube_channel" where "id" = $1`, id)
2018-09-24 11:57:12 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
2018-09-15 15:09:41 +00:00
return
}
// Add -
func (p *YoutubeChannel) Add() (err error) {
stmt, err := x.PrepareNamed(`insert into "public"."youtube_channel" ("id", "name") values (:id, :name) returning *`)
if err != nil {
return err
}
err = stmt.Get(p, p)
return
}
2018-09-15 15:09:41 +00:00
// UpdateLastVideo -
func (p *YoutubeChannel) UpdateLastVideo(vid string) (err error) {
p.LastVideo = vid
2018-10-15 08:12:11 +00:00
_, err = x.NamedExec(`update "public"."youtube_channel" set "lastvideo" = :lastvideo, "mtime" = now() where "id" = :id`, p)
2018-09-15 15:09:41 +00:00
return
}
// UpdateExpire -
func (p *YoutubeChannel) UpdateExpire(t int64) (err error) {
p.Expire = t
2018-10-15 08:12:11 +00:00
_, err = x.NamedExec(`update "public"."youtube_channel" set "expire" = :expire, "mtime" = now() where "id" = :id`, p)
2018-09-15 15:09:41 +00:00
if err != nil {
return err
}
return
}
// GetGroups -
func (p *YoutubeChannel) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."youtube_channel" yt
left join "public"."line_youtube_rt" rt
on rt.youtube = yt.id
left join "public"."line_group" g
on g.id = rt.line
where
2018-09-21 10:36:09 +00:00
yt.id = $1
and rt.youtube is not null`
2018-09-15 15:09:41 +00:00
err = x.Select(&p.Groups, query, p.ID)
return
2018-09-12 10:07:49 +00:00
}