/// <summary>Updates a product.</summary> /// <param name="product">The product to update.</param> /// <returns>The updated product.</returns> public Product Update(Product product) { //TODO: Check arguments if (product == null) { throw new ArgumentNullException(nameof(product)); } if (product.Id <= 0) { throw new ArgumentOutOfRangeException(nameof(product), "Id must be greater than zero"); } //TODO: Validate product var validationResults = new ObjectValidator().TryValidateFullobject(product); if (validationResults.Count() > 0) { var builder = new System.Text.StringBuilder(); foreach (var result in validationResults) { builder.AppendLine(result.ErrorMessage); } ; //Show error message throw new Exception(builder.ToString()); // return null; } ; //Get existing product var existing = GetCore(product.Id); var products = GetAll(); if (products != null) { foreach (var product2 in products) { if (String.Compare(product.Name, product2.Name, true) == 0) { if (!(String.Compare(existing.Name, product.Name, true) == 0)) { throw new InvalidOperationException(" Product name must be unique"); } } } } ; return(UpdateCore(existing, product)); }
/// <summary>Adds a product.</summary> /// <param name="product">The product to add.</param> /// <returns>The added product.</returns> public Product Add(Product product) { //TODO: Check arguments if (product == null) { throw new ArgumentNullException(nameof(product)); } var validationResults = new ObjectValidator().TryValidateFullobject(product); if (validationResults.Count() > 0) { var builder = new System.Text.StringBuilder(); foreach (var result in validationResults) { builder.AppendLine(result.ErrorMessage); } ; // Show error message throw new Exception(builder.ToString()); // return null; } ; var existing = GetAll(); if (existing != null) { foreach (var product2 in existing) { if (String.Compare(product.Name, product2.Name, true) == 0) { throw new InvalidOperationException(" Product name must be unique"); } } } ; //Emulate database by storing copy try { return(AddCore(product)); } catch (Exception e) { //Throwing a new exception throw new InvalidOperationException("Add Failed", e); }; }