public async Task <IActionResult> Put(int id, [FromBody] CountyAgentViewModel countyAgentVm) { if (countyAgentVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbItem = await _countyAgentRepository.GetSingleAsync(id); if (dbItem == null) { return(NotFound()); } Mapper.Map(countyAgentVm, dbItem); _countyAgentRepository.SetPinyin(dbItem); dbItem.SetModification(UserName); _countyAgentRepository.Update(dbItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } var vm = Mapper.Map <CountyAgentViewModel>(dbItem); return(Ok(vm)); }
public async Task <IActionResult> Post([FromBody] CountyAgentViewModel countyAgentVm) { if (countyAgentVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newItem = Mapper.Map <CountyAgent>(countyAgentVm); _countyAgentRepository.SetPinyin(newItem); newItem.SetCreation(UserName); _countyAgentRepository.Add(newItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } var vm = Mapper.Map <CountyAgentViewModel>(newItem); return(CreatedAtRoute("GetCountyAgent", new { id = vm.Id }, vm)); }