feat: 迁移plant
This commit is contained in:
@@ -2,11 +2,12 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
"sundynix-micro-go/app/plant/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LikePostLogic struct {
|
||||
@@ -16,15 +17,21 @@ type LikePostLogic struct {
|
||||
}
|
||||
|
||||
func NewLikePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikePostLogic {
|
||||
return &LikePostLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
return &LikePostLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||||
}
|
||||
|
||||
func (l *LikePostLogic) LikePost(in *plant.LikePostReq) (*plant.CommonResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &plant.CommonResp{}, nil
|
||||
var existing plantModel.PostLike
|
||||
err := l.svcCtx.DB.Where("user_id = ? AND post_id = ?", in.UserId, in.PostId).First(&existing).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
like := plantModel.PostLike{UserID: in.UserId, PostID: in.PostId}
|
||||
if err2 := l.svcCtx.DB.Create(&like).Error; err2 != nil {
|
||||
return nil, err2
|
||||
}
|
||||
l.svcCtx.DB.Model(&plantModel.Post{}).Where("id = ?", in.PostId).UpdateColumn("like_count", gorm.Expr("like_count + 1"))
|
||||
} else {
|
||||
l.svcCtx.DB.Delete(&existing)
|
||||
l.svcCtx.DB.Model(&plantModel.Post{}).Where("id = ?", in.PostId).UpdateColumn("like_count", gorm.Expr("GREATEST(like_count - 1, 0)"))
|
||||
}
|
||||
return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user