71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"juwan-backend/app/community/rpc/internal/models/posts"
|
|
"juwan-backend/app/community/rpc/internal/svc"
|
|
"juwan-backend/app/community/rpc/pb"
|
|
"juwan-backend/app/snowflake/rpc/snowflake"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AddCommentsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewAddCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCommentsLogic {
|
|
return &AddCommentsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// -----------------------comments-----------------------
|
|
func (l *AddCommentsLogic) AddComments(in *pb.AddCommentsReq) (*pb.AddCommentsResp, error) {
|
|
if in.GetPostId() <= 0 {
|
|
return nil, errors.New("postId is required")
|
|
}
|
|
if in.GetAuthorId() <= 0 {
|
|
return nil, errors.New("authorId is required")
|
|
}
|
|
if in.GetContent() == "" {
|
|
return nil, errors.New("content is required")
|
|
}
|
|
|
|
exists, err := l.svcCtx.CommunityModelRO.Posts.Query().
|
|
Where(posts.IDEQ(in.GetPostId()), posts.DeletedAtIsNil()).
|
|
Exist(l.ctx)
|
|
if err != nil || !exists {
|
|
return nil, errors.New("post not found")
|
|
}
|
|
|
|
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
|
if err != nil {
|
|
return nil, errors.New("create comment id failed")
|
|
}
|
|
|
|
_, err = l.svcCtx.CommunityModelRW.Comments.Create().
|
|
SetID(idResp.Id).
|
|
SetPostID(in.GetPostId()).
|
|
SetAuthorID(in.GetAuthorId()).
|
|
SetContent(in.GetContent()).
|
|
Save(l.ctx)
|
|
if err != nil {
|
|
logx.Errorf("addComments err: %v", err)
|
|
return nil, errors.New("add comment failed")
|
|
}
|
|
|
|
l.svcCtx.CommunityModelRW.Posts.Update().
|
|
Where(posts.IDEQ(in.GetPostId())).
|
|
AddCommentCount(1).
|
|
Exec(l.ctx)
|
|
|
|
return &pb.AddCommentsResp{}, nil
|
|
}
|