Пример #1
0
        public async Task <IActionResult> Post(ResidenceDto residenceDto)
        {
            try
            {
                var residence = _mapper.Map <Residence>(residenceDto);

                //Validate data integrity in ViaCep Api
                residence = await _repo.GetAddressByViaCepApiAsync(residence);

                if (residence.ZipCode == null)
                {
                    return(NotFound(ApiReturnMessages.ZipCodeNotFound));
                }

                _repo.Add(residence);

                if (await _repo.SaveChangesAsync())
                {
                    return(Created($"/api/Residence/{residence.ResidenceId}", residenceDto));
                }
            }
            catch (System.Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ApiReturnMessages.DbFailed));
            }

            return(BadRequest());
        }
Пример #2
0
        public async Task <IActionResult> Get(string zipCode)
        {
            try
            {
                var results = await _repo.GetAddressByViaCepApiAsync(zipCode);

                if (results.ZipCode == null)
                {
                    return(this.StatusCode(StatusCodes.Status404NotFound, ApiReturnMessages.ZipCodeNotFound));
                }

                return(Ok(results));
            }
            catch (System.Exception ex)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ApiReturnMessages.DbFailed + ex.Message));
            }
        }