Ejemplo n.º 1
0
        public async Task <IActionResult> Post(Location location, [FromServices] IOptions <ApiBehaviorOptions> apiBehaviorOptions)
        {
            try
            {
                Guid.Parse(location.Id);
            }
            catch (Exception _)
            {
                ModelState.AddModelError(nameof(Location.Id), "Location Id is not a valid Guid");
                return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext));
            }

            var postcode = location.Physical_Addresses.First().Postal_Code;
            var isValid  = await _postcodeServiceClient.ValidatePostcode(postcode);

            if (!isValid.Result)
            {
                ModelState.AddModelError(nameof(Organisation.Id), "Postcode does not exist.");
                return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext));
            }

            var locationResult = await _postcodeServiceClient.GetPostcodeLocation(postcode);

            location.Latitude  = locationResult.Latitude;
            location.Longitude = locationResult.Longitude;

            _locationSearchServiceClient.AddOrUpdateLocation(location);
            await _locationRepository.InsertOne(location);

            return(Accepted(location));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get([FromServices] IOptions <ApiBehaviorOptions> apiBehaviorOptions, string postcode = null, double?proximity = 5, string text = null)
        {
            var services = _serRepository.GetAll();

            if (postcode != null)
            {
                var isValid = await _postcodeServiceClient.ValidatePostcode(postcode);

                if (!isValid.Result)
                {
                    ModelState.AddModelError(nameof(Organisation.Id), "Postcode does not exist.");
                    return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext));
                }
                var locationResults = await _locationSearchServiceClient.QueryLocations(postcode, proximity);

                var locationIds = locationResults.Select(res => res.id);
                //TODO: We only check for the first serviceAtLocation as we only save one, for now.
                services = services.ToList()
                           .FindAll(service => locationIds.Contains(service.Service_At_Locations.First().Location_Id));
            }

            if (text != null)
            {
                services = services.ToList().FindAll(service => service.Name.Contains(text));
            }

            return(Ok(services));
        }