From 03c27e7790cb8c00970ee44d7ca0de80070dad9a Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 21 Jun 2026 22:14:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97=EF=BC=88?= =?UTF-8?q?CRUD=20=E6=93=8D=E4=BD=9C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/api/conversation.go | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/backend/internal/api/conversation.go b/backend/internal/api/conversation.go index 728cc9a..c3eed70 100644 --- a/backend/internal/api/conversation.go +++ b/backend/internal/api/conversation.go @@ -13,6 +13,7 @@ import ( "github.com/hhs/camtalk/internal/models" "github.com/hhs/camtalk/internal/session" "github.com/hhs/camtalk/internal/store" + "github.com/hhs/camtalk/internal/trace" ) // ConversationHandler 提供对话相关的 REST 端点。 @@ -47,6 +48,7 @@ func (h *ConversationHandler) RegisterRoutes(rg *gin.RouterGroup) { // List GET /api/conversations — 获取当前用户的对话列表。 func (h *ConversationHandler) List(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) userID := c.GetString(auth.ContextKeyUserID) page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) @@ -61,6 +63,9 @@ func (h *ConversationHandler) List(c *gin.Context) { summaries, total, err := h.sessionMgr.ListByUser(c.Request.Context(), userID, page, size) if err != nil { + log.Errorw("list conversations failed", + "user_id", userID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to list conversations", @@ -83,6 +88,7 @@ type CreateConversationRequest struct { // Create POST /api/conversations — 创建新对话。 func (h *ConversationHandler) Create(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) userID := c.GetString(auth.ContextKeyUserID) var req CreateConversationRequest @@ -95,6 +101,9 @@ func (h *ConversationHandler) Create(c *gin.Context) { sessionID, err := h.sessionMgr.Create(c.Request.Context(), userID, cfg) if err != nil { + log.Errorw("create conversation failed", + "user_id", userID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to create conversation", @@ -104,6 +113,9 @@ func (h *ConversationHandler) Create(c *gin.Context) { sess, err := h.sessionMgr.Get(c.Request.Context(), sessionID) if err != nil { + log.Errorw("retrieve created conversation failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to retrieve created conversation", @@ -111,6 +123,9 @@ func (h *ConversationHandler) Create(c *gin.Context) { return } + log.Infow("conversation created", + "conversation_id", sess.ID, + "user_id", userID) c.JSON(http.StatusCreated, gin.H{ "id": sess.ID, "title": sess.Title, @@ -144,6 +159,7 @@ type UpdateTitleRequest struct { // UpdateTitle PATCH /api/conversations/:id — 更新对话标题。 func (h *ConversationHandler) UpdateTitle(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) sessionID := c.Param("id") // 先校验归属 @@ -176,6 +192,9 @@ func (h *ConversationHandler) UpdateTitle(c *gin.Context) { }) return } + log.Errorw("update title failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to update title", @@ -190,6 +209,7 @@ func (h *ConversationHandler) UpdateTitle(c *gin.Context) { // Delete DELETE /api/conversations/:id — 删除对话。 func (h *ConversationHandler) Delete(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) sessionID := c.Param("id") // 先校验归属 @@ -205,6 +225,9 @@ func (h *ConversationHandler) Delete(c *gin.Context) { }) return } + log.Errorw("delete conversation failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to delete conversation", @@ -221,6 +244,7 @@ func (h *ConversationHandler) Delete(c *gin.Context) { // - limit: 返回消息数量上限,默认 50 // - before: 消息 ID 游标(用于分页),返回此 ID 之前的消息 func (h *ConversationHandler) GetMessages(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) sessionID := c.Param("id") // 先校验归属 @@ -239,6 +263,9 @@ func (h *ConversationHandler) GetMessages(c *gin.Context) { if h.msgRepo != nil { messages, err := h.msgRepo.GetMessages(c.Request.Context(), sessionID, limit, beforeID) if err != nil { + log.Errorw("get messages failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to get messages", @@ -263,6 +290,9 @@ func (h *ConversationHandler) GetMessages(c *gin.Context) { }) return } + log.Errorw("get messages failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": apperr.CodeInternalError, "message": "failed to get messages",