update
This commit is contained in:
102
internal/service/favorite_service.go
Normal file
102
internal/service/favorite_service.go
Normal file
@ -0,0 +1,102 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"fashionapi/internal/dto"
|
||||
"fashionapi/internal/model"
|
||||
"fashionapi/internal/repository"
|
||||
)
|
||||
|
||||
// FavoriteService 收藏业务接口。所有方法均要求调用方已通过中间件解析出 userID。
|
||||
type FavoriteService interface {
|
||||
// ListPaged 分页取当前用户收藏(服务端分页,与收藏总量解耦)。
|
||||
ListPaged(ctx context.Context, userID uint32, page, perPage int) ([]dto.FavoriteItem, int64, error)
|
||||
// Add 新增收藏(幂等)。
|
||||
Add(ctx context.Context, userID uint32, req dto.FavoriteAdd) (*dto.FavoriteItem, error)
|
||||
// Remove 删除收藏。
|
||||
Remove(ctx context.Context, userID uint32, targetUID string) error
|
||||
// Check 返回给定 ids 中已收藏的子集(列表页打勾用)。
|
||||
Check(ctx context.Context, userID uint32, ids []string) ([]string, error)
|
||||
}
|
||||
|
||||
type favoriteService struct {
|
||||
favs repository.FavoriteRepository
|
||||
}
|
||||
|
||||
// NewFavoriteService 创建收藏服务。
|
||||
func NewFavoriteService(favs repository.FavoriteRepository) FavoriteService {
|
||||
return &favoriteService{favs: favs}
|
||||
}
|
||||
|
||||
func (s *favoriteService) ListPaged(ctx context.Context, userID uint32, page, perPage int) ([]dto.FavoriteItem, int64, error) {
|
||||
rows, err := s.favs.ListPaged(ctx, userID, page, perPage)
|
||||
if err != nil {
|
||||
return nil, 0, internalErr(err.Error())
|
||||
}
|
||||
total, err := s.favs.CountByUser(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, 0, internalErr(err.Error())
|
||||
}
|
||||
out := make([]dto.FavoriteItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, toFavoriteItem(r))
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
func (s *favoriteService) Add(ctx context.Context, userID uint32, req dto.FavoriteAdd) (*dto.FavoriteItem, error) {
|
||||
if req.TargetType != "runway" && req.TargetType != "street" {
|
||||
return nil, badRequest("invalid target_type")
|
||||
}
|
||||
if req.TargetUID == "" {
|
||||
return nil, badRequest("target_uid required")
|
||||
}
|
||||
row := &model.Favorite{
|
||||
UserID: userID,
|
||||
TargetType: req.TargetType,
|
||||
TargetUID: req.TargetUID,
|
||||
Title: req.Title,
|
||||
Cover: req.Cover,
|
||||
Brand: req.Brand,
|
||||
CreatedAt: uint32(time.Now().Unix()),
|
||||
}
|
||||
if err := s.favs.Add(ctx, row); err != nil {
|
||||
return nil, internalErr(err.Error())
|
||||
}
|
||||
item := toFavoriteItem(*row)
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *favoriteService) Remove(ctx context.Context, userID uint32, targetUID string) error {
|
||||
if targetUID == "" {
|
||||
return badRequest("target_uid required")
|
||||
}
|
||||
if err := s.favs.Remove(ctx, userID, targetUID); err != nil {
|
||||
return internalErr(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *favoriteService) Check(ctx context.Context, userID uint32, ids []string) ([]string, error) {
|
||||
if len(ids) == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
got, err := s.favs.FilterExisting(ctx, userID, ids)
|
||||
if err != nil {
|
||||
return nil, internalErr(err.Error())
|
||||
}
|
||||
return got, nil
|
||||
}
|
||||
|
||||
func toFavoriteItem(r model.Favorite) dto.FavoriteItem {
|
||||
return dto.FavoriteItem{
|
||||
ID: r.TargetUID,
|
||||
Type: r.TargetType,
|
||||
Title: r.Title,
|
||||
Cover: r.Cover,
|
||||
Brand: r.Brand,
|
||||
AddedAt: r.CreatedAt,
|
||||
}
|
||||
}
|
||||
79
internal/service/history_service.go
Normal file
79
internal/service/history_service.go
Normal file
@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"fashionapi/internal/dto"
|
||||
"fashionapi/internal/model"
|
||||
"fashionapi/internal/repository"
|
||||
)
|
||||
|
||||
// HistoryService 浏览历史业务接口。所有方法均要求调用方已通过中间件解析出 userID。
|
||||
type HistoryService interface {
|
||||
// Record 记录一次浏览:点开某篇文章详情时上报其对外编码串 id(upsert:同文章刷新时间)。
|
||||
Record(ctx context.Context, userID uint32, req dto.HistoryAdd) error
|
||||
// ListPaged 分页取当前用户历史(服务端分页)。
|
||||
ListPaged(ctx context.Context, userID uint32, page, perPage int) ([]dto.HistoryItem, int64, error)
|
||||
// Remove 删除某条历史。
|
||||
Remove(ctx context.Context, userID uint32, targetUID string) error
|
||||
// Clear 清空当前用户全部历史。
|
||||
Clear(ctx context.Context, userID uint32) error
|
||||
}
|
||||
|
||||
type historyService struct {
|
||||
hist repository.HistoryRepository
|
||||
}
|
||||
|
||||
// NewHistoryService 创建浏览历史服务。
|
||||
func NewHistoryService(hist repository.HistoryRepository) HistoryService {
|
||||
return &historyService{hist: hist}
|
||||
}
|
||||
|
||||
func (s *historyService) Record(ctx context.Context, userID uint32, req dto.HistoryAdd) error {
|
||||
if req.TargetUID == "" {
|
||||
return badRequest("target_uid required")
|
||||
}
|
||||
row := &model.History{
|
||||
UserID: userID,
|
||||
TargetUID: req.TargetUID,
|
||||
ViewedAt: uint32(time.Now().Unix()),
|
||||
}
|
||||
if err := s.hist.Upsert(ctx, row); err != nil {
|
||||
return internalErr(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *historyService) ListPaged(ctx context.Context, userID uint32, page, perPage int) ([]dto.HistoryItem, int64, error) {
|
||||
rows, err := s.hist.ListPaged(ctx, userID, page, perPage)
|
||||
if err != nil {
|
||||
return nil, 0, internalErr(err.Error())
|
||||
}
|
||||
total, err := s.hist.CountByUser(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, 0, internalErr(err.Error())
|
||||
}
|
||||
out := make([]dto.HistoryItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, dto.HistoryItem{ID: r.TargetUID, ViewedAt: r.ViewedAt})
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
func (s *historyService) Remove(ctx context.Context, userID uint32, targetUID string) error {
|
||||
if targetUID == "" {
|
||||
return badRequest("target_uid required")
|
||||
}
|
||||
if err := s.hist.Remove(ctx, userID, targetUID); err != nil {
|
||||
return internalErr(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *historyService) Clear(ctx context.Context, userID uint32) error {
|
||||
if err := s.hist.Clear(ctx, userID); err != nil {
|
||||
return internalErr(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user