From 31dcbe81bb7c454a9b5233439c68b7fcbb0e2897 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 14 Oct 2018 21:16:58 +0800 Subject: [PATCH] add api line router --- model/line_group.go | 9 +++++ router/api/api.go | 53 --------------------------- router/api/line.go | 79 +++++++++++++++++++++++++++++++++++++++++ router/routes/routes.go | 3 +- 4 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 router/api/line.go diff --git a/model/line_group.go b/model/line_group.go index 28666c4..a8b5594 100644 --- a/model/line_group.go +++ b/model/line_group.go @@ -51,6 +51,15 @@ func GetLineGroup(id string) (g *LineGroup, err error) { return } +// GetLineGroupList - +func GetLineGroupList() (ls []*LineGroup, err error) { + err = x.Select(&ls, `select * from "public"."line_group" order by "name"`) + if err == sql.ErrNoRows { + return nil, nil + } + return +} + // AddLineGroup - func AddLineGroup(name, owner string, notify bool) (g *LineGroup, err error) { g = &LineGroup{} diff --git a/router/api/api.go b/router/api/api.go index 693295e..d38433c 100644 --- a/router/api/api.go +++ b/router/api/api.go @@ -1,12 +1,9 @@ package api import ( - "strconv" - "git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/module/apis/twitch" "git.trj.tw/golang/mtfosbot/module/context" - "git.trj.tw/golang/mtfosbot/module/utils" "github.com/gin-gonic/contrib/sessions" "golang.org/x/crypto/bcrypt" ) @@ -103,53 +100,3 @@ func GetSessionData(c *context.Context) { } c.Success(user) } - -// GetLineMessageLog - -func GetLineMessageLog(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", "") - u := c.DefaultQuery("user", "") - - count, err := model.GetLineMessageLogCount() - if err != nil { - c.ServerError(nil) - return - } - - page := utils.CalcPage(count, numP, numMax) - - logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit) - if err != nil { - c.ServerError(nil) - return - } - - resMap := make([]map[string]interface{}, 0) - - for _, v := range logs { - m := utils.ToMap(v.LineMessageLog) - m["group_name"] = v.GroupName - m["user_name"] = v.UserName - resMap = append(resMap, m) - } - - c.Success(map[string]interface{}{ - "list": resMap, - "page": map[string]interface{}{ - "cur": page.Page, - "total": page.Total, - }, - }) -} diff --git a/router/api/line.go b/router/api/line.go new file mode 100644 index 0000000..4b7cd3d --- /dev/null +++ b/router/api/line.go @@ -0,0 +1,79 @@ +package api + +import ( + "strconv" + + "git.trj.tw/golang/mtfosbot/model" + "git.trj.tw/golang/mtfosbot/module/context" + "git.trj.tw/golang/mtfosbot/module/utils" +) + +// GetLineList - +func GetLineList(c *context.Context) { + ls, err := model.GetLineGroupList() + if err != nil { + c.ServerError(nil) + return + } + list := make([]map[string]interface{}, 0) + + if ls != nil { + for v := range ls { + list = append(list, utils.ToMap(v)) + } + } + + c.Success(map[string]interface{}{ + "list": list, + }) +} + +// GetLineMessageLog - +func GetLineMessageLog(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", "") + u := c.DefaultQuery("user", "") + + count, err := model.GetLineMessageLogCount() + if err != nil { + c.ServerError(nil) + return + } + + page := utils.CalcPage(count, numP, numMax) + + logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit) + if err != nil { + c.ServerError(nil) + return + } + + resMap := make([]map[string]interface{}, 0) + + for _, v := range logs { + m := utils.ToMap(v.LineMessageLog) + m["group_name"] = v.GroupName + m["user_name"] = v.UserName + resMap = append(resMap, m) + } + + c.Success(map[string]interface{}{ + "list": resMap, + "page": map[string]interface{}{ + "cur": page.Page, + "total": page.Total, + }, + }) +} diff --git a/router/routes/routes.go b/router/routes/routes.go index 906a6e8..935484d 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -62,7 +62,8 @@ func SetRoutes(r *gin.Engine) { { apiGroup.POST("/login", context.PatchCtx(api.UserLogin)) apiGroup.POST("/logout", context.PatchCtx(api.UserLogout)) - apiGroup.GET("/line_msg", 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("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData)) apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus)) }