Files
hfb_sys/backend/internal/modules/adminmgr/service.go
T

78 lines
1.9 KiB
Go

package adminmgr
import (
"context"
"errors"
)
var ErrDependencyUnavailable = errors.New("dependency unavailable")
type Service struct {
repo *Repository
}
func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if page < 1 {
page = 1
}
if pageSize < 1 || pageSize > 100 {
pageSize = 20
}
return s.repo.List(ctx, page, pageSize)
}
func (s *Service) FindByID(ctx context.Context, id uint64) (*AdminUserDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.FindByID(ctx, id)
}
func (s *Service) Create(ctx context.Context, req CreateAdminRequest) (*AdminUserDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if len(req.Password) < 6 {
return nil, errors.New("password too short")
}
return s.repo.Create(ctx, req)
}
func (s *Service) Update(ctx context.Context, id uint64, req UpdateAdminRequest) (*AdminUserDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.Update(ctx, id, req)
}
func (s *Service) Delete(ctx context.Context, id uint64, currentAdminID uint64) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.Delete(ctx, id, currentAdminID)
}
func (s *Service) AssignRoles(ctx context.Context, adminID uint64, req AssignRolesRequest) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.AssignRoles(ctx, adminID, req.RoleIDs)
}
func (s *Service) ChangePassword(ctx context.Context, id uint64, req ChangePasswordRequest) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
if len(req.NewPassword) < 6 {
return errors.New("new password too short")
}
return s.repo.ChangePassword(ctx, id, req.OldPassword, req.NewPassword)
}