refactor(publish): 公开读改走只读视图,后台仍读基表
This commit is contained in:
@ -16,6 +16,8 @@ import (
|
||||
type StreetSnapRepository interface {
|
||||
// List 按筛选条件分页查询街拍,同时返回符合条件的总数。
|
||||
List(ctx context.Context, q dto.StreetSnapQuery) ([]model.StreetSnap, int64, error)
|
||||
// ListAdmin 后台管理列表:读基表(含 pending / rejected),与 article_repository.ListAdmin 对称。
|
||||
ListAdmin(ctx context.Context, q dto.StreetSnapQuery) ([]model.StreetSnap, int64, error)
|
||||
// FindByID 查询单条街拍;不存在时返回 ErrNotFound。
|
||||
FindByID(ctx context.Context, id string) (*model.StreetSnap, error)
|
||||
// ListImages 查询某条街拍的全部图片,按排序值升序。
|
||||
@ -75,9 +77,39 @@ func snapOrderBy(sort string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// List 公开读:读公开视图,pending / rejected 街拍不可见。
|
||||
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).
|
||||
Table(publicStreetSnaps).
|
||||
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).
|
||||
Table(publicStreetSnaps).
|
||||
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
|
||||
}
|
||||
|
||||
// ListAdmin 后台管理列表:与 List 同构,但读基表 —— 后台必须看得到 pending / rejected,
|
||||
// 否则审核员在列表页看不到待审内容(公开 List 读视图,会把这些行挡掉)。
|
||||
func (r *streetSnapRepository) ListAdmin(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{}).
|
||||
@ -102,9 +134,11 @@ func (r *streetSnapRepository) List(ctx context.Context, q dto.StreetSnapQuery)
|
||||
return rows, total, nil
|
||||
}
|
||||
|
||||
// FindByID 公开读:读公开视图,pending / rejected 街拍视同不存在(返回 ErrNotFound)。
|
||||
func (r *streetSnapRepository) FindByID(ctx context.Context, id string) (*model.StreetSnap, error) {
|
||||
var snap model.StreetSnap
|
||||
err := r.db.WithContext(ctx).
|
||||
Table(publicStreetSnaps).
|
||||
Where("id = ? AND is_deleted = 0", id).
|
||||
First(&snap).Error
|
||||
if err != nil {
|
||||
@ -116,7 +150,19 @@ func (r *streetSnapRepository) FindByID(ctx context.Context, id string) (*model.
|
||||
return &snap, nil
|
||||
}
|
||||
|
||||
// ListImages 公开读:读公开视图,未发布街拍的图片不可见(视图已含 is_deleted 过滤)。
|
||||
func (r *streetSnapRepository) ListImages(ctx context.Context, snapID string) ([]model.StreetSnapImage, error) {
|
||||
var imgs []model.StreetSnapImage
|
||||
err := r.db.WithContext(ctx).
|
||||
Table(publicStreetSnapImages).
|
||||
Where("snap_id = ?", snapID).
|
||||
Order("sort_order ASC, id ASC").
|
||||
Find(&imgs).Error
|
||||
return imgs, err
|
||||
}
|
||||
|
||||
// listImagesBase 读基表(含未发布),仅供后台 GetForEdit 使用 —— 否则编辑待审内容时看不到图。
|
||||
func (r *streetSnapRepository) listImagesBase(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).
|
||||
@ -133,8 +179,9 @@ func (r *streetSnapRepository) ImagesBySnapIDs(ctx context.Context, ids []uint32
|
||||
|
||||
var imgs []model.StreetSnapImage
|
||||
err := r.db.WithContext(ctx).
|
||||
Table(publicStreetSnapImages).
|
||||
Select("snap_id, image, name, sort_order").
|
||||
Where("snap_id IN ? AND is_deleted = 0", ids).
|
||||
Where("snap_id IN ?", ids).
|
||||
Order("sort_order ASC, id ASC").
|
||||
Find(&imgs).Error
|
||||
if err != nil {
|
||||
@ -151,6 +198,7 @@ func (r *streetSnapRepository) ImagesBySnapIDs(ctx context.Context, ids []uint32
|
||||
func (r *streetSnapRepository) Popular(ctx context.Context, limit int) ([]model.StreetSnap, error) {
|
||||
var rows []model.StreetSnap
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table(publicStreetSnaps).
|
||||
Where("is_deleted = 0").
|
||||
Order("image_count DESC, id DESC").
|
||||
Limit(limit).
|
||||
@ -178,7 +226,7 @@ func (r *streetSnapRepository) GetForEdit(ctx context.Context, id uint32) (*mode
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
imgs, err := r.ListImages(ctx, strconv.FormatUint(uint64(id), 10))
|
||||
imgs, err := r.listImagesBase(ctx, strconv.FormatUint(uint64(id), 10))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user