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