From 874ab24f6ba8e40dbfa74efd2ccb464815971bb1 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 6 Dec 2018 17:44:37 +0800 Subject: [PATCH] updatye --- model/commands.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ router/api/line.go | 21 ++++++++++++ 2 files changed, 103 insertions(+) diff --git a/model/commands.go b/model/commands.go index af6effb..6ee8aac 100644 --- a/model/commands.go +++ b/model/commands.go @@ -2,6 +2,8 @@ package model import ( "errors" + "fmt" + "regexp" "time" ) @@ -14,6 +16,12 @@ type Commands struct { Mtime time.Time `db:"mtime" cc:"ctime"` } +// CommandsWithGroup - +type CommandsWithGroup struct { + Commands + GroupName string `db:"group_name" cc:"group_name"` +} + // GetAllCommands - func GetAllCommands() (cmds []*Commands, err error) { err = x.Select(&cmds, `select * from "public"."commands"`) @@ -39,3 +47,77 @@ func GetGroupCommand(c, g string) (cmd *Commands, err error) { } return } + +// GetCommandCount - +func GetCommandCount(where ...interface{}) (c int, err error) { + query := `select count(*) as c from "public"."commands"` + values := make([]interface{}, 0) + if len(where) > 0 { + if whereMap, ok := where[0].(map[string]string); ok { + str := "" + idx := 1 + for k, v := range whereMap { + if len(str) > 0 { + str += " and " + } + str += fmt.Sprintf(` "%s" = $%d `, k, idx) + idx++ + values = append(values, v) + } + if len(str) > 0 { + query += ` where ` + str + } + } + } + err = x.Get(&c, query, values) + return +} + +// GetCommands - +func GetCommands(where map[string]string, offset, limit int, order map[string]string) (cmds []*Commands, err error) { + query := `select c.*, (case when g.name is null then '' else g.name end) as group_name from "public"."commands" + left join "public"."line_group" g + on g.id = c.group ` + values := make([]interface{}, (len(where) + len(order))) + idx := 1 + + if len(where) > 0 { + str := "" + for k, v := range where { + if len(str) > 0 { + str += " and " + } + str += fmt.Sprintf(` "%s" = $%d `, k, idx) + idx++ + values = append(values, v) + } + if len(str) > 0 { + query += ` where ` + str + } + } + if offset >= 0 { + query += fmt.Sprintf(" offset %d ", offset) + } + if limit > 0 { + query += fmt.Sprintf(" limit %d ", limit) + } + + if len(order) > 0 { + regex, err := regexp.Compile("(?i)(desc|asc)") + if err != nil { + return nil, err + } + str := "" + for k, v := range order { + if !regex.Match([]byte(v)) { + continue + } + if len(str) > 0 { + str += " , " + } + str += fmt.Sprintf(` "%s" %s `, k, v) + } + } + + return +} diff --git a/router/api/line.go b/router/api/line.go index 624dbcd..b5996ff 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -98,3 +98,24 @@ func GetLineMessageLog(c *context.Context) { }, }) } + +// GetCommands - +func GetCommandList(c *context.Context) { + numP := 1 + if p, ok := c.GetQuery("p"); ok { + if i, err := strconv.Atoi(p); err == nil { + numP = i + } + } + + numMax := 20 + if max, ok := c.GetQuery("max"); ok { + if m, err := strconv.Atoi(max); err == nil { + numMax = m + } + } + + g := c.DefaultQuery("group", "") + cmd := c.DefaultQuery("cmd", "") + +}