public async Task <IHttpActionResult> PutGoodAsync(int id, [FromBody] GoodApiVM good) { Good current = await _goodsRepo.GetAsync(id); if (current == null) { return(NotFound()); } try { if (good.CategoryId > 0) { current.CategoryId = good.CategoryId; } if (good.ManufacturerId > 0) { current.ManufacturerId = good.ManufacturerId; } if (!string.IsNullOrEmpty(good.GoodName)) { current.GoodName = good.GoodName; } if (good.GoodCount != default) { current.GoodCount = good.GoodCount; } if (good.Price != default) { current.Price = good.Price; } if (good.Photos.Count > 0) { Parallel.ForEach(good.Photos, (p) => _photosRepo.CreateOrUpdate(new Photo() { GoodId = current.GoodId, PhotoPath = p })); } await _goodsRepo.CreateOrUpdateAsync(current); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok(new { UpdatedGood = new GoodApiVM(current), Message = "Good successfully updated.", StatusCode = 200 })); }
public async Task <IHttpActionResult> PostGoodAsync([FromBody] GoodApiVM good) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newGood = new Good() { GoodName = good.GoodName, GoodCount = good.GoodCount, Price = good.Price, }; if (good.CategoryId > 0) { newGood.CategoryId = good.CategoryId; } if (good.ManufacturerId > 0) { newGood.ManufacturerId = good.ManufacturerId; } var added = await _goodsRepo.AddAsync(newGood); if (good.Photos.Count > 0) { Parallel.ForEach(good.Photos, (p) => _photosRepo.CreateOrUpdate(new Photo() { GoodId = added.GoodId, PhotoPath = p })); } return(CreatedAtRoute("API Default", new { id = added.GoodId }, new GoodApiVM(added))); }