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