public async Task <bool> DeleteRobot(string toSearch, List <Guid> guids) { var robotsToDelete = await _repository.GetAllRobots( new RobotFilter { ToSearch = toSearch, Ids = guids, PerfectMatch = true }); if (!robotsToDelete.Any()) { throw new CrewApiException { ExceptionMessage = "Robot is not in the repository.", Severity = ExceptionSeverity.Error, Type = ExceptionType.ServiceException } } ; var result = true; foreach (var robot in robotsToDelete) { var correspondingTeam = await _teamOfExplorersRepository.GetAllTeamsOfExplorers( new TeamOfExplorersFilter { Ids = new List <Guid> { robot.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 = 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={Guid.Parse(planetId.Substring(1,planetId.Length-2))}&numberOfRobotsDelta={-1}", null); httpResponse.EnsureSuccessStatusCode(); } } result = result && await _repository.DeleteRobot(robot.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 } } ; } } }
public async Task <bool> CreateTeamOfExplorers(TeamOfExplorers inputTeamOfExplorers) { _teamOfExplorersValidator.Validate(inputTeamOfExplorers); var sameIdShuttles = await _shuttleRepository.GetAllShuttles( new ShuttleFilter { Ids = new List <Guid> { inputTeamOfExplorers.ShuttleId } }); if (sameIdShuttles.Count != 1) { throw new CrewApiException { ExceptionMessage = $"TeamOfExplorers's Shuttle id {inputTeamOfExplorers.ShuttleId} does not correspond to any shuttle !", Severity = ExceptionSeverity.Error, Type = ExceptionType.ServiceException } } ; var sameNameTeamsOfExplorers = await _repository.GetAllTeamsOfExplorers( new TeamOfExplorersFilter { ToSearch = inputTeamOfExplorers.Name, PerfectMatch = true }); if (sameNameTeamsOfExplorers.Any()) { throw new CrewApiException { ExceptionMessage = $"TeamOfExplorers name {inputTeamOfExplorers.Name} is not unique !", Severity = ExceptionSeverity.Error, Type = ExceptionType.ServiceException } } ; var sameIdTeamsOfExplorers = await _repository.GetAllTeamsOfExplorers( new TeamOfExplorersFilter { Ids = new List <Guid>() { inputTeamOfExplorers.Id } }); if (sameIdTeamsOfExplorers.Any()) { throw new CrewApiException { ExceptionMessage = $"TeamOfExplorers id {inputTeamOfExplorers.Id} already exists in the repository !", Severity = ExceptionSeverity.Error, Type = ExceptionType.ServiceException } } ; var sameShuttleIdTeamsOfExplorers = await _repository.GetAllTeamsOfExplorers( new TeamOfExplorersFilter { ShuttleGuid = sameIdShuttles.First().Id }); if (sameShuttleIdTeamsOfExplorers.Any()) { throw new CrewApiException { ExceptionMessage = $"TeamOfExplorers's Shuttle id {inputTeamOfExplorers.ShuttleId} has already been assigned to another team !", Severity = ExceptionSeverity.Error, Type = ExceptionType.ServiceException } } ; return(await _repository.CreateTeamOfExplorers(inputTeamOfExplorers)); }
public async Task <List <Shuttle> > GetAllShuttles(string toSearch, List <Guid> guids, int pagination = 50, int skip = 0) { return(await _repository.GetAllShuttles( new ShuttleFilter { ToSearch = toSearch, Ids = guids }, pagination, skip)); }