public IActionResult CreateCity([FromBody] CityDetailsDto cityDTO) { try { var entityMayor = _mapper.Map <Mayor>(cityDTO); _mayorInfoRepository.CreateCity(entityMayor); _mayorInfoRepository.Save(); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IActionResult UpdateCity(int mayorID, [FromBody] CityDetailsDto cityDTO) { try { var entityCity = _mayorInfoRepository.GetCity(mayorID, false); if (entityCity == null) { return(NotFound()); } _mapper.Map(cityDTO, entityCity); _mayorInfoRepository.UpdateCity(mayorID, entityCity); _mayorInfoRepository.Save(); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> CreateCity([FromBody] CityDetailsDto cityResource) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var state = await _stateRepo.GetState(cityResource.StateId); if (state == null) { return(NotFound()); } if (_cityRepo.CheckCityCode(cityResource.CityCode)) { ModelState.AddModelError("Error", "The City with this Code already exists."); return(BadRequest(ModelState)); } var city = _mapper.Map <CityDetailsDto, City>(cityResource); city.Deleted = false; city.Protected = false; state.Cities.Add(city); await _unitOfWork.CompleteAsync(); city = await _cityRepo.GetCity(city.Id); var result = _mapper.Map <City, CityDetailsDto>(city); return(Ok(result)); } catch (Exception ex) { ModelState.AddModelError("Error", "An error occured while performing this operation."); return(BadRequest(ModelState)); } }
public async Task <IActionResult> EditCity([FromRoute] Guid id, [FromBody] CityDetailsDto cityResource) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = await _cityRepo.GetCity(id); if (city == null) { return(NotFound()); } if (city.Protected == true) { ModelState.AddModelError("Error", "This record is a protected record. It can not be edited"); return(BadRequest(ModelState)); } cityResource.CityCode = city.Code; _mapper.Map <CityDetailsDto, City>(cityResource, city); await _unitOfWork.CompleteAsync(); city = await _cityRepo.GetCity(id); var result = _mapper.Map <City, CityDetailsDto>(city); return(Ok(result)); } catch (Exception ex) { ModelState.AddModelError("Error", "An error occured while performing this operation."); return(BadRequest(ModelState)); } }