public RepositoryActionResult<ProductPrice> AddProductPrice(ProductPrice newProductPrice) { try { newProductPrice.Wholesaler=_ctx.Wholesalers.Find(newProductPrice.WholesalerId); //Adds new product price _ctx.ProductPrices.Add(newProductPrice); //Saves Changes var result = _ctx.SaveChanges(); //Returns Product Price & Status if (result > 0) { return new RepositoryActionResult<ProductPrice>(newProductPrice, RepositoryActionStatus.Created); } else { return new RepositoryActionResult<ProductPrice>(newProductPrice, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<ProductPrice>(null, RepositoryActionStatus.Error, ex); } }
public DTO.ProductPrice CreateProductPrice(ProductPrice productPrice) { return new DTO.ProductPrice() { Id = productPrice.Id, Created = productPrice.Created, Modified = productPrice.Modified, Price = productPrice.Price, ProductId = productPrice.ProductId, WholesalerId = productPrice.WholesalerId, Wholesaler = productPrice.Wholesaler == null ? new DTO.Wholesaler() : wholesalerFactory.CreateWholesaler(productPrice.Wholesaler) }; }
public RepositoryActionResult<ProductPrice> UpdateProductPrice(ProductPrice updatedProductPrice) { try { // Only update when product price already exists var existingProductPrice = _ctx.ProductPrices.FirstOrDefault(b => b.Id == updatedProductPrice.Id); if (existingProductPrice == null) { return new RepositoryActionResult<ProductPrice>(updatedProductPrice, RepositoryActionStatus.NotFound); } // Change the original entity status to detached; otherwise, we get an error on attach // as the entity is already in the dbSet // set original entity state to detached _ctx.Entry(existingProductPrice).State = EntityState.Detached; // attach & save _ctx.ProductPrices.Attach(updatedProductPrice); // set the updated entity state to modified, so it gets updated. _ctx.Entry(updatedProductPrice).State = EntityState.Modified; // Save Changes var result = _ctx.SaveChanges(); //Returns Entity & Status if (result > 0) { return new RepositoryActionResult<ProductPrice>(updatedProductPrice, RepositoryActionStatus.Updated); } else { return new RepositoryActionResult<ProductPrice>(updatedProductPrice, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<ProductPrice>(null, RepositoryActionStatus.Error, ex); } }