25f655a6fc
关注之前只做了一半:能从帖子上点「+关注」,也有「关注」信息流,但 关注完就石沉大海——没有用户主页、看不到自己关注了谁、也不知道谁关注 了自己。功能是通的,路是断的。 后端新增: - GET /api/users/:id/profile 公开资料 + 帖子/关注/粉丝三个计数 + 我是否已关注 - GET /api/users/:id/posts TA 发布的帖子 - GET /api/users/:id/relations?kind=following|followers - /api/user/summary 补 following / followers,否则「我的」页那两行永远是 0 关系列表里「我是否关注了这批人」用一次 IN 查询查完,不是每行一次。 小程序端新增两个页面: - pages/user/user 用户主页:头像/昵称/三项计数/关注按钮 + TA 的帖子 - pages/relations 关注列表与粉丝列表(同一页,kind 区分) 入口: - 社区点头像或昵称 → TA 的主页 - 主页点「关注/粉丝」数字 → 对应列表 - 「我的」页新增「我的关注」「我的粉丝」两行 关注按钮先本地翻转再发请求,失败回滚——等接口返回按钮才变会显得很迟钝。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
222 lines
5.8 KiB
Go
222 lines
5.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/datatypes"
|
|
|
|
"github.com/sundynix/pets-be/internal/middleware"
|
|
"github.com/sundynix/pets-be/internal/service"
|
|
"github.com/sundynix/pets-be/pkg/response"
|
|
)
|
|
|
|
type postListReq struct {
|
|
response.PageQuery
|
|
Tab string `form:"tab"`
|
|
}
|
|
|
|
// ListPosts GET /api/posts?tab=&page=&page_size=
|
|
func (h *Handler) ListPosts(c *gin.Context) {
|
|
var req postListReq
|
|
_ = c.ShouldBindQuery(&req)
|
|
req.Normalize()
|
|
|
|
posts, total, err := h.svc.ListPosts(middleware.UserID(c), req.Tab, req.Offset(), req.Limit())
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, response.NewPage(posts, total, req.PageQuery))
|
|
}
|
|
|
|
// GetPost GET /api/posts/:id
|
|
func (h *Handler) GetPost(c *gin.Context) {
|
|
post, err := h.svc.GetPost(idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, post)
|
|
}
|
|
|
|
type postReq struct {
|
|
PetID *string `json:"pet_id"`
|
|
Identity string `json:"identity"`
|
|
Content string `json:"content"`
|
|
Tags []string `json:"tags"`
|
|
Images []string `json:"images"`
|
|
ImageFileIDs []string `json:"image_file_ids"`
|
|
}
|
|
|
|
// CreatePost POST /api/posts
|
|
func (h *Handler) CreatePost(c *gin.Context) {
|
|
var req postReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
in := service.PostInput{PetID: req.PetID, Identity: req.Identity, Content: req.Content}
|
|
if b, err := jsonMarshal(req.Tags); err == nil {
|
|
in.Tags = datatypes.JSON(b)
|
|
}
|
|
if b, err := jsonMarshal(req.Images); err == nil {
|
|
in.Images = datatypes.JSON(b)
|
|
}
|
|
if len(req.ImageFileIDs) > 0 {
|
|
if b, err := jsonMarshal(req.ImageFileIDs); err == nil {
|
|
in.ImageFileIDs = datatypes.JSON(b)
|
|
}
|
|
}
|
|
post, err := h.svc.CreatePost(middleware.UserID(c), in)
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, post)
|
|
}
|
|
|
|
// LikePost POST /api/posts/:id/like
|
|
func (h *Handler) LikePost(c *gin.Context) {
|
|
count, err := h.svc.LikePost(middleware.UserID(c), idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"like_count": count})
|
|
}
|
|
|
|
// UnlikePost DELETE /api/posts/:id/like
|
|
func (h *Handler) UnlikePost(c *gin.Context) {
|
|
count, err := h.svc.UnlikePost(middleware.UserID(c), idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"like_count": count})
|
|
}
|
|
|
|
type commentListReq struct {
|
|
response.PageQuery
|
|
}
|
|
|
|
// ListComments GET /api/posts/:id/comments
|
|
func (h *Handler) ListComments(c *gin.Context) {
|
|
var req commentListReq
|
|
_ = c.ShouldBindQuery(&req)
|
|
req.Normalize()
|
|
|
|
comments, total, err := h.svc.ListComments(middleware.UserID(c), idParam(c, "id"), req.Offset(), req.Limit())
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, response.NewPage(comments, total, req.PageQuery))
|
|
}
|
|
|
|
type commentReq struct {
|
|
Content string `json:"content"`
|
|
Images []string `json:"images"`
|
|
ParentID string `json:"parent_id"` // 回复某条评论时传它的 id
|
|
}
|
|
|
|
// CreateComment POST /api/posts/:id/comments
|
|
func (h *Handler) CreateComment(c *gin.Context) {
|
|
var req commentReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
if req.Content == "" {
|
|
response.FailParams(c, "评论内容不能为空")
|
|
return
|
|
}
|
|
comment, err := h.svc.CreateComment(middleware.UserID(c), idParam(c, "id"), req.Content, req.Images, req.ParentID)
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, comment)
|
|
}
|
|
|
|
// FollowUser POST /api/users/:id/follow
|
|
func (h *Handler) FollowUser(c *gin.Context) {
|
|
if err := h.svc.FollowUser(middleware.UserID(c), idParam(c, "id")); err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"followed": true})
|
|
}
|
|
|
|
// UnfollowUser DELETE /api/users/:id/follow
|
|
func (h *Handler) UnfollowUser(c *gin.Context) {
|
|
if err := h.svc.UnfollowUser(middleware.UserID(c), idParam(c, "id")); err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"followed": false})
|
|
}
|
|
|
|
// DeleteOwnPost DELETE /api/posts/:id (仅限本人)
|
|
func (h *Handler) DeleteOwnPost(c *gin.Context) {
|
|
if err := h.svc.DeleteOwnPost(middleware.UserID(c), idParam(c, "id")); err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"deleted": true})
|
|
}
|
|
|
|
// DeleteOwnComment DELETE /api/comments/:id (仅限本人)
|
|
func (h *Handler) DeleteOwnComment(c *gin.Context) {
|
|
if err := h.svc.DeleteOwnComment(middleware.UserID(c), idParam(c, "id")); err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"deleted": true})
|
|
}
|
|
|
|
// ListReplies GET /api/comments/:id/replies 展开某条评论的全部回复
|
|
func (h *Handler) ListReplies(c *gin.Context) {
|
|
list, err := h.svc.ListReplies(middleware.UserID(c), idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, list)
|
|
}
|
|
|
|
// UserCard GET /api/users/:id/profile 某人的公开资料
|
|
func (h *Handler) UserCard(c *gin.Context) {
|
|
card, err := h.svc.GetUserCard(middleware.UserID(c), idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, card)
|
|
}
|
|
|
|
// UserPosts GET /api/users/:id/posts 某人发布的帖子
|
|
func (h *Handler) UserPosts(c *gin.Context) {
|
|
var req adminListReq
|
|
_ = c.ShouldBindQuery(&req)
|
|
req.Normalize()
|
|
posts, total, err := h.svc.ListUserPosts(middleware.UserID(c), idParam(c, "id"), req.Offset(), req.Limit())
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, response.NewPage(posts, total, req.PageQuery))
|
|
}
|
|
|
|
// Relations GET /api/users/:id/relations?kind=following|followers
|
|
func (h *Handler) Relations(c *gin.Context) {
|
|
var req adminListReq
|
|
_ = c.ShouldBindQuery(&req)
|
|
req.Normalize()
|
|
kind := c.Query("kind")
|
|
list, total, err := h.svc.ListRelations(middleware.UserID(c), idParam(c, "id"), kind, req.Offset(), req.Limit())
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, response.NewPage(list, total, req.PageQuery))
|
|
}
|