e2af482cc9
- Implement FileHandler with CRUD operations for files/directories - Add FileService with business logic and SHA-256 hashing - Create LocalStorage backend for filesystem persistence - Add database repository with pagination and name uniqueness constraints - Configure max upload size in storage settings - Include comprehensive tests for all layers
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/dhao2001/mygo/internal/model"
|
|
)
|
|
|
|
// FileRepository provides access to file records.
|
|
type FileRepository interface {
|
|
Create(ctx context.Context, file *model.File) error
|
|
FindByID(ctx context.Context, id string) (*model.File, error)
|
|
FindByUserID(ctx context.Context, userID string, offset, limit int) ([]model.File, int64, error)
|
|
FindByParentID(ctx context.Context, userID string, parentID *string) ([]model.File, error)
|
|
FindByParentIDPaginated(ctx context.Context, userID string, parentID *string, offset, limit int) ([]model.File, int64, error)
|
|
FindByNameAndParent(ctx context.Context, userID string, parentID *string, name string) (*model.File, error)
|
|
Update(ctx context.Context, file *model.File) error
|
|
Delete(ctx context.Context, id string) error
|
|
}
|
|
|
|
type fileRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewFileRepository creates a FileRepository backed by GORM.
|
|
func NewFileRepository(db *gorm.DB) FileRepository {
|
|
return &fileRepository{db: db}
|
|
}
|
|
|
|
func (r *fileRepository) Create(ctx context.Context, file *model.File) error {
|
|
result := r.db.WithContext(ctx).Create(file)
|
|
if result.Error != nil {
|
|
if isDuplicateKeyError(result.Error) {
|
|
return model.ErrDuplicate
|
|
}
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *fileRepository) FindByID(ctx context.Context, id string) (*model.File, error) {
|
|
var file model.File
|
|
result := r.db.WithContext(ctx).First(&file, "id = ?", id)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &file, nil
|
|
}
|
|
|
|
func (r *fileRepository) FindByUserID(ctx context.Context, userID string, offset, limit int) ([]model.File, int64, error) {
|
|
var files []model.File
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&model.File{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := r.db.WithContext(ctx).Where("user_id = ?", userID).Offset(offset).Limit(limit).Find(&files)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return files, total, nil
|
|
}
|
|
|
|
func (r *fileRepository) FindByParentID(ctx context.Context, userID string, parentID *string) ([]model.File, error) {
|
|
var files []model.File
|
|
result := r.db.WithContext(ctx).Where("user_id = ? AND parent_id IS ?", userID, parentID).Find(&files)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
func (r *fileRepository) FindByParentIDPaginated(ctx context.Context, userID string, parentID *string, offset, limit int) ([]model.File, int64, error) {
|
|
var files []model.File
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&model.File{}).Where("user_id = ? AND parent_id IS ?", userID, parentID).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := r.db.WithContext(ctx).Where("user_id = ? AND parent_id IS ?", userID, parentID).Offset(offset).Limit(limit).Order("is_dir DESC, name ASC").Find(&files)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return files, total, nil
|
|
}
|
|
|
|
func (r *fileRepository) FindByNameAndParent(ctx context.Context, userID string, parentID *string, name string) (*model.File, error) {
|
|
var file model.File
|
|
result := r.db.WithContext(ctx).Where("user_id = ? AND parent_id IS ? AND name = ?", userID, parentID, name).First(&file)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &file, nil
|
|
}
|
|
|
|
func (r *fileRepository) Update(ctx context.Context, file *model.File) error {
|
|
result := r.db.WithContext(ctx).Save(file)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *fileRepository) Delete(ctx context.Context, id string) error {
|
|
result := r.db.WithContext(ctx).Delete(&model.File{}, "id = ?", id)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|