Skip to content

Commit 8fd3fc8

Browse files
committed
feat: 支持用户操作日志记录
1 parent c81927d commit 8fd3fc8

File tree

6 files changed

+97
-8
lines changed

6 files changed

+97
-8
lines changed

internal/api/router.go

+3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ func SetAPIRouter(router *gin.Engine) {
3232
apiRouter.DELETE("/questions/:id", controller.DeleteQuestion)
3333

3434
apiRouter.GET("/user/login", controller.UserLogin) // 登录接口
35+
apiRouter.GET("/user", controller.Auth, controller.UserList) // 自己创建的用户列表
3536
apiRouter.POST("/user", controller.Auth, controller.CreateUser) // 创建用户
3637
apiRouter.DELETE("/user/:id", controller.Auth, controller.DeleteUser) // 删除用户
3738

39+
apiRouter.GET("/logs", controller.LogList) // 删除用户
40+
3841
}
3942

4043
type embedFileSystem struct {

internal/controller/logs.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package controller
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/itihey/tikuAdapter/internal/dao"
6+
)
7+
8+
func LogList(c *gin.Context) {
9+
find, err := dao.Log.Where(dao.Log.UserID.Eq(0)).Find()
10+
if err != nil {
11+
c.JSON(400, gin.H{
12+
"message": "参数错误",
13+
})
14+
return
15+
}
16+
c.JSON(200, find)
17+
18+
}

internal/controller/tiku.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package controller
22

33
import (
4+
"encoding/json"
45
"github.com/gin-gonic/gin"
56
"github.com/itihey/tikuAdapter/internal/dao"
67
"github.com/itihey/tikuAdapter/internal/entity"
78
"github.com/itihey/tikuAdapter/internal/middleware"
9+
"github.com/itihey/tikuAdapter/pkg/logger"
810
"github.com/itihey/tikuAdapter/pkg/util"
911
"net/http"
1012
"strconv"
13+
"time"
1114
)
1215

1316
// Page 分页
@@ -100,13 +103,27 @@ func UpdateQuestions(c *gin.Context) {
100103
})
101104
return
102105
}
106+
// 找到旧答案
107+
oldTiku, err := dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).First()
108+
103109
updates, err := dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).Updates(tiku)
104110
if err != nil {
105111
c.JSON(500, gin.H{
106112
"message": "服务器错误",
107113
})
108114
return
109115
}
116+
if updates.RowsAffected == 1 {
117+
l := entity.Log{
118+
Qid: oldTiku.ID,
119+
Action: 1,
120+
UserID: 0,
121+
CreateTime: time.Now(),
122+
OldAnswer: oldTiku.Answer,
123+
NewAnswer: tiku.Answer,
124+
}
125+
dao.Log.Create(&l)
126+
}
110127
c.JSON(200, gin.H{
111128
"data": updates,
112129
})
@@ -121,6 +138,19 @@ func DeleteQuestion(c *gin.Context) {
121138
})
122139
return
123140
}
141+
142+
// 记录删除日志
143+
oldTiku, err := dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).First()
144+
marshal, _ := json.Marshal(oldTiku)
145+
l := entity.Log{
146+
Qid: oldTiku.ID,
147+
Action: 2,
148+
UserID: 0,
149+
CreateTime: time.Now(),
150+
OldAnswer: string(marshal),
151+
}
152+
dao.Log.Create(&l)
153+
124154
dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).Delete()
125155
c.JSON(200, gin.H{
126156
"message": "删除成功",
@@ -143,6 +173,18 @@ func CreateQuestion(c *gin.Context) {
143173
middleware.FillHash(t)
144174
err := dao.Tiku.Create(t)
145175
if err == nil {
176+
marshal, _ := json.Marshal(t)
177+
l := entity.Log{
178+
Qid: t.ID,
179+
Action: 0,
180+
UserID: 0,
181+
CreateTime: time.Now(),
182+
OldAnswer: string(marshal),
183+
}
184+
err := dao.Log.Create(&l)
185+
if err != nil {
186+
logger.SysError(err.Error())
187+
}
146188
count++
147189
}
148190
}
@@ -153,7 +195,6 @@ func CreateQuestion(c *gin.Context) {
153195

154196
// Courses 获取这个平台的所有课程名称
155197
func Courses(c *gin.Context) {
156-
157198
tx := dao.Tiku.Select(dao.Tiku.CourseName)
158199

159200
if c.Query("plat") != "" {

internal/controller/user.go

+22
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ func UserLogin(c *gin.Context) {
8080
}
8181
}
8282

83+
// UserList 自己开通的用户的列表
84+
func UserList(c *gin.Context) {
85+
parent, _ := c.Get(`user`)
86+
users, err := dao.User.Where(dao.User.ParentID.Eq(parent.(*entity.User).ID)).Find()
87+
if err != nil {
88+
c.JSON(400, gin.H{
89+
"message": "获取用户列表失败",
90+
})
91+
return
92+
}
93+
c.JSON(200, users)
94+
}
95+
8396
// CreateUser 创建用户
8497
func CreateUser(c *gin.Context) {
8598
parent, _ := c.Get(`user`)
@@ -94,6 +107,15 @@ func CreateUser(c *gin.Context) {
94107
return
95108
}
96109

110+
// 检测用户名是否被使用
111+
_, err = dao.User.Where(dao.User.Username.Eq(user.Username)).First()
112+
if err == nil {
113+
c.JSON(400, gin.H{
114+
"message": "用户名已经被使用",
115+
})
116+
return
117+
}
118+
97119
user.ParentID = parent.(*entity.User).ID
98120

99121
err = dao.User.Create(&user)

internal/dao/logs.gen.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/entity/logs.gen.go

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)