public void EditGood([FromBody] IGood good) { var savedGoodQuery = db.Good.Where(x => x.Id == good.Id); if (savedGoodQuery.Any()) { var savedGood = savedGoodQuery.First(); savedGood.Name = good.Name; savedGood.Price = good.Price; savedGood.Discount = good.Discount; savedGood.Explanation = good.Explanation; //var categoryIds = good.CategoryIds.ToHashSet(); //savedGood.GoodCategory = categoryIds.Select(categoryId => new GoodCategory { Good = newGood, CategoryId = categoryId }).ToList(); db.SaveChanges(); } }
public void AddGood([FromBody] IGood good) { if (good.Id != 0) { throw new FormatException("Invalid request id is set"); } var newGood = new Good { Name = good.Name, Sku = good.Sku, Quantity = good.Quantity, Explanation = good.Explanation, Discount = good.Discount, Price = good.Price }; //var categoryIds = good.CategoryIds.ToHashSet(); //db.GoodCategory.AddRange(categoryIds.Select(categoryId => new GoodCategory { Good = newGood, CategoryId = categoryId })); db.Good.Add(newGood); db.SaveChanges(); }