262 lines
8.6 KiB
Go
262 lines
8.6 KiB
Go
package repository
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"strconv"
|
||
"time"
|
||
|
||
"fashionapi/internal/dto"
|
||
"fashionapi/internal/model"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// StreetSnapRepository 街拍数据访问接口。
|
||
type StreetSnapRepository interface {
|
||
// List 按筛选条件分页查询街拍,同时返回符合条件的总数。
|
||
List(ctx context.Context, q dto.StreetSnapQuery) ([]model.StreetSnap, int64, error)
|
||
// FindByID 查询单条街拍;不存在时返回 ErrNotFound。
|
||
FindByID(ctx context.Context, id string) (*model.StreetSnap, error)
|
||
// ListImages 查询某条街拍的全部图片,按排序值升序。
|
||
ListImages(ctx context.Context, snapID string) ([]model.StreetSnapImage, error)
|
||
// ImagesBySnapIDs 批量查询多条街拍的图片,单次 IN 查询避免 N+1。
|
||
ImagesBySnapIDs(ctx context.Context, ids []uint32) (map[uint32][]model.StreetSnapImage, error)
|
||
// Popular 按图片数降序返回热度前 N 个街拍,供 SSG 首页热门街拍使用。
|
||
Popular(ctx context.Context, limit int) ([]model.StreetSnap, error)
|
||
// GetForEdit 取原始街拍记录与图片,供后台编辑表单预填。
|
||
GetForEdit(ctx context.Context, id uint32) (*model.StreetSnap, []model.StreetSnapImage, error)
|
||
// UpdateSnap 编辑后更新字段(白名单,杜绝任意列注入)。
|
||
UpdateSnap(ctx context.Context, id uint32, fields map[string]any) error
|
||
// DeleteSnapImage 软删单张街拍图片。
|
||
DeleteSnapImage(ctx context.Context, imageID uint32) error
|
||
// GetSnapImageKey 取单张街拍图片的七牛 key(删除前清理用);不存在返回空串。
|
||
GetSnapImageKey(ctx context.Context, imageID uint32) (string, error)
|
||
// SoftDeleteSnapImages 软删某街拍的全部图片(级联下架用),返回被删图片的 key 列表。
|
||
SoftDeleteSnapImages(ctx context.Context, snapID uint32) ([]string, error)
|
||
// SetSnapCover 更新街拍封面。
|
||
SetSnapCover(ctx context.Context, snapID uint32, image string) error
|
||
// SetSnapDeleted 上下架:deleted=1 下架,0 上架。
|
||
SetSnapDeleted(ctx context.Context, id uint32, deleted uint8) error
|
||
}
|
||
|
||
type streetSnapRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewStreetSnapRepository 创建街拍仓储。
|
||
func NewStreetSnapRepository(db *gorm.DB) StreetSnapRepository {
|
||
return &streetSnapRepository{db: db}
|
||
}
|
||
|
||
// filterScope 把查询条件编译为 GORM Scope(白名单化,杜绝 SQL 注入)。
|
||
func snapFilterScope(q dto.StreetSnapQuery) func(*gorm.DB) *gorm.DB {
|
||
return func(db *gorm.DB) *gorm.DB {
|
||
db = db.Where("is_deleted = 0")
|
||
if q.Year != 0 {
|
||
db = db.Where("year = ?", q.Year)
|
||
}
|
||
if q.City != "" {
|
||
db = db.Where("city = ?", q.City)
|
||
}
|
||
return db
|
||
}
|
||
}
|
||
|
||
// snapOrderBy 把 sort 参数映射为 ORDER BY(白名单,杜绝注入)。
|
||
func snapOrderBy(sort string) string {
|
||
switch sort {
|
||
case "year_asc":
|
||
return "street_snap.year ASC, street_snap.id ASC"
|
||
case "year_desc":
|
||
return "street_snap.year DESC, street_snap.id DESC"
|
||
default: // image_count
|
||
return "street_snap.image_count DESC, street_snap.id DESC"
|
||
}
|
||
}
|
||
|
||
func (r *streetSnapRepository) List(ctx context.Context, q dto.StreetSnapQuery) ([]model.StreetSnap, int64, error) {
|
||
scope := snapFilterScope(q)
|
||
|
||
var total int64
|
||
if err := r.db.WithContext(ctx).
|
||
Model(&model.StreetSnap{}).
|
||
Scopes(scope).
|
||
Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
if total == 0 {
|
||
return []model.StreetSnap{}, 0, nil
|
||
}
|
||
|
||
var rows []model.StreetSnap
|
||
if err := r.db.WithContext(ctx).
|
||
Model(&model.StreetSnap{}).
|
||
Scopes(scope).
|
||
Order(snapOrderBy(q.Sort)).
|
||
Offset(q.Offset()).
|
||
Limit(q.Size).
|
||
Find(&rows).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
return rows, total, nil
|
||
}
|
||
|
||
func (r *streetSnapRepository) FindByID(ctx context.Context, id string) (*model.StreetSnap, error) {
|
||
var snap model.StreetSnap
|
||
err := r.db.WithContext(ctx).
|
||
Where("id = ? AND is_deleted = 0", id).
|
||
First(&snap).Error
|
||
if err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, ErrNotFound
|
||
}
|
||
return nil, err
|
||
}
|
||
return &snap, nil
|
||
}
|
||
|
||
func (r *streetSnapRepository) ListImages(ctx context.Context, snapID string) ([]model.StreetSnapImage, error) {
|
||
var imgs []model.StreetSnapImage
|
||
err := r.db.WithContext(ctx).
|
||
Where("snap_id = ? AND is_deleted = 0", snapID).
|
||
Order("sort_order ASC, id ASC").
|
||
Find(&imgs).Error
|
||
return imgs, err
|
||
}
|
||
|
||
func (r *streetSnapRepository) ImagesBySnapIDs(ctx context.Context, ids []uint32) (map[uint32][]model.StreetSnapImage, error) {
|
||
result := make(map[uint32][]model.StreetSnapImage, len(ids))
|
||
if len(ids) == 0 {
|
||
return result, nil
|
||
}
|
||
|
||
var imgs []model.StreetSnapImage
|
||
err := r.db.WithContext(ctx).
|
||
Select("snap_id, image, name, sort_order").
|
||
Where("snap_id IN ? AND is_deleted = 0", ids).
|
||
Order("sort_order ASC, id ASC").
|
||
Find(&imgs).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for _, im := range imgs {
|
||
result[im.SnapID] = append(result[im.SnapID], im)
|
||
}
|
||
return result, nil
|
||
}
|
||
|
||
// IDs 返回全部未删除街拍的 id(升序),供 SSG 构建期枚举详情页路径。
|
||
// Popular 按图片数降序返回热度前 N 个街拍(SSG 首页热门街拍用)。
|
||
func (r *streetSnapRepository) Popular(ctx context.Context, limit int) ([]model.StreetSnap, error) {
|
||
var rows []model.StreetSnap
|
||
if err := r.db.WithContext(ctx).
|
||
Where("is_deleted = 0").
|
||
Order("image_count DESC, id DESC").
|
||
Limit(limit).
|
||
Find(&rows).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return rows, nil
|
||
}
|
||
|
||
// ── 后台编辑支持 ──────────────────────────────────────────────────────────────
|
||
|
||
// snapEditable 后台可编辑的街拍字段白名单(键即列名)。
|
||
var snapEditable = map[string]bool{
|
||
"title": true,
|
||
"year": true,
|
||
"city": true,
|
||
"cover": true,
|
||
}
|
||
|
||
func (r *streetSnapRepository) GetForEdit(ctx context.Context, id uint32) (*model.StreetSnap, []model.StreetSnapImage, error) {
|
||
var snap model.StreetSnap
|
||
if err := r.db.WithContext(ctx).Where("id = ? AND is_deleted = 0", id).First(&snap).Error; err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, nil, ErrNotFound
|
||
}
|
||
return nil, nil, err
|
||
}
|
||
imgs, err := r.ListImages(ctx, strconv.FormatUint(uint64(id), 10))
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return &snap, imgs, nil
|
||
}
|
||
|
||
func (r *streetSnapRepository) UpdateSnap(ctx context.Context, id uint32, fields map[string]any) error {
|
||
clean := map[string]any{}
|
||
for k, v := range fields {
|
||
if snapEditable[k] {
|
||
clean[k] = v
|
||
}
|
||
}
|
||
if len(clean) == 0 {
|
||
return nil
|
||
}
|
||
clean["updated_at"] = uint32(time.Now().Unix())
|
||
return r.db.WithContext(ctx).
|
||
Model(&model.StreetSnap{}).
|
||
Where("id = ?", id).
|
||
Updates(clean).Error
|
||
}
|
||
|
||
func (r *streetSnapRepository) DeleteSnapImage(ctx context.Context, imageID uint32) error {
|
||
return r.db.WithContext(ctx).
|
||
Model(&model.StreetSnapImage{}).
|
||
Where("id = ? AND is_deleted = 0", imageID).
|
||
Updates(map[string]any{"is_deleted": 1, "updated_at": uint32(time.Now().Unix())}).Error
|
||
}
|
||
|
||
func (r *streetSnapRepository) GetSnapImageKey(ctx context.Context, imageID uint32) (string, error) {
|
||
var img model.StreetSnapImage
|
||
err := r.db.WithContext(ctx).Where("id = ?", imageID).First(&img).Error
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return "", nil
|
||
}
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return img.Image, nil
|
||
}
|
||
|
||
// SoftDeleteSnapImages 软删某街拍的全部图片(级联下架用)。
|
||
// 先取非删除图片的 key 列表返回给调用方(用于后续七牛孤儿清理),再统一置 is_deleted=1。
|
||
func (r *streetSnapRepository) SoftDeleteSnapImages(ctx context.Context, snapID uint32) ([]string, error) {
|
||
var imgs []model.StreetSnapImage
|
||
if err := r.db.WithContext(ctx).
|
||
Select("image").
|
||
Where("snap_id = ? AND is_deleted = 0", snapID).
|
||
Find(&imgs).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
keys := make([]string, 0, len(imgs))
|
||
for _, im := range imgs {
|
||
if im.Image != "" {
|
||
keys = append(keys, im.Image)
|
||
}
|
||
}
|
||
if err := r.db.WithContext(ctx).
|
||
Model(&model.StreetSnapImage{}).
|
||
Where("snap_id = ? AND is_deleted = 0", snapID).
|
||
Updates(map[string]any{"is_deleted": 1, "updated_at": uint32(time.Now().Unix())}).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return keys, nil
|
||
}
|
||
|
||
func (r *streetSnapRepository) SetSnapCover(ctx context.Context, snapID uint32, image string) error {
|
||
return r.db.WithContext(ctx).
|
||
Model(&model.StreetSnap{}).
|
||
Where("id = ?", snapID).
|
||
Updates(map[string]any{"cover": image, "updated_at": uint32(time.Now().Unix())}).Error
|
||
}
|
||
|
||
func (r *streetSnapRepository) SetSnapDeleted(ctx context.Context, id uint32, deleted uint8) error {
|
||
return r.db.WithContext(ctx).
|
||
Model(&model.StreetSnap{}).
|
||
Where("id = ?", id).
|
||
Updates(map[string]any{"is_deleted": deleted, "updated_at": uint32(time.Now().Unix())}).Error
|
||
}
|