mtfosbot/model/line_group.go

95 lines
2.1 KiB
Go
Raw Permalink Normal View History

2018-09-07 08:33:37 +00:00
package model
2018-09-26 08:17:54 +00:00
import (
"database/sql"
2019-06-25 09:02:03 +00:00
"errors"
2018-09-26 08:17:54 +00:00
"time"
)
2018-09-07 08:33:37 +00:00
// LineGroup - struct
type LineGroup struct {
2019-06-25 09:02:03 +00:00
ID string `db:"id" cc:"id"`
Name string `db:"name" cc:"name"`
Notify bool `db:"notify" cc:"notify"`
Owner string `db:"owner" cc:"owner"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
BotID sql.NullString `db:"bot_id" cc:"bot_id"`
2018-09-07 08:33:37 +00:00
}
2018-09-13 10:18:59 +00:00
// CheckGroup -
func CheckGroup(g string) (exists bool, err error) {
ss := struct {
C int `db:"c"`
}{}
err = x.Get(&ss, `select count(*) as c from "public"."line_group" where "id" = $1`, g)
if err != nil {
return false, err
}
return ss.C > 0, nil
}
2018-09-14 16:01:36 +00:00
// CheckGroupOwner -
func CheckGroupOwner(user, g string) (exists bool, err error) {
ss := struct {
C int `db:"c"`
}{}
err = x.Get(&ss, `select count(*) as c from "public"."line_group" where "id" = $1 and "owner" = $2`, g, user)
if err != nil {
return false, err
}
return ss.C > 0, nil
}
// GetLineGroup -
func GetLineGroup(id string) (g *LineGroup, err error) {
2018-09-20 17:14:08 +00:00
g = &LineGroup{}
err = x.Get(g, `select * from "public"."line_group" where "id" = $1`, id)
2018-09-26 08:17:54 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
2018-09-14 16:01:36 +00:00
return
}
2018-10-14 13:16:58 +00:00
// GetLineGroupList -
func GetLineGroupList() (ls []*LineGroup, err error) {
err = x.Select(&ls, `select * from "public"."line_group" order by "name"`)
if err == sql.ErrNoRows {
return nil, nil
}
return
}
2018-09-14 16:01:36 +00:00
// AddLineGroup -
func AddLineGroup(name, owner string, notify bool) (g *LineGroup, err error) {
2018-09-20 17:14:08 +00:00
g = &LineGroup{}
err = x.Get(g, `insert into "public"."line_group" ("name", "owner", "notify") values ($1, $2, $3)`, name, owner, notify)
2018-09-26 08:17:54 +00:00
if err == sql.ErrNoRows {
return nil, nil
2018-09-14 16:01:36 +00:00
}
return
}
// DeleteGroup -
func (p *LineGroup) DeleteGroup() (err error) {
_, err = x.Exec(`delete from "public"."line_group" where "id" = $1`, p.ID)
return
}
2019-06-25 09:02:03 +00:00
// GetBot - get group binding bot
func (p *LineGroup) GetBot() (bot *LineBot, err error) {
id, err := p.BotID.Value()
if err != nil {
return nil, err
}
var botid string
var ok bool
if botid, ok = id.(string); !ok {
return nil, errors.New("botid get fail")
}
bot, err = GetBotInfo(botid)
return
}