add api line router

This commit is contained in:
Jay 2018-10-14 21:16:58 +08:00
parent 0b8aeee181
commit 31dcbe81bb
4 changed files with 90 additions and 54 deletions

View File

@ -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{}

View File

@ -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,
},
})
}

79
router/api/line.go Normal file
View File

@ -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,
},
})
}

View File

@ -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))
}