完成剩余模块 Context 超时控制改造
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package adminmgr
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
|
||||
@@ -12,7 +15,7 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -22,53 +25,53 @@ func (s *Service) List(page, pageSize int) (*PaginatedResult, error) {
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
return s.repo.List(page, pageSize)
|
||||
return s.repo.List(ctx, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) FindByID(id uint64) (*AdminUserDTO, error) {
|
||||
func (s *Service) FindByID(ctx context.Context, id uint64) (*AdminUserDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.FindByID(id)
|
||||
return s.repo.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) Create(req CreateAdminRequest) (*AdminUserDTO, error) {
|
||||
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(req)
|
||||
return s.repo.Create(ctx, req)
|
||||
}
|
||||
|
||||
func (s *Service) Update(id uint64, req UpdateAdminRequest) (*AdminUserDTO, error) {
|
||||
func (s *Service) Update(ctx context.Context, id uint64, req UpdateAdminRequest) (*AdminUserDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Update(id, req)
|
||||
return s.repo.Update(ctx, id, req)
|
||||
}
|
||||
|
||||
func (s *Service) Delete(id uint64, currentAdminID uint64) error {
|
||||
func (s *Service) Delete(ctx context.Context, id uint64, currentAdminID uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Delete(id, currentAdminID)
|
||||
return s.repo.Delete(ctx, id, currentAdminID)
|
||||
}
|
||||
|
||||
func (s *Service) AssignRoles(adminID uint64, req AssignRolesRequest) error {
|
||||
func (s *Service) AssignRoles(ctx context.Context, adminID uint64, req AssignRolesRequest) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.AssignRoles(adminID, req.RoleIDs)
|
||||
return s.repo.AssignRoles(ctx, adminID, req.RoleIDs)
|
||||
}
|
||||
|
||||
func (s *Service) ChangePassword(id uint64, req ChangePasswordRequest) error {
|
||||
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(id, req.OldPassword, req.NewPassword)
|
||||
return s.repo.ChangePassword(ctx, id, req.OldPassword, req.NewPassword)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user