59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
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"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type CreatePostLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
|
|
return &CreatePostLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 发布帖子
|
|
func (l *CreatePostLogic) CreatePost(in *plant.CreatePostReq) (*plant.CommonResp, error) {
|
|
err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
|
post := plantModel.Post{
|
|
UserID: in.UserId,
|
|
Title: in.Title,
|
|
Content: in.Content,
|
|
Location: in.Location,
|
|
}
|
|
if err := tx.Create(&post).Error; err != nil {
|
|
return err
|
|
}
|
|
if len(in.ImgIds) > 0 {
|
|
var relations []map[string]interface{}
|
|
for _, id := range in.ImgIds {
|
|
relations = append(relations, map[string]interface{}{
|
|
"post_id": post.ID,
|
|
"oss_id": id,
|
|
})
|
|
}
|
|
if err := tx.Table("sundynix_post_oss").Create(relations).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
|
|
}
|