public async Task <ActionResult <Product> > PostProduct([FromForm] ProductWithCategory productWithCategory) { var product = new Product() { Name = productWithCategory.Name, Price = productWithCategory.Price, Description = productWithCategory.Description, Active = productWithCategory.Active, ImgSrc = String.Format("{0}://{1}{2}/Images/{3}", Request.Scheme, Request.Host, Request.PathBase, productWithCategory.ImgName) }; product.ImgName = await SaveImage(productWithCategory.ImgFile); _context.Products.Add(product); await _context.SaveChangesAsync(); var productCategory = new Productcategory() { Productid = product.Id, Categoryname = productWithCategory.Categoryname }; _context.Productcategories.Add(productCategory); await _context.SaveChangesAsync(); return(CreatedAtAction("GetProduct", new { id = product.Id }, product)); }
public List <ProductWithCategory> GetProducts() { connection.Open(); string query = "Select * from ProductWithCategory order by Name"; command = new SqlCommand(query, connection); SqlDataReader reader = command.ExecuteReader(); List <ProductWithCategory> products = new List <ProductWithCategory>(); while (reader.Read()) { ProductWithCategory aProduct = new ProductWithCategory(); aProduct.Name = reader["Name"].ToString(); aProduct.CategoryName = reader["CategoryName"].ToString(); aProduct.Quantity = Convert.ToInt32(reader["Quantity"]); products.Add(aProduct); } reader.Close(); connection.Close(); return(products); }
public CategorySuggestionResponse(ProductWithCategory productWithCategory) { this.ProductName = productWithCategory.ProductName; this.CategoryId = productWithCategory.CategoryId; }