97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"juwan-backend/app/community/rpc/internal/svc"
|
|
"juwan-backend/app/community/rpc/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type SearchPostsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewSearchPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchPostsLogic {
|
|
return &SearchPostsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *SearchPostsLogic) SearchPosts(in *pb.SearchPostsReq) (*pb.SearchPostsResp, error) {
|
|
limit := in.GetLimit()
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
offset := in.GetOffset()
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
|
|
store := l.svcCtx.Store
|
|
store.Mu.RLock()
|
|
defer store.Mu.RUnlock()
|
|
|
|
filtered := make([]*pb.Posts, 0, len(store.Posts))
|
|
for _, p := range store.Posts {
|
|
if p.GetDeletedAt() > 0 {
|
|
continue
|
|
}
|
|
if in.GetId() > 0 && p.GetId() != in.GetId() {
|
|
continue
|
|
}
|
|
if in.AuthorId != nil && p.GetAuthorId() != in.GetAuthorId() {
|
|
continue
|
|
}
|
|
if in.AuthorRole != nil && p.GetAuthorRole() != in.GetAuthorRole() {
|
|
continue
|
|
}
|
|
if in.Title != nil && !strings.Contains(strings.ToLower(p.GetTitle()), strings.ToLower(in.GetTitle())) {
|
|
continue
|
|
}
|
|
if in.Content != nil && !strings.Contains(strings.ToLower(p.GetContent()), strings.ToLower(in.GetContent())) {
|
|
continue
|
|
}
|
|
if len(in.GetTags()) > 0 {
|
|
match := false
|
|
for _, t := range in.GetTags() {
|
|
for _, pt := range p.GetTags() {
|
|
if t == pt {
|
|
match = true
|
|
break
|
|
}
|
|
}
|
|
if match {
|
|
break
|
|
}
|
|
}
|
|
if !match {
|
|
continue
|
|
}
|
|
}
|
|
|
|
cp := *p
|
|
cp.Images = append([]string(nil), p.Images...)
|
|
cp.Tags = append([]string(nil), p.Tags...)
|
|
filtered = append(filtered, &cp)
|
|
}
|
|
|
|
sortPostsDesc(filtered)
|
|
|
|
if offset >= int64(len(filtered)) {
|
|
return &pb.SearchPostsResp{Posts: []*pb.Posts{}}, nil
|
|
}
|
|
end := offset + limit
|
|
if end > int64(len(filtered)) {
|
|
end = int64(len(filtered))
|
|
}
|
|
|
|
return &pb.SearchPostsResp{Posts: filtered[offset:end]}, nil
|
|
}
|