add get line group list api

This commit is contained in:
Jay 2018-12-10 15:13:06 +08:00
parent 874ab24f6b
commit 35379d5c8c
3 changed files with 42 additions and 3 deletions

View File

@ -74,7 +74,7 @@ func GetCommandCount(where ...interface{}) (c int, err error) {
} }
// GetCommands - // 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" 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 left join "public"."line_group" g
on g.id = c.group ` on g.id = c.group `

View File

@ -81,7 +81,7 @@ func GetLineMessageLog(c *context.Context) {
return return
} }
resMap := make([]map[string]interface{}, 0) resMap := make([]map[string]interface{}, len(logs))
for _, v := range logs { for _, v := range logs {
m := utils.ToMap(v.LineMessageLog) m := utils.ToMap(v.LineMessageLog)
@ -99,7 +99,7 @@ func GetLineMessageLog(c *context.Context) {
}) })
} }
// GetCommands - // GetCommandList -
func GetCommandList(c *context.Context) { func GetCommandList(c *context.Context) {
numP := 1 numP := 1
if p, ok := c.GetQuery("p"); ok { if p, ok := c.GetQuery("p"); ok {
@ -118,4 +118,42 @@ func GetCommandList(c *context.Context) {
g := c.DefaultQuery("group", "") g := c.DefaultQuery("group", "")
cmd := c.DefaultQuery("cmd", "") 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,
},
})
} }

View File

@ -71,6 +71,7 @@ func SetRoutes(r *gin.Engine) {
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout)) apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
apiGroup.GET("/line/logs", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineMessageLog)) 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/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("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData))
apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus)) apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus))
} }