public InfrastructureLayer.Models.Product Update(InfrastructureLayer.Models.Product item) { var _ctx = Ctx; if (item.Id == null) { throw new NullReferenceException("Product Id can not be null"); } if (item.Category?.Id == null) { throw new NullReferenceException("Product Category can not be null"); } Product tobeUpdate = _ctx.Products.Find(item.Id); if (tobeUpdate == null) { throw new NullReferenceException("Product not found"); } tobeUpdate.Name = item.Name; tobeUpdate.Price = item.Price; tobeUpdate.Count = item.Count; tobeUpdate.ImageUrlRelative = item.ImageUrlRelative; Category category = _ctx.Categories.Find(item.Category.Id); if (category != null) { tobeUpdate.Category = category; } _ctx.SaveChanges(); return(tobeUpdate.ToDto()); }
public static Product ToEF(this InfrastructureLayer.Models.Product @this) { return(new Product { Id = @this.Id ?? default(long), Name = @this.Name, Price = @this.Price, Count = @this.Count, ImageUrlRelative = @this.ImageUrlRelative }); }
public InfrastructureLayer.Models.Product Add(InfrastructureLayer.Models.Product item) { var _ctx = Ctx; if (item.Category?.Id == null) { throw new NullReferenceException("Product Category can not be null"); } Category category = _ctx.Categories.Find(item.Category.Id.Value); if (category == null) { throw new NullReferenceException("Product Category Not Found"); } Product newProduct = item.ToEF(); category.Products.Add(newProduct); _ctx.SaveChanges(); return(newProduct.ToDto()); }