This commit is contained in:
2026-10-27 22:48:36 +08:00
parent cf832f0962
commit 1231251ea4
13 changed files with 946 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
// Package file 提供文件元数据(files 表)的数据访问。
// 所有查询均参数化,杜绝 SQL 注入;仅负责持久化,不涉及存储后端 IO。
package file
import (
"context"
"gorm.io/gorm"
"portal-mini-go/internal/model"
"portal-mini-go/internal/pkg/errors"
)
// Repository 文件元数据仓储。
type Repository struct {
db *gorm.DB
}
func New(db *gorm.DB) *Repository { return &Repository{db: db} }
// Create 写入文件记录。
func (r *Repository) Create(ctx context.Context, f *model.File) error {
if err := r.db.WithContext(ctx).Create(f).Error; err != nil {
return errors.Wrap(50000, "create file record", err)
}
return nil
}
// GetByID 按 ID 查询文件元数据,不存在返回 ErrNotFound。
func (r *Repository) GetByID(ctx context.Context, id uint64) (*model.File, error) {
var f model.File
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&f).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.ErrNotFound
}
return nil, errors.Wrap(50000, "query file", err)
}
return &f, nil
}
// Delete 按 ID 删除文件元数据,不存在返回 ErrNotFound。
func (r *Repository) Delete(ctx context.Context, id uint64) error {
res := r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.File{})
if res.Error != nil {
return errors.Wrap(50000, "delete file", res.Error)
}
if res.RowsAffected == 0 {
return errors.ErrNotFound
}
return nil
}