mtfosbot/model/commands.go

42 lines
912 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
// Commands - struct
type Commands struct {
Cmd string `db:"cmd" cc:"cmd"`
Message string `db:"message" cc:"message"`
Group string `db:"group" cc:"group"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
}
2018-09-13 10:18:59 +00:00
// GetAllCommands -
func GetAllCommands() (cmds []*Commands, err error) {
err = x.Select(&cmds, `select * from "public"."commands"`)
return
}
// GetGroupCommand -
func GetGroupCommand(c, g string) (cmd *Commands, err error) {
if len(c) == 0 {
return nil, errors.New("command is empty")
}
2018-09-20 17:14:08 +00:00
query := `select c.* from "public"."commands" c
2018-09-13 10:18:59 +00:00
where c."cmd" = $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 = &Commands{}
err = row.StructScan(cmd)
2018-09-13 10:18:59 +00:00
if err != nil {
return nil, err
}
return
}