Ejemplo n.º 1
0
        public async Task <IActionResult> CreateArea([FromBody] AreaDetailsDto areaResource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var state = await _stateRepo.GetState(areaResource.StateId);

                if (state == null)
                {
                    return(NotFound());
                }
                var city = await _cityRepo.GetCity(areaResource.CityId);

                if (_areaRepo.CheckAreaCode(areaResource.AreaCode))
                {
                    ModelState.AddModelError("Error", "The Area with this Code already exists.");
                    return(BadRequest(ModelState));
                }

                var area = _mapper.Map <AreaDetailsDto, Area>(areaResource);
                area.Deleted   = false;
                area.Protected = false;

                state.Areas.Add(area);
                if (city != null)
                {
                    city.Areas.Add(area);
                }
                await _unitOfWork.CompleteAsync();

                area = await _areaRepo.GetArea(area.Id);

                var result = _mapper.Map <Area, AreaDetailsDto>(area);
                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing the operation.");
                return(BadRequest(ModelState));
            }
        }