-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_controller.go
57 lines (49 loc) · 1.31 KB
/
log_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package controllers
import (
"github.com/gin-gonic/gin"
"github.com/zhsyourai/URCF-engine/http/controllers/shard"
"github.com/zhsyourai/URCF-engine/http/gin-jwt"
"github.com/zhsyourai/URCF-engine/services/log"
"net/http"
"strconv"
)
func NewLogController(middleware *gin_jwt.JwtMiddleware) *LogController {
return &LogController{
service: log.GetInstance(),
middleware: middleware,
}
}
// LogController is our /log controller.
type LogController struct {
service log.Service
middleware *gin_jwt.JwtMiddleware
}
func (c *LogController) Handler(root *gin.RouterGroup) {
root.GET("/list", c.ListLogHandler)
root.DELETE("/*id", c.CleanLogHandler)
}
func (c *LogController) ListLogHandler(ctx *gin.Context) {
var paging shard.Paging
if ctx.BindQuery(&paging) != nil {
return
}
total, logs, err := c.service.ListAll(paging.Page, paging.Size, paging.Sort, paging.Order)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, &shard.LogsWithCount{
TotalCount: total,
Items: logs,
})
}
func (c *LogController) CleanLogHandler(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseInt(idStr, 10, 0)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.service.Clean(id)
ctx.Status(http.StatusOK)
}