mtfosbot/model/key_commands.go

38 lines
792 B
Go
Raw Normal View History

2018-09-07 08:33:37 +00:00
package model
2018-09-13 10:18:59 +00:00
import (
"errors"
"time"
)
2018-09-07 08:33:37 +00:00
// KeyCommands - struct
type KeyCommands struct {
Key string `db:"key" cc:"key"`
Group string `db:"group" cc:"group"`
Message string `db:"message" cc:"message"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
}
2018-09-13 10:18:59 +00:00
2018-09-14 16:01:36 +00:00
// GetGroupKeyCommand -
func GetGroupKeyCommand(c, g string) (cmd *KeyCommands, err error) {
2018-09-13 10:18:59 +00:00
if len(c) == 0 {
return nil, errors.New("command is empty")
}
2018-09-20 17:14:08 +00:00
query := `select c.* from "public"."key_commands" c
2018-09-13 10:18:59 +00:00
where c."key" = $1
2018-09-20 17:14:08 +00:00
and (c."group" = '' or c."group" = $2)
order by c."group" desc
limit 1`
row := x.QueryRowx(query, c, g)
// err = x.Get(&cmd, query, c, g)
cmd = &KeyCommands{}
err = row.StructScan(cmd)
2018-09-13 10:18:59 +00:00
if err != nil {
return nil, err
}
return
}