Esempio n. 1
0
        public async Task <bool> CreatePlanet(Planet inputPlanet)
        {
            _planetValidator.Validate(inputPlanet);

            var sameIdPlanets = await _repository.GetAllPlanets(
                new PlanetFilter { Ids = new List <Guid> {
                                       inputPlanet.Id
                                   } });

            if (EnumerableExtensions.Any(sameIdPlanets))
            {
                throw new PlanetApiException
                      {
                          ExceptionMessage = $"Planet with id {inputPlanet.Id} exists in the repository !",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;

            var sameNamePlanets = await _repository.GetAllPlanets(
                new PlanetFilter { ToSearch = inputPlanet.Name, PerfectMatch = true });

            if (EnumerableExtensions.Any(sameNamePlanets))
            {
                throw new PlanetApiException
                      {
                          ExceptionMessage = $"Planet name {inputPlanet.Name} is not unique !",
                          Severity         = ExceptionSeverity.Error,
                          Type             = ExceptionType.ServiceException
                      }
            }
            ;

            var sameIdSolarSystems = await _solarSystemRepository.GetAllSolarSystems(
                new SolarSystemFilter { Ids = new List <Guid> {
                                            inputPlanet.SolarSystemId
                                        } });

            if (!sameIdSolarSystems.Any())
            {
                throw new PlanetApiException
                      {
                          ExceptionMessage =
                              $"Planet's solar system id {inputPlanet.SolarSystemId} does not correspond to a solar system !",
                          Severity = ExceptionSeverity.Error,
                          Type     = ExceptionType.ServiceException
                      }
            }
            ;

            if (inputPlanet.ShuttleId != Guid.Empty)
            {
                var sameShuttleIdPlanets = await _repository.GetAllPlanets(
                    new PlanetFilter { ShuttleId = inputPlanet.ShuttleId });

                if (sameShuttleIdPlanets.Any())
                {
                    throw new PlanetApiException
                          {
                              ExceptionMessage =
                                  $"Shuttle with id {inputPlanet.ShuttleId} is already visiting another planet !",
                              Severity = ExceptionSeverity.Error,
                              Type     = ExceptionType.ServiceException
                          }
                }
                ;

                var client = _clientFactory.CreateClient();
                using var httpResponse =
                          await client.GetAsync($"http://localhost:5000/exports?shuttleId={inputPlanet.ShuttleId}");

                httpResponse.EnsureSuccessStatusCode();
                var samePlanetShuttlesCount = int.Parse(await httpResponse.Content.ReadAsStringAsync());
                if (samePlanetShuttlesCount != 1)
                {
                    throw new PlanetApiException
                          {
                              ExceptionMessage =
                                  $"Planet's shuttle id {inputPlanet.ShuttleId} does not correspond to an existing shuttle.",
                              Severity = ExceptionSeverity.Error,
                              Type     = ExceptionType.ServiceException
                          }
                }
                ;
            }

            return(await _repository.CreatePlanet(inputPlanet));
        }
 public async Task <List <SolarSystem> > GetAllSolarSystems(string toSearch, List <Guid> guids, int pagination = 50,
                                                            int skip = 0)
 {
     return(await _repository.GetAllSolarSystems(
                new SolarSystemFilter { ToSearch = toSearch, Ids = guids }, pagination, skip));
 }