feat: 更新评论和帖子模型,添加用户信息和原创标识,优化API请求和组件结构

This commit is contained in:
2025-07-28 06:22:07 +08:00
parent 5c20a310e3
commit d73ed493be
20 changed files with 260 additions and 181 deletions

View File

@ -102,13 +102,12 @@ func (cc *CommentController) GetCommentList(ctx context.Context, c *app.RequestC
resps.BadRequest(c, "无效的 target_id")
return
}
req := dto.GetCommentListReq{
Desc: pagination.Desc,
OrderBy: pagination.OrderBy,
Page: pagination.Page,
Size: pagination.Size,
TargetID: uint(targetID),
Desc: pagination.Desc,
OrderBy: pagination.OrderBy,
Page: pagination.Page,
Size: pagination.Size,
TargetID: uint(targetID),
TargetType: c.Query("target_type"),
}
resp, err := cc.service.GetCommentList(ctx, &req)
@ -120,4 +119,6 @@ func (cc *CommentController) GetCommentList(ctx context.Context, c *app.RequestC
resps.Ok(c, resps.Success, resp)
}
func (cc *CommentController) ReactComment(ctx context.Context, c *app.RequestContext) {}
func (cc *CommentController) ReactComment(ctx context.Context, c *app.RequestContext) {
}

View File

