- Add GORM dependencies for SQLite and PostgreSQL - Create domain models (User, Session, File) with common errors - Implement repository interfaces and database layer with migrations - Update WebApp to bootstrap with database and repositories - Add comprehensive unit tests for repository methods - Update config structure to support multiple database drivers - Extend AGENTS.md with debugging principles and dependency rules
94 lines
2.6 KiB
Go
94 lines
2.6 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)
|
|
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) 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
|
|
}
|