public async Task <bool> DeleteTeamOfExplorers(string toSearch, List <Guid> guids)
        {
            var teamsToDelete = await _repository.GetAllTeamsOfExplorers(
                new TeamOfExplorersFilter { ToSearch = toSearch, Ids = guids, PerfectMatch = true });

            if (!teamsToDelete.Any())
            {
                throw new CrewApiException
                      {
                          ExceptionMessage = "TeamOfExplorers is not in the repository.",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;
            var result = true;

            foreach (var team in teamsToDelete)
            {
                var sameTeamIdExplorers = await _explorerRepository.GetAllExplorers(
                    new ExplorerFilter { TeamId = team.Id });

                if (sameTeamIdExplorers != 0)
                {
                    throw new CrewApiException
                          {
                              ExceptionMessage = $"Explorers still contain the team id {team.Id}. Delete them before removing the explorers team.",
                              Severity         = ExceptionSeverity.Error,
                              Type             = ExceptionType.ServiceException
                          }
                }
                ;
                result = result && await _repository.DeleteTeamOfExplorers(team.Id);
            }

            return(result);
        }
        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
                      }
            }
            ;
        }
    }
}
Beispiel #3
0
        private async Task ValidateRobotCreation(Robot inputRobot)
        {
            var sameIdRobots = await _repository.GetAllRobots(
                new RobotFilter { Ids = new List <Guid>()
                                  {
                                      inputRobot.Id
                                  } });

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

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

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

            var sameTeamIdExplorers = await _explorerRepository.GetAllExplorers(
                new ExplorerFilter { TeamId = inputRobot.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 Robot with id {inputRobot.TeamOfExplorersId} exceeds shuttle's {sameTeamIdShuttles.First().Id} max crew capacity !",
                              Severity = ExceptionSeverity.Error,
                              Type     = ExceptionType.ServiceException
                          }
                }
            }
            ;

            var nameAlikeRobots = await _repository.GetAllRobots(
                new RobotFilter { ToSearch = inputRobot.Name, PerfectMatch = true });

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

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

            if (correspondingTeam.Any())
            {
                var correspondingShuttle = await _shuttleRepository.GetAllShuttles(
                    new ShuttleFilter { Ids = new List <Guid> {
                                            correspondingTeam.First().ShuttleId
                                        } });

                if (correspondingShuttle.Any())
                {
                    var client   = _clientFactory.CreateClient();
                    var planetId = Guid.Parse(await(await client.GetAsync(
                                                        $"http://localhost:5001/exports?shuttleId={correspondingShuttle.First().Id}")).Content
                                              .ReadAsStringAsync());

                    using var httpResponse = await client.PutAsync(
                              $"http://localhost:5001/exports/update" +
                              $"?shuttleId={correspondingShuttle.First().Id}&planetId={planetId}&numberOfRobotsDelta={1}",
                              null);

                    httpResponse.EnsureSuccessStatusCode();
                }
            }
        }
    }
}