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)); } }
public async Task <IActionResult> EditArea([FromRoute] Guid id, [FromBody] AreaDetailsDto areaResource) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var area = await _areaRepo.GetArea(id); if (area == null) { return(NotFound()); } if (area.Protected == true) { ModelState.AddModelError("Error", "This record is a protected record. It can not be edited"); return(BadRequest(ModelState)); } areaResource.AreaCode = area.Code; _mapper.Map <AreaDetailsDto, Area>(areaResource, area); await _unitOfWork.CompleteAsync(); area = await _areaRepo.GetArea(id); var result = _mapper.Map <Area, AreaDetailsDto>(area); return(Ok(result)); } catch (Exception ex) { ModelState.AddModelError("Error", "An error occured while performing this operation."); return(BadRequest(ModelState)); } }