public ApiResult <Product> AddProduct(AddProductOptions options) { var result = new ApiResult <Product>(); if (options == null) { result.ErrorCode = StatusCode.BadRequest; result.ErrorText = "null options"; return(result); } if (string.IsNullOrWhiteSpace(options.Name)) { result.ErrorCode = StatusCode.BadRequest; result.ErrorText = "null or empty name"; return(result); } if (options.Price <= 0) { result.ErrorCode = StatusCode.BadRequest; result.ErrorText = "negative or zero price"; return(result); } if (options.ProductCategory == ProductCategory.Invalid) { result.ErrorCode = StatusCode.BadRequest; result.ErrorText = "invalid category "; return(result); } var product = new Product() { Name = options.Name, Price = options.Price, Category = options.ProductCategory }; context.Add(product); context.SaveChanges(); result.ErrorCode = StatusCode.Success; result.Data = product; return(result); }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <returns></returns> public bool AddProduct(AddProductOptions options) { if (options == null) { return(false); } var product = GetProductById(options.Id); if (product != null) { return(false); } if (string.IsNullOrWhiteSpace(options.Name)) { return(false); } if (options.Price <= 0) { return(false); } if (options.ProductCategory == ProductCategory.Invalid) { return(false); } product = new Product() { Id = options.Id, Name = options.Name, Price = options.Price, Category = options.ProductCategory }; product.Id = options.Id; product.Name = options.Name; product.Price = options.Price; product.Category = options.ProductCategory; ProductsList.Add(product); return(true); }
public void GetProductById_Success() { var product = new AddProductOptions() { Name = "product name", Price = 1230M, Category = ProductCategory.Cameras, Id = $"Test{CodeGenerator.CreateRandom()}" }; Assert.True(products_.AddProduct(product)); var retrivalProduct = products_.GetProductById(product.Id); Assert.NotNull(retrivalProduct); Assert.Equal(product.Price, retrivalProduct.Price); }
public bool AddProduct(AddProductOptions options) { if (options == null) { return(false); } if (string.IsNullOrWhiteSpace(options.Id)) { return(false); } var product = GetProductById(options.Id); if (product != null) { return(false); } if (GetProductById(options.Id) != null) { return(false); } if (string.IsNullOrWhiteSpace(options.Name)) { return(false); } if (options.Price <= 0) { return(false); } if (options.ProductCategory == Model.ProductCategory.Invalid) { return(false); } var prod1 = new Product(); prod1.Id = options.Id; prod1.Name = options.Name; prod1.Price = options.Price; prod1.Category = options.ProductCategory; ProductList.Add(prod1); return(true); }
public void AddProduct_Success() { var product = new AddProductOptions() { Id = $"Test_{DateTime.UtcNow.Millisecond}", Name = "Camer Test", Price = 69M, Category = ProductCategory.Cameras }; Assert.True(products_.AddProduct(product)); var p = products_.GetProductById(product.Id); Assert.NotNull(p); Assert.Equal(product.Name, p.Name); Assert.Equal(product.Price, p.Price); Assert.Equal(product.Category, p.Category); }
public void AddProduct_Success() { var options = new AddProductOptions(); options.Id = $"123{DateTime.Now.Millisecond}"; options.Name = "alex"; options.Price = 1.2M; options.ProductCategory = Core.Model.ProductCategory.Computers; var result = psvc_.AddProduct(options); Assert.True(result); var p = psvc_.GetProductById(options.Id); Assert.NotNull(p); Assert.Equal(options.Name, p.Name); Assert.Equal(options.Price, p.Price); Assert.Equal(options.ProductCategory, p.Category); }
public void UpdateProduct_Success() { var product = new AddProductOptions() { Name = "product name", Price = 1230M, Category = ProductCategory.Cameras, Id = $"Test{CodeGenerator.CreateRandom()}" }; Assert.True(products_.AddProduct(product)); var updateproduct = new UpdateProductOptions() { Category = ProductCategory.Televisions, Description = $"lalilulelo{CodeGenerator.CreateRandom()}", Discount = 45M, Name = $"New Name {CodeGenerator.CreateRandom()}", Price = 45M }; Assert.True(products_.UpdateProduct(product.Id, updateproduct)); }
public ApiResult <Product> AddProduct(AddProductOptions options) { return(prodServ.AddProduct(options)); }