public static Product add(Product product) { var existingProduct = false; Product fetchedProduct; if (product.Id != 0) { throw new Exception(@"Posted products cannot contain an id"); } try { fetchedProduct = get(product.Name); existingProduct = true; } catch (Exception) { //this is actually good that we are here. Means we didn't //find an existing product so it is OK to insert; product.Id = ProductsRepository.data.Select(x => x.Id).Max() + 1; var data = ProductsRepository.data.ToList(); data.Add(product); ProductsRepository.data = data; } if (existingProduct) { throw new Exception(@"The product name you are attempting to insert already exists."); } return product; }
public void PutProduct(Product product) { ProductsRepository.update(product); }
public Product PostProduct(Product product) { return ProductsRepository.add(product); }
public static void update(Product product) { try { Product existingProduct = get(product.Id); existingProduct.Name = product.Name; existingProduct.Price = product.Price; existingProduct.Rating = product.Rating; } catch (Exception) { throw new Exception(@"The product name you are attempting to put does not exist."); } }