feat: 添加搜索收藏微服务
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/search/rpc/internal/models"
|
||||
"juwan-backend/app/search/rpc/internal/models/favorites"
|
||||
"juwan-backend/app/search/rpc/internal/svc"
|
||||
"juwan-backend/app/search/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddFavoritesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddFavoritesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFavoritesLogic {
|
||||
return &AddFavoritesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddFavoritesLogic) AddFavorites(in *pb.AddFavoritesReq) (*pb.AddFavoritesResp, error) {
|
||||
if in.GetUserId() <= 0 {
|
||||
return nil, errors.New("userId is required")
|
||||
}
|
||||
if !validTargetType(in.GetTargetType()) {
|
||||
return nil, errors.New("invalid targetType")
|
||||
}
|
||||
if in.GetTargetId() <= 0 {
|
||||
return nil, errors.New("targetId is required")
|
||||
}
|
||||
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create favorite id failed")
|
||||
}
|
||||
|
||||
created, err := l.svcCtx.SearchModelRW.Favorites.Create().
|
||||
SetID(idResp.Id).
|
||||
SetUserID(in.GetUserId()).
|
||||
SetTargetType(in.GetTargetType()).
|
||||
SetTargetID(in.GetTargetId()).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
if models.IsConstraintError(err) {
|
||||
existing, getErr := l.svcCtx.SearchModelRW.Favorites.Query().
|
||||
Where(
|
||||
favorites.UserIDEQ(in.GetUserId()),
|
||||
favorites.TargetTypeEQ(in.GetTargetType()),
|
||||
favorites.TargetIDEQ(in.GetTargetId()),
|
||||
).
|
||||
Only(l.ctx)
|
||||
if getErr == nil {
|
||||
return &pb.AddFavoritesResp{Id: existing.ID}, nil
|
||||
}
|
||||
}
|
||||
logx.Errorf("addFavorites err: %v", err)
|
||||
return nil, errors.New("add favorite failed")
|
||||
}
|
||||
|
||||
return &pb.AddFavoritesResp{Id: created.ID}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/rpc/internal/svc"
|
||||
"juwan-backend/app/search/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DelFavoritesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDelFavoritesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelFavoritesLogic {
|
||||
return &DelFavoritesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DelFavoritesLogic) DelFavorites(in *pb.DelFavoritesReq) (*pb.DelFavoritesResp, error) {
|
||||
err := l.svcCtx.SearchModelRW.Favorites.DeleteOneID(in.GetId()).Exec(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("delFavorites err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.DelFavoritesResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/rpc/internal/svc"
|
||||
"juwan-backend/app/search/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetFavoritesByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetFavoritesByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFavoritesByIdLogic {
|
||||
return &GetFavoritesByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetFavoritesByIdLogic) GetFavoritesById(in *pb.GetFavoritesByIdReq) (*pb.GetFavoritesByIdResp, error) {
|
||||
f, err := l.svcCtx.SearchModelRO.Favorites.Get(l.ctx, in.GetId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.GetFavoritesByIdResp{Favorites: entFavoriteToPb(f)}, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"juwan-backend/app/search/rpc/internal/models"
|
||||
"juwan-backend/app/search/rpc/pb"
|
||||
)
|
||||
|
||||
func entFavoriteToPb(f *models.Favorites) *pb.Favorites {
|
||||
return &pb.Favorites{
|
||||
Id: f.ID,
|
||||
UserId: f.UserID,
|
||||
TargetType: f.TargetType,
|
||||
TargetId: f.TargetID,
|
||||
CreatedAt: f.CreatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func validTargetType(targetType string) bool {
|
||||
return targetType == "player" || targetType == "shop"
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/search/rpc/internal/models/favorites"
|
||||
"juwan-backend/app/search/rpc/internal/svc"
|
||||
"juwan-backend/app/search/rpc/pb"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchFavoritesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchFavoritesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchFavoritesLogic {
|
||||
return &SearchFavoritesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchFavoritesLogic) SearchFavorites(in *pb.SearchFavoritesReq) (*pb.SearchFavoritesResp, error) {
|
||||
limit := in.GetLimit()
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 100 {
|
||||
return nil, errors.New("limit too large")
|
||||
}
|
||||
offset := in.GetOffset()
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
query := l.svcCtx.SearchModelRO.Favorites.Query()
|
||||
if in.Id != nil {
|
||||
query = query.Where(favorites.IDEQ(in.GetId()))
|
||||
}
|
||||
if in.UserId != nil {
|
||||
query = query.Where(favorites.UserIDEQ(in.GetUserId()))
|
||||
}
|
||||
if in.TargetType != nil {
|
||||
query = query.Where(favorites.TargetTypeEQ(in.GetTargetType()))
|
||||
}
|
||||
if in.TargetId != nil {
|
||||
query = query.Where(favorites.TargetIDEQ(in.GetTargetId()))
|
||||
}
|
||||
|
||||
list, err := query.
|
||||
Order(favorites.ByCreatedAt(sql.OrderDesc()), favorites.ByID(sql.OrderDesc())).
|
||||
Offset(int(offset)).
|
||||
Limit(int(limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("searchFavorites err: %v", err)
|
||||
return nil, errors.New("search favorites failed")
|
||||
}
|
||||
|
||||
out := make([]*pb.Favorites, len(list))
|
||||
for i, f := range list {
|
||||
out[i] = entFavoriteToPb(f)
|
||||
}
|
||||
|
||||
return &pb.SearchFavoritesResp{Favorites: out}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user