Refactor backend APIs to RESTful route patterns.

Align group and conversation handlers/services with path-based endpoints, and unify response/service error handling for related modules.

Made-with: Cursor
This commit is contained in:
2026-03-10 20:52:50 +08:00
parent 86ef150fec
commit 21293644b8
10 changed files with 217 additions and 189 deletions

View File

@@ -11,8 +11,8 @@ import (
"carrot_bbs/internal/dto"
"carrot_bbs/internal/model"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/service"
)
@@ -548,7 +548,7 @@ func (h *MessageHandler) HandleDeleteConversationForSelf(c *gin.Context) {
return
}
conversationID := c.Param("id")
conversationID := getIDParam(c, "id")
if conversationID == "" {
response.BadRequest(c, "conversation id is required")
return
@@ -730,6 +730,11 @@ func (h *MessageHandler) getMyConversationParticipant(conversationID string, use
return nil, nil
}
// getIDParam 从路径参数获取 ID
func getIDParam(c *gin.Context, paramName string) string {
return c.Param(paramName)
}
// ==================== RESTful Action 端点 ====================
// HandleCreateConversation 创建会话
@@ -776,6 +781,7 @@ func (h *MessageHandler) HandleCreateConversation(c *gin.Context) {
// HandleGetConversation 获取会话详情
// GET /api/v1/conversations/get?conversation_id=xxx
// GET /api/v1/conversations/:id
func (h *MessageHandler) HandleGetConversation(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
@@ -783,7 +789,7 @@ func (h *MessageHandler) HandleGetConversation(c *gin.Context) {
return
}
conversationID := c.Query("conversation_id")
conversationID := getIDParam(c, "id")
if conversationID == "" {
response.BadRequest(c, "conversation_id is required")
return
@@ -820,6 +826,7 @@ func (h *MessageHandler) HandleGetConversation(c *gin.Context) {
// HandleGetMessages 获取会话消息
// GET /api/v1/conversations/get_messages?conversation_id=xxx
// GET /api/v1/conversations/:id/messages
func (h *MessageHandler) HandleGetMessages(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
@@ -827,7 +834,7 @@ func (h *MessageHandler) HandleGetMessages(c *gin.Context) {
return
}
conversationID := c.Query("conversation_id")
conversationID := getIDParam(c, "id")
if conversationID == "" {
response.BadRequest(c, "conversation_id is required")
return