@@ -2,6 +2,7 @@ package handlers
22
33import (
44 "errors"
5+ "log/slog"
56 "net/http"
67
78 "github.com/gin-gonic/gin"
@@ -27,8 +28,8 @@ func (h *AnnouncementHandler) GetOne(c *gin.Context) {
2728 c .JSON (http .StatusNotFound , gin.H {"message" : "Announcement not found" })
2829 return
2930 }
30- // 这里的打印可以保留,方便服务器排查
31- println ( "[Server Error] Query Error: " + err . Error () )
31+
32+ slog . Error ( "Database query error" , "handler" , "GetOne" , "id" , id , " err" , err )
3233 c .JSON (http .StatusInternalServerError , gin.H {"message" : "Something went wrong" })
3334 return
3435 }
@@ -39,6 +40,7 @@ func (h *AnnouncementHandler) GetOne(c *gin.Context) {
3940func (h * AnnouncementHandler ) GetAll (c * gin.Context ) {
4041 announcements , err := h .Service .GetAll ()
4142 if err != nil {
43+ slog .Error ("Failed to fetch announcements" , "err" , err )
4244 c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
4345 return
4446 }
@@ -50,13 +52,15 @@ func (h *AnnouncementHandler) Create(c *gin.Context) {
5052 var input models.CreateAnnouncementRequest
5153
5254 if err := c .ShouldBindJSON (& input ); err != nil {
55+ slog .Debug ("Create announcement bind failed" , "err" , err ) // DEBUG 即可,不干扰生产
5356 c .JSON (400 , gin.H {"error" : err .Error ()})
5457 return
5558 }
5659
5760 val , exists := c .Get ("currentUserID" )
5861
5962 if ! exists {
63+ slog .Error ("Create announcement obtain current user information failed" , "err" , "Unable to obtain current user information" )
6064 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Unable to obtain current user information" })
6165 return
6266 }
@@ -68,10 +72,18 @@ func (h *AnnouncementHandler) Create(c *gin.Context) {
6872 }
6973
7074 if err := h .Service .Create (& announcement ); err != nil {
75+ slog .Error ("Create announcement failed" , "err" , err .Error ())
76+
7177 c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
7278 return
7379 }
7480
81+ // 在 c.JSON 成功返回之前
82+ slog .Info ("Announcement created" ,
83+ "id" , announcement .ID ,
84+ "author_id" , announcement .AuthorID ,
85+ "title" , announcement .Title )
86+
7587 c .JSON (http .StatusCreated , gin.H {
7688 "message" : "Created" ,
7789 "data" : announcement ,
@@ -92,6 +104,9 @@ func (h *AnnouncementHandler) Update(c *gin.Context) {
92104 }
93105
94106 if announcement .AuthorID != val {
107+ slog .Warn ("Unauthorized update attempt" ,
108+ "announcement_id" , id ,
109+ "user_id" , val )
95110 c .JSON (http .StatusForbidden , gin.H {"error" : "You do not have permission to modify other people's announcements." })
96111 return
97112 }
@@ -106,10 +121,13 @@ func (h *AnnouncementHandler) Update(c *gin.Context) {
106121 announcement .Content = input .Content
107122
108123 if err := h .Service .Update (announcement ); err != nil {
124+ slog .Error ("Announcement failed" , "id" , id , "operator_id" , val , "err" , err .Error ())
109125 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Update Failed" })
110126 return
111127 }
112128
129+ slog .Info ("Announcement updated" , "id" , id , "operator_id" , val )
130+
113131 c .JSON (http .StatusOK , announcement )
114132
115133}
@@ -125,16 +143,20 @@ func (h *AnnouncementHandler) Delete(c *gin.Context) {
125143 }
126144
127145 if announcement .AuthorID != val {
146+ slog .Warn ("Unauthorized delete attempt" , "id" , id , "user_id" , val )
128147 c .JSON (http .StatusForbidden , gin.H {"error" : "You do not have permission to modify other people's announcements." })
129148 return
130149 }
131150
132151 err = h .Service .Delete (id )
133152 if err != nil {
153+ slog .Error ("Delete Announcement Failed" , "id" , id , "user_id" , val , "err" , err .Error ())
134154 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Cannot Delete" })
135155 return
136156 }
137157
158+ slog .Info ("Announcement deleted" , "id" , id , "operator_id" , val )
159+
138160 c .JSON (http .StatusOK , gin.H {"message" : "deleted" })
139161
140162 return
0 commit comments