feat: 迁移plant
This commit is contained in:
@@ -3,6 +3,7 @@ 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"
|
||||
|
||||
@@ -23,8 +24,48 @@ func NewGetPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPo
|
||||
}
|
||||
}
|
||||
|
||||
// 帖子列表
|
||||
func (l *GetPostListLogic) GetPostList(in *plant.PostListReq) (*plant.PostListResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
db := l.svcCtx.DB.Model(&plantModel.Post{})
|
||||
if in.Keyword != "" {
|
||||
db = db.Where("title like ? OR content like ?", "%"+in.Keyword+"%", "%"+in.Keyword+"%")
|
||||
}
|
||||
if in.UserId != "" {
|
||||
db = db.Where("user_id = ?", in.UserId)
|
||||
}
|
||||
|
||||
return &plant.PostListResp{}, nil
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
pageSize := int(in.PageSize)
|
||||
if pageSize <= 0 {
|
||||
pageSize = 20
|
||||
}
|
||||
offset := (int(in.Current) - 1) * pageSize
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
var list []plantModel.Post
|
||||
if err := db.Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []*plant.PostInfo
|
||||
for _, item := range list {
|
||||
result = append(result, &plant.PostInfo{
|
||||
Id: item.ID,
|
||||
UserId: item.UserID,
|
||||
Title: item.Title,
|
||||
Content: item.Content,
|
||||
ViewCount: int32(item.ViewCount),
|
||||
CommentCount: int32(item.CommentCount),
|
||||
LikeCount: int32(item.LikeCount),
|
||||
StarCount: int32(item.StarCount),
|
||||
Location: item.Location,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
return &plant.PostListResp{List: result, Total: total}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user