@ -2,7 +2,6 @@ package v1
import (
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/snowykami/neo-blog/internal/ctxutils"
@ -95,7 +94,6 @@ func (p *PostController) Update(ctx context.Context, c *app.RequestContext) {
func (p *PostController) List(ctx context.Context, c *app.RequestContext) {
pagination := ctxutils.GetPaginationParams(c)
fmt.Println(pagination)
if pagination.OrderBy == "" {
pagination.OrderBy = constant.OrderByUpdatedAt
}

View File

@ -5,11 +5,13 @@ import "time"
type PostDto struct {
ID uint `json:"id"` // 帖子ID
UserID uint `json:"user_id"` // 发布者的用户ID
User UserDto `json:"user"` // 发布者信息
Title string `json:"title"` // 帖子标题
Content string `json:"content"`
Cover string `json:"cover"` // 帖子封面图
Type string `json:"type"` // 帖子类型 markdown / html / text
Labels []LabelDto `json:"labels"` // 关联的标签
IsOriginal bool `json:"is_original"` // 是否为原创帖子
IsPrivate bool `json:"is_private"` // 是否为私密帖子
LikeCount uint64 `json:"like_count"` // 点赞数
CommentCount uint64 `json:"comment_count"` // 评论数

View File

@ -23,9 +23,9 @@ type UserLoginReq struct {
}
type UserLoginResp struct {
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
User *UserDto `json:"user"`
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
User UserDto `json:"user"`
}
type UserRegisterReq struct {
@ -37,9 +37,9 @@ type UserRegisterReq struct {
}
type UserRegisterResp struct {
Token string `json:"token"` // 访问令牌
RefreshToken string `json:"refresh_token"` // 刷新令牌
User *UserDto `json:"user"` // 用户信息
Token string `json:"token"` // 访问令牌
RefreshToken string `json:"refresh_token"` // 刷新令牌
User UserDto `json:"user"` // 用户信息
}
type VerifyEmailReq struct {
@ -57,9 +57,9 @@ type OidcLoginReq struct {
}
type OidcLoginResp struct {
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
User *UserDto `json:"user"`
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
User UserDto `json:"user"`
}
type ListOidcConfigResp struct {
@ -71,7 +71,7 @@ type GetUserReq struct {
}
type GetUserResp struct {
User *UserDto `json:"user"` // 用户信息
User UserDto `json:"user"` // 用户信息
}
type UpdateUserReq struct {

View File

@ -1,6 +1,7 @@
package model
import (
"fmt"
"github.com/snowykami/neo-blog/internal/dto"
"github.com/snowykami/neo-blog/pkg/constant"
"gorm.io/gorm"
@ -13,10 +14,11 @@ type Post struct {
Title string `gorm:"type:text;not null"` // 帖子标题
Cover string `gorm:"type:text"` // 帖子封面图
Content string `gorm:"type:text;not null"` // 帖子内容
Type string `gorm:"type:text;default:markdown"` // markdown类型支持markdown或html
Type string `gorm:"type:text;default:markdown"` // markdown类型支持markdown或html或txt
CategoryID uint `gorm:"index"` // 帖子分类ID
Category Category `gorm:"foreignKey:CategoryID;references:ID"` // 关联的分类
Labels []Label `gorm:"many2many:post_labels;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` // 关联的标签
IsOriginal bool `gorm:"default:true"` // 是否为原创帖子
IsPrivate bool `gorm:"default:false"`
LikeCount uint64
CommentCount uint64
@ -44,26 +46,36 @@ func (p *Post) AfterUpdate(tx *gorm.DB) (err error) {
return nil
}
func (p *Post) ToDto() dto.PostDto {
return dto.PostDto{
ID: p.ID,
UserID: p.UserID,
Title: p.Title,
Content: p.Content,
Cover: p.Cover,
Type: p.Type,
IsPrivate: p.IsPrivate,
func (p *Post) ToDto() *dto.PostDto {
fmt.Println("User", p.User)
return &dto.PostDto{
ID: p.ID,
UserID: p.UserID,
Title: p.Title,
Content: p.Content,
Cover: p.Cover,
Type: p.Type,
IsOriginal: p.IsOriginal,
IsPrivate: p.IsPrivate,
Labels: func() []dto.LabelDto {
labelDtos := make([]dto.LabelDto, len(p.Labels))
for i, label := range p.Labels {
labelDtos[i] = label.ToDto()
}
return labelDtos
}(),
LikeCount: p.LikeCount,
CommentCount: p.CommentCount,
ViewCount: p.ViewCount,
Heat: p.Heat,
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
User: p.User.ToDto(),
}
}
// ToDtoWithShortContent 返回一个简化的 DTO内容可以根据需要截断
func (p *Post) ToDtoWithShortContent(contentLength int) dto.PostDto {
func (p *Post) ToDtoWithShortContent(contentLength int) *dto.PostDto {
dtoPost := p.ToDto()
if len(p.Content) > contentLength {
dtoPost.Content = p.Content[:contentLength] + "..."

View File

@ -25,8 +25,8 @@ type UserOpenID struct {
Sub string `gorm:"index"` // OIDC Sub openid
}
func (user *User) ToDto() *dto.UserDto {
return &dto.UserDto{
func (user *User) ToDto() dto.UserDto {
return dto.UserDto{
ID: user.ID,
Username: user.Username,
Nickname: user.Nickname,

View File

@ -31,7 +31,7 @@ func (cs *CommentService) CreateComment(ctx context.Context, req *dto.CreateComm
UserID: currentUser.ID,
IsPrivate: req.IsPrivate,
}
err := repo.Comment.CreateComment(comment)
if err != nil {
@ -63,7 +63,7 @@ func (cs *CommentService) UpdateComment(ctx context.Context, req *dto.UpdateComm
if err != nil {
return err
}
return nil
}
@ -100,15 +100,15 @@ func (cs *CommentService) GetComment(ctx context.Context, commentID string) (*dt
}
commentDto := dto.CommentDto{
ID: comment.ID,
TargetID: comment.TargetID,
ID: comment.ID,
TargetID: comment.TargetID,
TargetType: comment.TargetType,
Content: comment.Content,
ReplyID: comment.ReplyID,
Depth: comment.Depth,
CreatedAt: comment.CreatedAt.String(),
UpdatedAt: comment.UpdatedAt.String(),
User: *comment.User.ToDto(),
Content: comment.Content,
ReplyID: comment.ReplyID,
Depth: comment.Depth,
CreatedAt: comment.CreatedAt.String(),
UpdatedAt: comment.UpdatedAt.String(),
User: comment.User.ToDto(),
}
return &commentDto, err
@ -132,8 +132,8 @@ func (cs *CommentService) GetCommentList(ctx context.Context, req *dto.GetCommen
TargetType: comment.TargetType,
CreatedAt: comment.CreatedAt.String(),
UpdatedAt: comment.UpdatedAt.String(),
Depth: comment.Depth,
User: *comment.User.ToDto(),
Depth: comment.Depth,
User: comment.User.ToDto(),
}
commentDtos = append(commentDtos, commentDto)
}

View File

@ -76,18 +76,7 @@ func (p *PostService) GetPost(ctx context.Context, id string) (*dto.PostDto, err
if post.IsPrivate && (!ok || post.UserID != currentUser.ID) {
return nil, errs.ErrForbidden
}
return &dto.PostDto{
UserID: post.UserID,
Title: post.Title,
Content: post.Content,
Labels: func() []dto.LabelDto {
labelDtos := make([]dto.LabelDto, 0)
for _, label := range post.Labels {
labelDtos = append(labelDtos, label.ToDto())
}
return labelDtos
}(),
}, nil
return post.ToDto(), nil
}
func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.CreateOrUpdatePostReq) (uint, error) {
@ -124,8 +113,8 @@ func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.Create
return post.ID, nil
}
func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) ([]dto.PostDto, error) {
postDtos := make([]dto.PostDto, 0)
func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) ([]*dto.PostDto, error) {
postDtos := make([]*dto.PostDto, 0)
currentUserID, _ := ctxutils.GetCurrentUserID(ctx)
posts, err := repo.Post.ListPosts(currentUserID, req.Keywords, req.Page, req.Size, req.OrderBy, req.Desc)
if err != nil {