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:
@@ -80,11 +80,8 @@ func (h *PostHandler) GetByID(c *gin.Context) {
|
||||
|
||||
// 注意:不再自动增加浏览量,浏览量通过 RecordView 端点单独记录
|
||||
|
||||
var isLiked, isFavorited bool
|
||||
if currentUserID != "" {
|
||||
isLiked = h.postService.IsLiked(c.Request.Context(), id, currentUserID)
|
||||
isFavorited = h.postService.IsFavorited(c.Request.Context(), id, currentUserID)
|
||||
}
|
||||
// 获取交互状态
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), id, currentUserID)
|
||||
|
||||
// 如果有当前用户,检查与帖子作者的相互关注状态
|
||||
var authorWithFollowStatus *dto.UserResponse
|
||||
@@ -196,19 +193,15 @@ func (h *PostHandler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isLikedMap := make(map[string]bool)
|
||||
isFavoritedMap := make(map[string]bool)
|
||||
if currentUserID != "" {
|
||||
for _, post := range posts {
|
||||
isLiked := h.postService.IsLiked(c.Request.Context(), post.ID, currentUserID)
|
||||
isFavorited := h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
|
||||
isLikedMap[post.ID] = isLiked
|
||||
isFavoritedMap[post.ID] = isFavorited
|
||||
}
|
||||
// 批量获取交互状态
|
||||
postIDs := make([]string, len(posts))
|
||||
for i, post := range posts {
|
||||
postIDs[i] = post.ID
|
||||
}
|
||||
isLikedMap, isFavoritedMap, _ := h.postService.GetPostInteractionStatus(c.Request.Context(), postIDs, currentUserID)
|
||||
|
||||
// 转换为响应结构
|
||||
postResponses := dto.ConvertPostsToResponse(posts, isLikedMap, isFavoritedMap)
|
||||
postResponses := dto.BuildPostsWithInteractionResponse(posts, isLikedMap, isFavoritedMap)
|
||||
|
||||
response.Paginated(c, postResponses, total, page, pageSize)
|
||||
}
|
||||
@@ -265,14 +258,11 @@ func (h *PostHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取交互状态
|
||||
currentUserID := c.GetString("user_id")
|
||||
var isLiked, isFavorited bool
|
||||
if currentUserID != "" {
|
||||
isLiked = h.postService.IsLiked(c.Request.Context(), post.ID, currentUserID)
|
||||
isFavorited = h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
|
||||
}
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), post.ID, currentUserID)
|
||||
|
||||
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
|
||||
response.Success(c, dto.BuildPostResponse(post, isLiked, isFavorited))
|
||||
}
|
||||
|
||||
// Delete 删除帖子
|
||||
@@ -328,10 +318,10 @@ func (h *PostHandler) Like(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
|
||||
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
|
||||
// 获取交互状态
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), id, userID)
|
||||
|
||||
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
|
||||
response.Success(c, dto.BuildPostResponse(post, isLiked, isFavorited))
|
||||
}
|
||||
|
||||
// Unlike 取消点赞
|
||||
@@ -357,10 +347,10 @@ func (h *PostHandler) Unlike(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
|
||||
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
|
||||
// 获取交互状态
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), id, userID)
|
||||
|
||||
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
|
||||
response.Success(c, dto.BuildPostResponse(post, isLiked, isFavorited))
|
||||
}
|
||||
|
||||
// Favorite 收藏帖子
|
||||
@@ -386,10 +376,10 @@ func (h *PostHandler) Favorite(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
|
||||
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
|
||||
// 获取交互状态
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), id, userID)
|
||||
|
||||
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
|
||||
response.Success(c, dto.BuildPostResponse(post, isLiked, isFavorited))
|
||||
}
|
||||
|
||||
// Unfavorite 取消收藏
|
||||
@@ -415,10 +405,10 @@ func (h *PostHandler) Unfavorite(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
|
||||
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
|
||||
// 获取交互状态
|
||||
isLiked, isFavorited := h.postService.GetPostInteractionStatusSingle(c.Request.Context(), id, userID)
|
||||
|
||||
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
|
||||
response.Success(c, dto.BuildPostResponse(post, isLiked, isFavorited))
|
||||
}
|
||||
|
||||
// GetUserPosts 获取用户帖子列表
|
||||
@@ -435,18 +425,15 @@ func (h *PostHandler) GetUserPosts(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取当前用户ID用于判断点赞和收藏状态
|
||||
isLikedMap := make(map[string]bool)
|
||||
isFavoritedMap := make(map[string]bool)
|
||||
if currentUserID != "" {
|
||||
for _, post := range posts {
|
||||
isLikedMap[post.ID] = h.postService.IsLiked(c.Request.Context(), post.ID, currentUserID)
|
||||
isFavoritedMap[post.ID] = h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
|
||||
}
|
||||
// 批量获取交互状态
|
||||
postIDs := make([]string, len(posts))
|
||||
for i, post := range posts {
|
||||
postIDs[i] = post.ID
|
||||
}
|
||||
isLikedMap, isFavoritedMap, _ := h.postService.GetPostInteractionStatus(c.Request.Context(), postIDs, currentUserID)
|
||||
|
||||
// 转换为响应结构
|
||||
postResponses := dto.ConvertPostsToResponse(posts, isLikedMap, isFavoritedMap)
|
||||
postResponses := dto.BuildPostsWithInteractionResponse(posts, isLikedMap, isFavoritedMap)
|
||||
|
||||
response.Paginated(c, postResponses, total, page, pageSize)
|
||||
}
|
||||
@@ -463,19 +450,16 @@ func (h *PostHandler) GetFavorites(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取当前用户ID用于判断点赞和收藏状态
|
||||
// 批量获取交互状态
|
||||
currentUserID := c.GetString("user_id")
|
||||
isLikedMap := make(map[string]bool)
|
||||
isFavoritedMap := make(map[string]bool)
|
||||
if currentUserID != "" {
|
||||
for _, post := range posts {
|
||||
isLikedMap[post.ID] = h.postService.IsLiked(c.Request.Context(), post.ID, currentUserID)
|
||||
isFavoritedMap[post.ID] = h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
|
||||
}
|
||||
postIDs := make([]string, len(posts))
|
||||
for i, post := range posts {
|
||||
postIDs[i] = post.ID
|
||||
}
|
||||
isLikedMap, isFavoritedMap, _ := h.postService.GetPostInteractionStatus(c.Request.Context(), postIDs, currentUserID)
|
||||
|
||||
// 转换为响应结构
|
||||
postResponses := dto.ConvertPostsToResponse(posts, isLikedMap, isFavoritedMap)
|
||||
postResponses := dto.BuildPostsWithInteractionResponse(posts, isLikedMap, isFavoritedMap)
|
||||
|
||||
response.Paginated(c, postResponses, total, page, pageSize)
|
||||
}
|
||||
@@ -492,19 +476,16 @@ func (h *PostHandler) Search(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取当前用户ID用于判断点赞和收藏状态
|
||||
// 批量获取交互状态
|
||||
currentUserID := c.GetString("user_id")
|
||||
isLikedMap := make(map[string]bool)
|
||||
isFavoritedMap := make(map[string]bool)
|
||||
if currentUserID != "" {
|
||||
for _, post := range posts {
|
||||
isLikedMap[post.ID] = h.postService.IsLiked(c.Request.Context(), post.ID, currentUserID)
|
||||
isFavoritedMap[post.ID] = h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
|
||||
}
|
||||
postIDs := make([]string, len(posts))
|
||||
for i, post := range posts {
|
||||
postIDs[i] = post.ID
|
||||
}
|
||||
isLikedMap, isFavoritedMap, _ := h.postService.GetPostInteractionStatus(c.Request.Context(), postIDs, currentUserID)
|
||||
|
||||
// 转换为响应结构
|
||||
postResponses := dto.ConvertPostsToResponse(posts, isLikedMap, isFavoritedMap)
|
||||
postResponses := dto.BuildPostsWithInteractionResponse(posts, isLikedMap, isFavoritedMap)
|
||||
|
||||
response.Paginated(c, postResponses, total, page, pageSize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user