feat: 迁移plant
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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"
|
||||
)
|
||||
|
||||
type StarPostLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewStarPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StarPostLogic {
|
||||
return &StarPostLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||||
}
|
||||
|
||||
func (l *StarPostLogic) StarPost(in *plant.LikePostReq) (*plant.CommonResp, error) {
|
||||
var existing plantModel.UserStar
|
||||
err := l.svcCtx.DB.Where("user_id = ? AND target_id = ? AND type = ?", in.UserId, in.PostId, "post").First(&existing).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 新增收藏
|
||||
star := plantModel.UserStar{UserID: in.UserId, TargetID: in.PostId, Type: "post"}
|
||||
if err2 := l.svcCtx.DB.Create(&star).Error; err2 != nil {
|
||||
return nil, err2
|
||||
}
|
||||
l.svcCtx.DB.Model(&plantModel.Post{}).Where("id = ?", in.PostId).UpdateColumn("star_count", gorm.Expr("star_count + 1"))
|
||||
} else {
|
||||
// 取消收藏
|
||||
l.svcCtx.DB.Delete(&existing)
|
||||
l.svcCtx.DB.Model(&plantModel.Post{}).Where("id = ?", in.PostId).UpdateColumn("star_count", gorm.Expr("GREATEST(star_count - 1, 0)"))
|
||||
}
|
||||
return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user