public async Task <bool> CreateHumanCaptain(HumanCaptain inputHumanCaptain)
        {
            _humanCaptainValidator.Validate(inputHumanCaptain);
            await ValidateHumanCaptainCreation(inputHumanCaptain);

            return(await _repository.CreateHumanCaptain(inputHumanCaptain));
        }
        public async Task <bool> UpdateHumanCaptain(HumanCaptain inputHumanCaptain)
        {
            _dataContext.HumanCaptains.Update(inputHumanCaptain);
            await _dataContext.SaveChangesAsync();

            return(true);
        }
Beispiel #3
0
 private static bool ValidateGrade(HumanCaptain inputHumanCaptain)
 {
     if (inputHumanCaptain.Grade.Length <= 1)
     {
         throw new CrewApiException
               {
                   ExceptionMessage = $"HumanCaptain's Grade {inputHumanCaptain.Grade} cannot be 1 character or less.",
                   Severity         = ExceptionSeverity.Error,
                   Type             = ExceptionType.ValidationException
               }
     }
     ;
     return(true);
 }
Beispiel #4
0
        private static bool ValidatePassword(HumanCaptain inputHumanCaptain)
        {
            if (inputHumanCaptain.Password.Length < 6)
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain's Password {inputHumanCaptain.Password} cannot be less than 6 characters.",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ValidationException
                      }
            }
            ;
            if (!Regex.Match(inputHumanCaptain.Password, @"[a-z]", RegexOptions.ECMAScript).Success)
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain's Password {inputHumanCaptain.Password} must contain at least one lowercase character from a-z .",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ValidationException
                      }
            }
            ;
            if (!Regex.Match(inputHumanCaptain.Password, @"[A-Z]", RegexOptions.ECMAScript).Success)
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain's Password {inputHumanCaptain.Password} must contain at least one uppercase character from a-z .",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ValidationException
                      }
            }
            ;
            if (!Regex.Match(inputHumanCaptain.Password, @".[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]", RegexOptions.ECMAScript)
                .Success)
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain's Password {inputHumanCaptain.Password} must contain at least one symbol .",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ValidationException
                      }
            }
            ;
            return(true);
        }
    }
}
        public async Task <bool> UpdateHumanCaptain(HumanCaptain inputHumanCaptain)
        {
            _humanCaptainValidator.Validate(inputHumanCaptain);

            var sameIdHumanCaptains = await _repository.GetAllHumanCaptains(
                new HumanCaptainFilter { Ids = new List <Guid> {
                                             inputHumanCaptain.Id
                                         } });

            if (!sameIdHumanCaptains.Any())
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"Cannot update non-existant humanCaptain {inputHumanCaptain.Id}.",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;

            if (inputHumanCaptain.Name != sameIdHumanCaptains.First().Name)
            {
                var sameNameHumanCaptains = await _repository.GetAllHumanCaptains(
                    new HumanCaptainFilter { ToSearch = inputHumanCaptain.Name, PerfectMatch = true });

                if (sameNameHumanCaptains.Any(h => h.Id != inputHumanCaptain.Id))
                {
                    throw new CrewApiException
                          {
                              ExceptionMessage = $"Cannot update Human Captain name {inputHumanCaptain.Name} to one that already exists.",
                              Severity         = ExceptionSeverity.Error,
                              Type             = ExceptionType.ServiceException
                          }
                }
                ;
            }

            if (inputHumanCaptain.TeamOfExplorersId != sameIdHumanCaptains.First().TeamOfExplorersId)
            {
                var sameIdTeamOfExplorers = await _teamOfExplorersRepository.GetAllTeamsOfExplorers(
                    new TeamOfExplorersFilter { Ids = new List <Guid> {
                                                    inputHumanCaptain.TeamOfExplorersId
                                                } });

                if (!sameIdTeamOfExplorers.Any())
                {
                    throw new CrewApiException
                          {
                              ExceptionMessage = $"Team with id {inputHumanCaptain.TeamOfExplorersId} does not exist.",
                              Severity         = ExceptionSeverity.Error,
                              Type             = ExceptionType.ServiceException
                          }
                }
                ;
                var sameTeamIdHumanCaptains = await _repository.GetAllHumanCaptains(
                    new HumanCaptainFilter { TeamId = inputHumanCaptain.TeamOfExplorersId });

                if (sameTeamIdHumanCaptains.Any())
                {
                    throw new CrewApiException
                          {
                              ExceptionMessage = $"Team with id {inputHumanCaptain.TeamOfExplorersId} has another captain.",
                              Severity         = ExceptionSeverity.Error,
                              Type             = ExceptionType.ServiceException
                          }
                }
                ;
            }

            var humanCaptain = sameIdHumanCaptains.First();

            humanCaptain.UpdateByReflection(inputHumanCaptain);
            return(await _repository.UpdateHumanCaptain(humanCaptain));
        }
        private async Task ValidateHumanCaptainCreation(HumanCaptain inputHumanCaptain)
        {
            var sameIdHumanCaptains = await _repository.GetAllHumanCaptains(
                new HumanCaptainFilter { Ids = new List <Guid>()
                                         {
                                             inputHumanCaptain.Id
                                         } });

            if (sameIdHumanCaptains.Any())
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain with same id {inputHumanCaptain.Id} already exists in the repository !",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;

            var sameIdTeamsOfExplorers = await _teamOfExplorersRepository.GetAllTeamsOfExplorers(
                new TeamOfExplorersFilter { Ids = new List <Guid> {
                                                inputHumanCaptain.TeamOfExplorersId
                                            } });

            if (sameIdTeamsOfExplorers.Count != 1)
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"Human Captain's TeamOfExplorers id {inputHumanCaptain.TeamOfExplorersId} does not correspond to any team !",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;

            var sameTeamIdExplorers = await _explorerRepository.GetAllExplorers(
                new ExplorerFilter { TeamId = inputHumanCaptain.TeamOfExplorersId });

            var sameTeamIdShuttles = await _shuttleRepository.GetAllShuttles(
                new ShuttleFilter { Ids = new List <Guid> {
                                        sameIdTeamsOfExplorers.First().ShuttleId
                                    } });

            if (sameTeamIdShuttles.Any())
            {
                if (sameTeamIdExplorers > sameTeamIdShuttles.First().MaxCrewCapacity)
                {
                    throw new CrewApiException
                          {
                              ExceptionMessage = $"Adding Human captain with id {inputHumanCaptain.TeamOfExplorersId} exceeds shuttle's max crew capacity !",
                              Severity         = ExceptionSeverity.Error,
                              Type             = ExceptionType.ServiceException
                          }
                }
            }
            ;

            var sameTeamIdHumanCaptains = await _repository.GetAllHumanCaptains(
                new HumanCaptainFilter { Ids = new List <Guid> {
                                             inputHumanCaptain.TeamOfExplorersId
                                         } });

            if (sameTeamIdHumanCaptains.Any())
            {
                throw new CrewApiException
                      {
                          ExceptionMessage =
                              $"TeamOfExplorers with id {inputHumanCaptain.TeamOfExplorersId} can have only one human captain !",
                          Severity = ExceptionSeverity.Error,
                          Type     = ExceptionType.ServiceException
                      }
            }
            ;

            var nameAlikeHumanCaptains = await _repository.GetAllHumanCaptains(
                new HumanCaptainFilter { ToSearch = inputHumanCaptain.Name, PerfectMatch = true });

            if (nameAlikeHumanCaptains.Any())
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = $"HumanCaptain name {inputHumanCaptain.Name} is not unique !",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;
        }
    }
}