fix: api descript
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/game/rpc/internal/models/games"
|
||||
"juwan-backend/app/game/rpc/internal/models/predicate"
|
||||
|
||||
"juwan-backend/app/game/rpc/internal/svc"
|
||||
"juwan-backend/app/game/rpc/pb"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchGamesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchGamesLogic {
|
||||
return &SearchGamesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchGamesLogic) SearchGames(in *pb.SearchGamesReq) (*pb.SearchGamesResp, error) {
|
||||
if in.Limit > 1000 {
|
||||
return nil, errors.New("limit too large")
|
||||
}
|
||||
|
||||
preds := make([]predicate.Games, 0, 8)
|
||||
if in.IdOpt != nil {
|
||||
preds = append(preds, games.IDEQ(*in.IdOpt))
|
||||
} else if in.Id != 0 {
|
||||
preds = append(preds, games.IDEQ(in.Id))
|
||||
}
|
||||
if in.NameOpt != nil {
|
||||
if *in.NameOpt != "" {
|
||||
preds = append(preds, games.NameContainsFold(*in.NameOpt))
|
||||
}
|
||||
} else if in.Name != "" {
|
||||
preds = append(preds, games.NameContainsFold(in.Name))
|
||||
}
|
||||
if in.IconOpt != nil {
|
||||
if *in.IconOpt != "" {
|
||||
preds = append(preds, games.IconContainsFold(*in.IconOpt))
|
||||
}
|
||||
} else if in.Icon != "" {
|
||||
preds = append(preds, games.IconContainsFold(in.Icon))
|
||||
}
|
||||
if in.CategoryOpt != nil {
|
||||
if *in.CategoryOpt != "" {
|
||||
preds = append(preds, games.CategoryContainsFold(*in.CategoryOpt))
|
||||
}
|
||||
} else if in.Category != "" {
|
||||
preds = append(preds, games.CategoryContainsFold(in.Category))
|
||||
}
|
||||
if in.SortOrderOpt != nil {
|
||||
preds = append(preds, games.SortOrderEQ(int(*in.SortOrderOpt)))
|
||||
} else if in.SortOrder != 0 {
|
||||
preds = append(preds, games.SortOrderEQ(int(in.SortOrder)))
|
||||
}
|
||||
if in.IsActiveOpt != nil {
|
||||
preds = append(preds, games.IsActiveEQ(*in.IsActiveOpt))
|
||||
} else if in.IsActive {
|
||||
preds = append(preds, games.IsActiveEQ(true))
|
||||
}
|
||||
|
||||
query := l.svcCtx.GameModelRO.Games.Query()
|
||||
if len(preds) > 0 {
|
||||
if in.MatchMode == pb.MatchMode_MATCH_MODE_AND {
|
||||
query = query.Where(games.And(preds...))
|
||||
} else {
|
||||
query = query.Where(games.Or(preds...))
|
||||
}
|
||||
}
|
||||
|
||||
all, err := query.
|
||||
Offset(int(in.Page * in.Limit)).
|
||||
Limit(int(in.Limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("search games failed, %s", err.Error())
|
||||
return nil, errors.New("search games failed")
|
||||
}
|
||||
|
||||
list := make([]*pb.Games, 0, len(all))
|
||||
for _, v := range all {
|
||||
temp := &pb.Games{}
|
||||
err = copier.Copy(temp, &v)
|
||||
if err != nil {
|
||||
logx.Errorf("search games failed, %s", err.Error())
|
||||
continue
|
||||
}
|
||||
temp.CreatedAt = v.CreatedAt.Unix()
|
||||
temp.UpdatedAt = v.UpdatedAt.Unix()
|
||||
list = append(list, temp)
|
||||
}
|
||||
|
||||
return &pb.SearchGamesResp{
|
||||
Games: list,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user