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
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/dhao2001/mygo/internal/model"
|
|
"github.com/dhao2001/mygo/internal/repository"
|
|
)
|
|
|
|
// AdminService handles administrator user-management operations.
|
|
type AdminService struct {
|
|
userRepo repository.AdminUserRepository
|
|
}
|
|
|
|
// NewAdminService creates an AdminService.
|
|
func NewAdminService(userRepo repository.AdminUserRepository) *AdminService {
|
|
return &AdminService{userRepo: userRepo}
|
|
}
|
|
|
|
// GetUser returns a user by ID, including deleted users.
|
|
func (s *AdminService) GetUser(ctx context.Context, id string) (*model.User, error) {
|
|
user, err := s.userRepo.FindByIDIncludeDeleted(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, model.NewNotFoundError("user not found", model.ErrNotFound)
|
|
}
|
|
return nil, model.NewInternalError("find admin user", err)
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
// ListUsers returns all users, including deleted users, with pagination.
|
|
func (s *AdminService) ListUsers(ctx context.Context, offset, limit int) ([]model.User, int64, error) {
|
|
users, total, err := s.userRepo.ListIncludeDeleted(ctx, offset, limit)
|
|
if err != nil {
|
|
return nil, 0, model.NewInternalError("list admin users", err)
|
|
}
|
|
return users, total, nil
|
|
}
|
|
|
|
// DeleteUser soft-deletes a user.
|
|
func (s *AdminService) DeleteUser(ctx context.Context, id string) error {
|
|
if _, err := s.GetUser(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
if err := s.userRepo.MarkAdminDeleted(ctx, id); err != nil {
|
|
return model.NewInternalError("delete admin user", err)
|
|
}
|
|
return nil
|
|
}
|