feat(repository): refactor query/mutation ports with capability-safe

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
This commit is contained in:
2026-07-16 12:24:36 +08:00
parent 6604ecb026
commit a18f96912d
30 changed files with 1259 additions and 394 deletions
+39 -24
View File
@@ -15,19 +15,37 @@ func isDuplicateKeyError(err error) bool {
return errors.Is(err, gorm.ErrDuplicatedKey) || strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
}
// UserRepository provides access to user records.
type UserRepository interface {
Create(ctx context.Context, user *model.User) error
// 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)
FindByIDIncludeDeleted(ctx context.Context, id string) (*model.User, error)
FindByEmail(ctx context.Context, email string) (*model.User, error)
FindByUsername(ctx context.Context, username string) (*model.User, error)
Update(ctx context.Context, user *model.User) error
Delete(ctx context.Context, id string) error
List(ctx context.Context, offset, limit int) ([]model.User, int64, 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
}
@@ -37,15 +55,23 @@ func NewUserRepository(db *gorm.DB) UserRepository {
return &userRepository{db: db}
}
func (r *userRepository) Create(ctx context.Context, user *model.User) error {
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 model.ErrDuplicate
return nil, model.ErrDuplicate
}
return result.Error
return nil, result.Error
}
return nil
return user, nil
}
func (r *userRepository) FindByID(ctx context.Context, id string) (*model.User, error) {
@@ -97,18 +123,7 @@ func (r *userRepository) FindByUsername(ctx context.Context, username string) (*
return &user, nil
}
func (r *userRepository) Update(ctx context.Context, user *model.User) error {
result := r.db.WithContext(ctx).Save(user)
if result.Error != nil {
if isDuplicateKeyError(result.Error) {
return model.ErrDuplicate
}
return result.Error
}
return nil
}
func (r *userRepository) Delete(ctx context.Context, id string) error {
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