示例#1
0
        public async Task <ActionResult <GeolocationDataResource> > Add([FromBody] IpAddress ipAddress)
        {
            try
            {
                if (!_ipAddressValidator.IsValid(ipAddress.Value))
                {
                    return(BadRequest($"Invalid IpAddress format: {ipAddress.Value}."));
                }
                var remoteGeolocationData = await _geolocationDataService.GetByIpAddressAsync(ipAddress.Value);

                if (remoteGeolocationData == null)
                {
                    return(BadRequest($"Not found geolocation data for IpAddress {ipAddress.Value}"));
                }
                var localGeolocationData = await _geolocationService.AddAsync(remoteGeolocationData);

                return(CreatedAtAction(nameof(Get), new { localGeolocationData.IpAddress }, _geolocationDataConverter.Convert(localGeolocationData)));
            }
            catch (RemoteApiException ex)
            {
                return(StatusCode((int)HttpStatusCode.ServiceUnavailable, ex.Message));
            }
            catch (EntityDuplicateException ex)
            {
                return(Conflict(ex.Message));
            }
        }