a18f96912d
interfaces - refactor: split UserRepository into AuthUserRepository and AdminUserRepository capabilities - refactor: split SessionRepository into AuthSessionRepository and repository-owned CLI/admin methods - refactor: replace generic Repository Update/Delete with operation-specific params and ownership predicates - refactor: replace CredentialRepository generic CRUD with passkey-specific methods - refactor: replace FileRepository generic Create/Update/Delete with UploadedFileParams, DirectoryParams, and owned soft-delete - refactor: remove repository fields from WebApp struct; repositories are now composition-time wiring only - feat: add domain error kinds ErrParentNotFound, ErrParentNotDir, ErrDirectoryNotEmpty, ErrInvalidMove - feat: add CredentialTypeAppPasskey constant - feat: add testutil.SetUserAdmin for test fixture setup that bypasses production service ports - test: add architecture test banning GORM Save in repository package - test: add capability interface contract tests ensuring each service receives the minimal interface - test: add blockingStorage helper for concurrent promotion tests - test: add preserved DSN parameter test for sqliteImmediateDSN - docs: update architecture decisions with repository write rules and capability separation - docs: update roadmap to clarify atomic single-use refresh sessions - docs: add -race test target to development docs
166 lines
5.1 KiB
Go
166 lines
5.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/dhao2001/mygo/internal/model"
|
|
)
|
|
|
|
// isDuplicateKeyError checks if the error indicates a unique constraint violation.
|
|
func isDuplicateKeyError(err error) bool {
|
|
return errors.Is(err, gorm.ErrDuplicatedKey) || strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
|
|
}
|
|
|
|
// RegisteredUserParams contains the fields accepted when registering a user.
|
|
// Administrative and lifecycle fields are intentionally excluded.
|
|
type RegisteredUserParams struct {
|
|
ID string
|
|
Username string
|
|
Email string
|
|
PasswordHash string
|
|
}
|
|
|
|
// AuthUserRepository exposes only user operations required by authentication.
|
|
type AuthUserRepository interface {
|
|
CreateRegisteredUser(ctx context.Context, params RegisteredUserParams) (*model.User, error)
|
|
FindByID(ctx context.Context, id string) (*model.User, error)
|
|
FindByEmail(ctx context.Context, email string) (*model.User, error)
|
|
}
|
|
|
|
// AdminUserRepository exposes only user operations required by administration.
|
|
type AdminUserRepository interface {
|
|
FindByIDIncludeDeleted(ctx context.Context, id string) (*model.User, error)
|
|
MarkAdminDeleted(ctx context.Context, id string) error
|
|
ListIncludeDeleted(ctx context.Context, offset, limit int) ([]model.User, int64, error)
|
|
}
|
|
|
|
// UserRepository is the composition-time union of user capabilities.
|
|
type UserRepository interface {
|
|
AuthUserRepository
|
|
AdminUserRepository
|
|
FindByUsername(ctx context.Context, username string) (*model.User, error)
|
|
List(ctx context.Context, offset, limit int) ([]model.User, int64, error)
|
|
}
|
|
|
|
type userRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserRepository creates a UserRepository backed by GORM.
|
|
func NewUserRepository(db *gorm.DB) UserRepository {
|
|
return &userRepository{db: db}
|
|
}
|
|
|
|
func (r *userRepository) CreateRegisteredUser(ctx context.Context, params RegisteredUserParams) (*model.User, error) {
|
|
user := &model.User{
|
|
ID: params.ID,
|
|
Username: params.Username,
|
|
Email: params.Email,
|
|
PasswordHash: params.PasswordHash,
|
|
IsAdmin: false,
|
|
Status: model.StatusActive,
|
|
}
|
|
result := r.db.WithContext(ctx).Create(user)
|
|
if result.Error != nil {
|
|
if isDuplicateKeyError(result.Error) {
|
|
return nil, model.ErrDuplicate
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (r *userRepository) FindByID(ctx context.Context, id string) (*model.User, error) {
|
|
var user model.User
|
|
result := r.db.WithContext(ctx).First(&user, "id = ? AND status = ?", id, model.StatusActive)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// FindByIDIncludeDeleted finds a user by ID regardless of status.
|
|
func (r *userRepository) FindByIDIncludeDeleted(ctx context.Context, id string) (*model.User, error) {
|
|
var user model.User
|
|
result := r.db.WithContext(ctx).First(&user, "id = ?", id)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*model.User, error) {
|
|
var user model.User
|
|
result := r.db.WithContext(ctx).First(&user, "email = ? AND status = ?", email, model.StatusActive)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) FindByUsername(ctx context.Context, username string) (*model.User, error) {
|
|
var user model.User
|
|
result := r.db.WithContext(ctx).First(&user, "username = ? AND status = ?", username, model.StatusActive)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepository) MarkAdminDeleted(ctx context.Context, id string) error {
|
|
result := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Update("status", model.StatusAdminDeleted)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *userRepository) List(ctx context.Context, offset, limit int) ([]model.User, int64, error) {
|
|
var users []model.User
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("status = ?", model.StatusActive).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := r.db.WithContext(ctx).Where("status = ?", model.StatusActive).Offset(offset).Limit(limit).Find(&users)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return users, total, nil
|
|
}
|
|
|
|
// ListIncludeDeleted returns all users (including soft-deleted), paginated.
|
|
func (r *userRepository) ListIncludeDeleted(ctx context.Context, offset, limit int) ([]model.User, int64, error) {
|
|
var users []model.User
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&users)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return users, total, nil
|
|
}
|