pyq
This commit is contained in:
483
internal/api/moment_handler.go
Normal file
483
internal/api/moment_handler.go
Normal file
@@ -0,0 +1,483 @@
|
||||
/**
|
||||
* package api
|
||||
* 作用:处理朋友圈相关的 HTTP 请求接口
|
||||
*/
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"xk-websocket-v2/internal/model"
|
||||
"xk-websocket-v2/internal/service"
|
||||
"xk-websocket-v2/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ==========================================
|
||||
// 动态相关接口
|
||||
// ==========================================
|
||||
|
||||
/**
|
||||
* CreateMomentHandler
|
||||
* 功能:发布动态
|
||||
* 路径:POST /api/moments
|
||||
*/
|
||||
func CreateMomentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
var req model.CreateMomentReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验内容不能为空(除非有媒体)
|
||||
if req.Content == "" && len(req.MediaURLs) == 0 {
|
||||
utils.BadRequest(c, "内容不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
moment, err := service.MomentSvc.CreateMoment(userID, &req)
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, moment, "发布成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteMomentHandler
|
||||
* 功能:删除动态
|
||||
* 路径:DELETE /api/moments/:id
|
||||
*/
|
||||
func DeleteMomentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.MomentSvc.DeleteMoment(userID, uint(momentID)); err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(c, "删除成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetMomentDetailHandler
|
||||
* 功能:获取动态详情
|
||||
* 路径:GET /api/moments/:id
|
||||
*/
|
||||
func GetMomentDetailHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
moment, err := service.MomentSvc.GetMomentDetail(userID, uint(momentID))
|
||||
if err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, moment, "获取成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetMomentsHandler
|
||||
* 功能:获取好友动态列表
|
||||
* 路径:GET /api/moments
|
||||
*/
|
||||
func GetMomentsHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
moments, total, err := service.MomentSvc.GetFriendsMoments(userID, page, pageSize)
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 确保返回空数组而不是null
|
||||
if moments == nil {
|
||||
moments = []model.Moment{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, gin.H{
|
||||
"data": moments,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": pageSize,
|
||||
}, "获取成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetUserMomentsHandler
|
||||
* 功能:获取指定用户的动态列表
|
||||
* 路径:GET /api/moments/user/:user_id
|
||||
*/
|
||||
func GetUserMomentsHandler(c *gin.Context) {
|
||||
viewerID := c.GetString("user_id")
|
||||
if viewerID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
targetUserID := c.Param("user_id")
|
||||
if targetUserID == "" {
|
||||
utils.BadRequest(c, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
moments, total, err := service.MomentSvc.GetUserMoments(viewerID, targetUserID, page, pageSize)
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if moments == nil {
|
||||
moments = []model.Moment{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, gin.H{
|
||||
"data": moments,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": pageSize,
|
||||
}, "获取成功")
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 点赞相关接口
|
||||
// ==========================================
|
||||
|
||||
/**
|
||||
* LikeMomentHandler
|
||||
* 功能:点赞动态
|
||||
* 路径:POST /api/moments/:id/like
|
||||
*/
|
||||
func LikeMomentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.MomentSvc.LikeMoment(userID, uint(momentID)); err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(c, "点赞成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* UnlikeMomentHandler
|
||||
* 功能:取消点赞
|
||||
* 路径:DELETE /api/moments/:id/like
|
||||
*/
|
||||
func UnlikeMomentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.MomentSvc.UnlikeMoment(userID, uint(momentID)); err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(c, "取消点赞成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetMomentLikesHandler
|
||||
* 功能:获取动态的点赞列表
|
||||
* 路径:GET /api/moments/:id/likes
|
||||
*/
|
||||
func GetMomentLikesHandler(c *gin.Context) {
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
likes, err := service.MomentSvc.GetMomentLikes(uint(momentID))
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if likes == nil {
|
||||
likes = []model.MomentLike{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, likes, "获取成功")
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 评论相关接口
|
||||
// ==========================================
|
||||
|
||||
/**
|
||||
* CreateCommentHandler
|
||||
* 功能:发表评论
|
||||
* 路径:POST /api/moments/:id/comments
|
||||
*/
|
||||
func CreateCommentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req model.CreateCommentReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Content == "" {
|
||||
utils.BadRequest(c, "评论内容不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
comment, err := service.MomentSvc.CreateComment(userID, uint(momentID), &req)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, comment, "评论成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteCommentHandler
|
||||
* 功能:删除评论
|
||||
* 路径:DELETE /api/moments/comments/:id
|
||||
*/
|
||||
func DeleteCommentHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
commentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的评论ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.MomentSvc.DeleteComment(userID, uint(commentID)); err != nil {
|
||||
utils.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(c, "删除成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetMomentCommentsHandler
|
||||
* 功能:获取动态的评论列表
|
||||
* 路径:GET /api/moments/:id/comments
|
||||
*/
|
||||
func GetMomentCommentsHandler(c *gin.Context) {
|
||||
momentID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "无效的动态ID")
|
||||
return
|
||||
}
|
||||
|
||||
comments, err := service.MomentSvc.GetMomentComments(uint(momentID))
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if comments == nil {
|
||||
comments = []model.MomentComment{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, comments, "获取成功")
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 通知相关接口
|
||||
// ==========================================
|
||||
|
||||
/**
|
||||
* GetMomentNotificationsHandler
|
||||
* 功能:获取朋友圈通知列表
|
||||
* 路径:GET /api/moments/notifications
|
||||
*/
|
||||
func GetMomentNotificationsHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
notifications, total, err := service.MomentSvc.GetNotifications(userID, page, pageSize)
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if notifications == nil {
|
||||
notifications = []model.MomentNotification{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, gin.H{
|
||||
"data": notifications,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": pageSize,
|
||||
}, "获取成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* MarkNotificationsReadHandler
|
||||
* 功能:标记通知为已读
|
||||
* 路径:POST /api/moments/notifications/read
|
||||
*/
|
||||
func MarkNotificationsReadHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
IDs []uint `json:"ids"`
|
||||
All bool `json:"all"` // 是否标记全部已读
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
if req.All {
|
||||
err = service.MomentSvc.MarkAllNotificationsRead(userID)
|
||||
} else if len(req.IDs) > 0 {
|
||||
err = service.MomentSvc.MarkNotificationsRead(userID, req.IDs)
|
||||
} else {
|
||||
utils.BadRequest(c, "请提供通知ID或设置all为true")
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(c, "标记成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetUnreadCountHandler
|
||||
* 功能:获取未读通知数量
|
||||
* 路径:GET /api/moments/notifications/unread-count
|
||||
*/
|
||||
func GetUnreadCountHandler(c *gin.Context) {
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
utils.Unauthorized(c, "未登录")
|
||||
return
|
||||
}
|
||||
|
||||
count, err := service.MomentSvc.GetUnreadCount(userID)
|
||||
if err != nil {
|
||||
utils.InternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, gin.H{"count": count}, "获取成功")
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 路由注册函数
|
||||
// ==========================================
|
||||
|
||||
/**
|
||||
* RegisterMomentRoutes
|
||||
* 功能:注册朋友圈相关路由
|
||||
*/
|
||||
func RegisterMomentRoutes(r *gin.RouterGroup) {
|
||||
moments := r.Group("/moments")
|
||||
{
|
||||
// 动态相关
|
||||
moments.POST("", CreateMomentHandler)
|
||||
moments.GET("", GetMomentsHandler)
|
||||
moments.GET("/:id", GetMomentDetailHandler)
|
||||
moments.DELETE("/:id", DeleteMomentHandler)
|
||||
moments.GET("/user/:user_id", GetUserMomentsHandler)
|
||||
|
||||
// 点赞相关
|
||||
moments.POST("/:id/like", LikeMomentHandler)
|
||||
moments.DELETE("/:id/like", UnlikeMomentHandler)
|
||||
moments.GET("/:id/likes", GetMomentLikesHandler)
|
||||
|
||||
// 评论相关
|
||||
moments.POST("/:id/comments", CreateCommentHandler)
|
||||
moments.GET("/:id/comments", GetMomentCommentsHandler)
|
||||
moments.DELETE("/comments/:id", DeleteCommentHandler)
|
||||
|
||||
// 通知相关
|
||||
moments.GET("/notifications", GetMomentNotificationsHandler)
|
||||
moments.POST("/notifications/read", MarkNotificationsReadHandler)
|
||||
moments.GET("/notifications/unread-count", GetUnreadCountHandler)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user