package services import ( "context" "tyapi-server/internal/domains/api/entities" repo "tyapi-server/internal/domains/api/repositories" ) type ApiUserAggregateService interface { CreateApiUser(ctx context.Context, apiUserId string) error UpdateWhiteList(ctx context.Context, apiUserId string, whiteList []string) error AddToWhiteList(ctx context.Context, apiUserId string, entry string) error RemoveFromWhiteList(ctx context.Context, apiUserId string, entry string) error FreezeApiUser(ctx context.Context, apiUserId string) error UnfreezeApiUser(ctx context.Context, apiUserId string) error LoadApiUserByUserId(ctx context.Context, apiUserId string) (*entities.ApiUser, error) LoadApiUserByAccessId(ctx context.Context, accessId string) (*entities.ApiUser, error) SaveApiUser(ctx context.Context, apiUser *entities.ApiUser) error } type ApiUserAggregateServiceImpl struct { repo repo.ApiUserRepository } func NewApiUserAggregateService(repo repo.ApiUserRepository) ApiUserAggregateService { return &ApiUserAggregateServiceImpl{repo: repo} } func (s *ApiUserAggregateServiceImpl) CreateApiUser(ctx context.Context, apiUserId string) error { apiUser, err := entities.NewApiUser(apiUserId) if err != nil { return err } if err := apiUser.Validate(); err != nil { return err } return s.repo.Create(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) UpdateWhiteList(ctx context.Context, apiUserId string, whiteList []string) error { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return err } apiUser.UpdateWhiteList(whiteList) return s.repo.Update(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) AddToWhiteList(ctx context.Context, apiUserId string, entry string) error { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return err } err = apiUser.AddToWhiteList(entry) if err != nil { return err } return s.repo.Update(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) RemoveFromWhiteList(ctx context.Context, apiUserId string, entry string) error { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return err } err = apiUser.RemoveFromWhiteList(entry) if err != nil { return err } return s.repo.Update(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) FreezeApiUser(ctx context.Context, apiUserId string) error { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return err } apiUser.Freeze() return s.repo.Update(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) UnfreezeApiUser(ctx context.Context, apiUserId string) error { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return err } apiUser.Unfreeze() return s.repo.Update(ctx, apiUser) } func (s *ApiUserAggregateServiceImpl) LoadApiUserByAccessId(ctx context.Context, accessId string) (*entities.ApiUser, error) { return s.repo.FindByAccessId(ctx, accessId) } func (s *ApiUserAggregateServiceImpl) LoadApiUserByUserId(ctx context.Context, apiUserId string) (*entities.ApiUser, error) { apiUser, err := s.repo.FindByUserId(ctx, apiUserId) if err != nil { return nil, err } // 确保WhiteList不为nil if apiUser.WhiteList == nil { apiUser.WhiteList = []string{} } return apiUser, nil } func (s *ApiUserAggregateServiceImpl) SaveApiUser(ctx context.Context, apiUser *entities.ApiUser) error { exists, err := s.repo.FindByUserId(ctx, apiUser.UserId) if err != nil { return err } if exists != nil { // 确保WhiteList不为nil if apiUser.WhiteList == nil { apiUser.WhiteList = []string{} } return s.repo.Update(ctx, apiUser) } else { return s.repo.Create(ctx, apiUser) } }