feat: 添加搜索收藏微服务
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package favorites
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
"juwan-backend/app/search/rpc/searchservice"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddFavoriteLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
func NewAddFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFavoriteLogic {
|
||||
return &AddFavoriteLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddFavoriteLogic) AddFavorite(req *types.FavoriteReq) (resp *types.EmptyResp, err error) {
|
||||
uid, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
targetID, err := parseSnowflakeID(req.TargetId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.SearchRpc.AddFavorites(l.ctx, &searchservice.AddFavoritesReq{
|
||||
UserId: uid,
|
||||
TargetType: req.TargetType,
|
||||
TargetId: targetID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package favorites
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
"juwan-backend/app/search/rpc/searchservice"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CheckFavoriteLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 检查收藏状态
|
||||
func NewCheckFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckFavoriteLogic {
|
||||
return &CheckFavoriteLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CheckFavoriteLogic) CheckFavorite(req *types.FavoriteCheckReq) (resp *types.FavoriteCheckResp, err error) {
|
||||
uid, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Id != uid {
|
||||
return nil, errors.New("user mismatch")
|
||||
}
|
||||
targetID, err := parseSnowflakeID(req.TargetId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out, err := l.svcCtx.SearchRpc.SearchFavorites(l.ctx, &searchservice.SearchFavoritesReq{
|
||||
Offset: 0,
|
||||
Limit: 1,
|
||||
UserId: &uid,
|
||||
TargetType: &req.TargetType,
|
||||
TargetId: &targetID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.FavoriteCheckResp{Favorited: len(out.GetFavorites()) > 0}, nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package favorites
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
"juwan-backend/app/search/rpc/searchservice"
|
||||
)
|
||||
|
||||
func toAPIFavorite(f *searchservice.Favorites) types.Favorite {
|
||||
return types.Favorite{
|
||||
Id: strconv.FormatInt(f.GetId(), 10),
|
||||
UserId: strconv.FormatInt(f.GetUserId(), 10),
|
||||
TargetType: f.GetTargetType(),
|
||||
TargetId: strconv.FormatInt(f.GetTargetId(), 10),
|
||||
CreatedAt: time.Unix(f.GetCreatedAt(), 0).Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func parseSnowflakeID(value string) (int64, error) {
|
||||
return strconv.ParseInt(value, 10, 64)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package favorites
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
"juwan-backend/app/search/rpc/searchservice"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListFavoritesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取收藏列表
|
||||
func NewListFavoritesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListFavoritesLogic {
|
||||
return &ListFavoritesLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListFavoritesLogic) ListFavorites(req *types.PageReq) (resp *types.FavoriteListResp, err error) {
|
||||
uid, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
limit := req.Limit
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
|
||||
out, err := l.svcCtx.SearchRpc.SearchFavorites(l.ctx, &searchservice.SearchFavoritesReq{
|
||||
Offset: req.Offset,
|
||||
Limit: limit,
|
||||
UserId: &uid,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]types.Favorite, 0, len(out.GetFavorites()))
|
||||
for _, item := range out.GetFavorites() {
|
||||
items = append(items, toAPIFavorite(item))
|
||||
}
|
||||
|
||||
return &types.FavoriteListResp{
|
||||
Items: items,
|
||||
Meta: types.PageMeta{
|
||||
Total: int64(len(items)),
|
||||
Offset: req.Offset,
|
||||
Limit: limit,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package favorites
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
"juwan-backend/app/search/rpc/searchservice"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RemoveFavoriteLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 取消收藏
|
||||
func NewRemoveFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveFavoriteLogic {
|
||||
return &RemoveFavoriteLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RemoveFavoriteLogic) RemoveFavorite(req *types.PathIDReq) (resp *types.EmptyResp, err error) {
|
||||
uid, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
current, err := l.svcCtx.SearchRpc.GetFavoritesById(l.ctx, &searchservice.GetFavoritesByIdReq{Id: req.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if current.GetFavorites() == nil || current.GetFavorites().GetUserId() != uid {
|
||||
return nil, errors.New("favorite not found")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.SearchRpc.DelFavorites(l.ctx, &searchservice.DelFavoritesReq{Id: req.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package search
|
||||
|
||||
import "juwan-backend/app/search/api/internal/types"
|
||||
|
||||
func emptySearchResp(offset, limit int64) *types.SearchResp {
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
return &types.SearchResp{
|
||||
Items: make([]interface{}, 0),
|
||||
Meta: types.PageMeta{
|
||||
Total: 0,
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RecommendationsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 首页推荐
|
||||
func NewRecommendationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecommendationsLogic {
|
||||
return &RecommendationsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RecommendationsLogic) Recommendations(req *types.PageReq) (resp *types.SearchResp, err error) {
|
||||
return emptySearchResp(req.Offset, req.Limit), nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/search/api/internal/svc"
|
||||
"juwan-backend/app/search/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 统一搜索
|
||||
func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
|
||||
return &SearchLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchLogic) Search(req *types.SearchReq) (resp *types.SearchResp, err error) {
|
||||
return emptySearchResp(req.Offset, req.Limit), nil
|
||||
}
|
||||
Reference in New Issue
Block a user