From 35379d5c8cbbb787092a7ac3875a7478d52c2f32 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 10 Dec 2018 15:13:06 +0800 Subject: [PATCH] add get line group list api --- model/commands.go | 2 +- router/api/line.go | 42 +++++++++++++++++++++++++++++++++++++++-- router/routes/routes.go | 1 + 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/model/commands.go b/model/commands.go index 6ee8aac..6aced12 100644 --- a/model/commands.go +++ b/model/commands.go @@ -74,7 +74,7 @@ func GetCommandCount(where ...interface{}) (c int, err error) { } // GetCommands - -func GetCommands(where map[string]string, offset, limit int, order map[string]string) (cmds []*Commands, err error) { +func GetCommands(where map[string]string, offset, limit int, order map[string]string) (cmds []*CommandsWithGroup, 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 ` diff --git a/router/api/line.go b/router/api/line.go index b5996ff..57ef575 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -81,7 +81,7 @@ func GetLineMessageLog(c *context.Context) { return } - resMap := make([]map[string]interface{}, 0) + resMap := make([]map[string]interface{}, len(logs)) for _, v := range logs { m := utils.ToMap(v.LineMessageLog) @@ -99,7 +99,7 @@ func GetLineMessageLog(c *context.Context) { }) } -// GetCommands - +// GetCommandList - func GetCommandList(c *context.Context) { numP := 1 if p, ok := c.GetQuery("p"); ok { @@ -118,4 +118,42 @@ func GetCommandList(c *context.Context) { g := c.DefaultQuery("group", "") cmd := c.DefaultQuery("cmd", "") + whereMap := make(map[string]string) + + if len(g) > 0 { + whereMap["group"] = g + } + if len(cmd) > 0 { + whereMap["cmd"] = cmd + } + + count, err := model.GetCommandCount(whereMap) + if err != nil { + c.ServerError(nil) + return + } + + page := utils.CalcPage(count, numP, numMax) + + cmds, err := model.GetCommands(whereMap, page.Offset, page.Limit, nil) + if err != nil { + c.ServerError(nil) + return + } + + cmdList := make([]map[string]interface{}, len(cmds)) + for _, v := range cmds { + tmp := utils.ToMap(v) + tmp["group_name"] = v.GroupName + cmdList = append(cmdList, tmp) + tmp = nil + } + + c.Success(map[string]interface{}{ + "list": cmdList, + "page": map[string]interface{}{ + "cur": page.Page, + "total": page.Total, + }, + }) } diff --git a/router/routes/routes.go b/router/routes/routes.go index f5b7abc..7139158 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -71,6 +71,7 @@ func SetRoutes(r *gin.Engine) { apiGroup.POST("/logout", context.PatchCtx(api.UserLogout)) apiGroup.GET("/line/logs", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineMessageLog)) apiGroup.GET("/line/groups", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineList)) + apiGroup.GET("/line/cmds", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetCommandList)) apiGroup.GET("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData)) apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus)) }