Skip to content

Commit 8682956

Browse files
committed
v2
1 parent b091411 commit 8682956

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
* 标签、分类增删改
1313
* 第三方登录(Github)
1414
* Jwt鉴权
15+
### 第二次修改2023.04.19
16+
* 日志管理
17+
* 统一配置
18+
* 响应封装
19+
* 错误处理完善
20+
21+
22+
1523

1624

1725

api/v1/article.go

+25-47
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package v1
22

33
import (
4-
"fmt"
54
"gin-blog/dao"
65
"gin-blog/models"
7-
"net/http"
6+
"gin-blog/models/response"
87
"strconv"
98

9+
"go.uber.org/zap"
10+
1011
"github.com/gin-gonic/gin"
1112
)
1213

@@ -15,71 +16,48 @@ type Article struct{}
1516
func (*Article) DeleteArticle(c *gin.Context) {
1617
id, err := strconv.Atoi(c.Param("id"))
1718
if err != nil {
18-
return
19+
zap.L().Error("请求参数转换出错", zap.Error(err))
1920
}
20-
i := dao.DeleteArticleById(id)
21-
if i > 0 {
22-
c.JSON(http.StatusOK, gin.H{
23-
"code": 200,
24-
"message": "success",
25-
})
26-
} else {
27-
c.JSON(http.StatusInternalServerError, gin.H{
28-
"code": 500,
29-
"message": "fail",
30-
})
21+
err = dao.DeleteArticleById(id)
22+
if err != nil {
23+
zap.L().Error("删除用户出错了", zap.Error(err))
24+
response.FailWithMessage("删除出错了", c)
3125
}
26+
response.OkWithMessage("删除成功", c)
3227
}
3328

3429
func (*Article) AddArtilce(c *gin.Context) {
3530
var article models.CreatedOrUpdateArticleDto
3631
err := c.ShouldBindJSON(&article)
3732
if err != nil {
38-
c.JSON(http.StatusBadRequest, gin.H{
39-
"error": err.Error(),
40-
})
33+
zap.L().Error("请求参数绑定出错", zap.Error(err))
4134
}
4235

43-
i := dao.CreateArticle(&article)
44-
if i > 0 {
45-
c.JSON(http.StatusOK, gin.H{
46-
"code": 200,
47-
"message": "success",
48-
})
49-
} else {
50-
c.JSON(http.StatusInternalServerError, gin.H{
51-
"code": 500,
52-
"message": "fail",
53-
})
36+
err = dao.CreateArticle(&article)
37+
if err != nil {
38+
zap.L().Error("创建文章出错了", zap.Error(err))
39+
response.FailWithMessage("发出出错了", c)
5440
}
41+
response.OkWithMessage("发布成功", c)
5542

5643
}
5744

5845
func (*Article) UpdateArtilce(c *gin.Context) {
5946
var article models.CreatedOrUpdateArticleDto
6047
id, err := strconv.Atoi(c.Param("id"))
6148
if err != nil {
62-
return
49+
zap.L().Error("请求参数转换出错", zap.Error(err))
6350
}
64-
65-
err2 := c.ShouldBindJSON(&article)
66-
if err2 != nil {
67-
c.JSON(http.StatusBadRequest, gin.H{
68-
"error": err.Error(),
69-
})
51+
err = c.ShouldBindJSON(&article)
52+
if err != nil {
53+
zap.L().Error("请求参数绑定出错", zap.Error(err))
7054
}
7155
article.Id = id
72-
fmt.Printf("article: %v\n", article)
73-
i := dao.UpdateArticleById(&article)
74-
if i > 0 {
75-
c.JSON(http.StatusOK, gin.H{
76-
"code": 200,
77-
"message": "success",
78-
})
79-
} else {
80-
c.JSON(http.StatusInternalServerError, gin.H{
81-
"code": 500,
82-
"message": "fail",
83-
})
56+
57+
err = dao.UpdateArticleById(&article)
58+
if err != nil {
59+
zap.L().Error("更新文章数据出错了", zap.Error(err))
60+
response.FailWithMessage("编辑出错了", c)
8461
}
62+
response.OkWithMessage("编辑成功", c)
8563
}

dao/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetUser(u, p string) (models.User, error) {
3737
func GetUserList(ps, pn int) ([]models.User, int64, error) {
3838
var total int64
3939
list := make([]models.User, 0)
40-
err := database.DB.Debug().
40+
err := database.DB.
4141
Table("users").
4242
Count(&total).
4343
Limit(pn).

log/test.log

+14
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,17 @@
9999
{"level":"ERROR","time":"2023-04-19T02:46:32.780+0800","caller":"v1/comment.go:39","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
100100
{"level":"ERROR","time":"2023-04-19T02:46:32.780+0800","caller":"v1/comment.go:43","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
101101
{"level":"INFO","time":"2023-04-19T02:46:32.783+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/","status":200,"method":"GET","path":"/api/comments/","query":"status=0","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0028217}
102+
{"level":"INFO","time":"2023-04-19T03:18:58.219+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/count","status":200,"method":"GET","path":"/api/comments/count","query":"status=0","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0419816}
103+
{"level":"INFO","time":"2023-04-19T03:18:58.269+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/overview","status":200,"method":"GET","path":"/api/overview","query":"","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0920447}
104+
{"level":"INFO","time":"2023-04-19T03:18:59.775+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/articles","status":200,"method":"GET","path":"/api/articles","query":"pageNum=1&pageSize=10","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0096007}
105+
{"level":"INFO","time":"2023-04-19T03:19:01.848+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/","status":200,"method":"GET","path":"/api/comments/","query":"pageNum=1&pageSize=10","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0072859}
106+
{"level":"INFO","time":"2023-04-19T03:19:03.871+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/categories","status":200,"method":"GET","path":"/api/categories","query":"","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.001535}
107+
{"level":"INFO","time":"2023-04-19T03:19:04.827+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/tags","status":200,"method":"GET","path":"/api/tags","query":"","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0010886}
108+
{"level":"INFO","time":"2023-04-19T03:19:05.835+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/users","status":200,"method":"GET","path":"/api/users","query":"pageNum=1&pageSize=10&username=&email=","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0038272}
109+
{"level":"INFO","time":"2023-04-19T03:19:09.333+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/","status":200,"method":"GET","path":"/api/comments/","query":"pageNum=1&pageSize=10","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0176958}
110+
{"level":"ERROR","time":"2023-04-19T03:19:10.469+0800","caller":"v1/comment.go:39","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
111+
{"level":"ERROR","time":"2023-04-19T03:19:10.470+0800","caller":"v1/comment.go:43","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
112+
{"level":"INFO","time":"2023-04-19T03:19:10.475+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/","status":200,"method":"GET","path":"/api/comments/","query":"status=0","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0057242}
113+
{"level":"ERROR","time":"2023-04-19T03:19:10.894+0800","caller":"v1/comment.go:39","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
114+
{"level":"ERROR","time":"2023-04-19T03:19:10.894+0800","caller":"v1/comment.go:43","msg":"请求参数转换出错","error":"strconv.Atoi: parsing \"\": invalid syntax"}
115+
{"level":"INFO","time":"2023-04-19T03:19:10.897+0800","caller":"middleware/LogMiddleware.go:26","msg":"/api/comments/","status":200,"method":"GET","path":"/api/comments/","query":"","ip":"127.0.0.1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","errors":"","cost":0.0035546}

0 commit comments

Comments
 (0)