/
audit.go
60 lines (55 loc) · 1.31 KB
/
audit.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
58
59
60
package api
import (
"errors"
"net/http"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type auditRoutes struct {
dao.DaoWrapper
}
func configAuditRouter(r *gin.Engine, d dao.DaoWrapper) {
auditRouter := auditRoutes{d}
g := r.Group("/api")
g.Use(tools.Admin)
{
g.GET("/audits", auditRouter.getAudits)
}
}
func (r auditRoutes) getAudits(c *gin.Context) {
type req struct {
Limit int `form:"limit"`
Offset int `form:"offset"`
Types []model.AuditType `form:"types[]"`
}
var reqData req
err := c.BindQuery(&reqData)
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind query",
Err: err,
})
return
}
if len(reqData.Types) == 0 {
reqData.Types = model.GetAllAuditTypes()
}
found, err := r.AuditDao.Find(reqData.Limit, reqData.Offset, reqData.Types...)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not find audits",
Err: err,
})
return
}
res := make([]gin.H, len(found))
for i := range res {
res[i] = found[i].Json()
}
c.JSON(http.StatusOK, res)
}