public async Task <IActionResult> PutProduct(long id, ProductDTOCreateUpdate productDTOCreateUpdate) { // Fetch product by the given ID, returns null or a product object var product = await GetActiveProduct(id); // If the product is not found, return a 404 if (product == null) { return(NotFound()); } // Update product values product.Name = productDTOCreateUpdate.Name; product.Price = productDTOCreateUpdate.Price; product.Quantity = productDTOCreateUpdate.Quantity; product.DateModified = DateTime.UtcNow; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(NoContent()); }
public async Task <ActionResult <Product> > PostProduct(ProductDTOCreateUpdate productDTOCreateUpdate) { // Create a new product based on the given product DTO var product = new Product { Id = DotShopApi.Utils.GenerateRandomId(), Name = productDTOCreateUpdate.Name, Quantity = productDTOCreateUpdate.Quantity, Price = productDTOCreateUpdate.Price, DateCreated = DateTime.UtcNow, DateModified = DateTime.UtcNow, Deleted = false }; // Persit and save in the database _context.Products.Add(product); await _context.SaveChangesAsync(); // Return created product return(CreatedAtAction("GetProduct", new { id = product.Id }, product)); }