/// <summary> /// Adds a new product to the database /// </summary> /// <param name="product"></param> /// <returns></returns> public ServiceResponse <Data.models.Product> CreateProduct(Data.models.Product product) { try { _db.Products.Add(product); var newInventory = new ProductInventory { Product = product, QuantityOnHand = 0, IdealQuantity = 10 }; _db.ProductInventories.Add(newInventory); _db.SaveChanges(); // execute changes here return(new ServiceResponse <Data.models.Product> { Data = product, Time = DateTime.UtcNow, Message = "Saved new product", IsSuccess = true }); } catch (Exception e) { return(new ServiceResponse <Data.models.Product> { Data = product, Time = DateTime.UtcNow, Message = e.StackTrace, IsSuccess = false }); } }
/// <summary> /// Maps a Product data model to a ProductModel view model /// </summary> /// <param name="product"></param> /// <returns></returns> public static ProductModel SerializeProductModel(Data.models.Product product) { return(new ProductModel { Id = product.Id, CreatedOn = product.CreatedOn, UpdatedOn = product.UpdatedOn, Name = product.Name, Description = product.Description, Price = product.Price, IsTaxable = product.IsTaxable, IsArchived = product.IsArchived }); }