public async Task GetProductOption_ShouldReturnOneOptionById() { var product = new Repository.Product { Name = "Samsung Note3 Mobile", Description = "testDesc", Price = 356, DeliveryPrice = 13 }; product.Id = await _productRepository.CreateProduct(product); var option = new Repository.ProductOption { Name = "White", Description = "White Samsung Note3 Mobile", ProductId = product.Id }; option.Id = await _productRepository.CreateProductOption(option); var response = await _productController.GetOption(option.Id); ObjectContent content = response.Content as ObjectContent; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(option.Name, ((ProductOption)content.Value).Name); Assert.AreEqual(option.Description, ((ProductOption)content.Value).Description); }
public async Task <Guid> CreateProductOption(ProductOption productOption) { productOption.Id = Guid.NewGuid(); _db.ProductOptions.Add(productOption); await _db.SaveChangesAsync(); return(productOption.Id); }
public async Task UpdateProductOption(ProductOption productOption) { var existedProductOption = _db.ProductOptions.Where(p => p.Id == productOption.Id).FirstOrDefault(); if (existedProductOption != null) { existedProductOption.ProductId = productOption.ProductId; existedProductOption.Name = productOption.Name; existedProductOption.Description = productOption.Description; await _db.SaveChangesAsync(); } }
public async Task CreateProductOption_ShouldReturnNotFoundIfProductNotExisted() { var productId = Guid.NewGuid(); string name = Guid.NewGuid().ToString(); var productOption = new Repository.ProductOption { ProductId = productId, Name = name, Description = "DESC", }; var response = await _productController.CreateOption(productId, productOption); ObjectContent content = response.Content as ObjectContent; Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); Assert.AreEqual("Product not existed in DB", content.Value.ToString()); }
public async Task <HttpResponseMessage> UpdateOption(Guid id, Repository.ProductOption option) { option.Id = id; try { var existedProductOption = await _productRepository.GetProductOption(id); if (existedProductOption == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } await _productRepository.UpdateProductOption(option); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public async Task <HttpResponseMessage> CreateOption(Guid productId, Repository.ProductOption option) { try { var product = await _productRepository.GetProductById(productId); if (product == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, "Product not existed in DB")); } option.ProductId = productId; var newId = await _productRepository.CreateProductOption(option); return(Request.CreateResponse(HttpStatusCode.OK, newId)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public async Task DeleteProductOption_ShouldRemoveOptionFromDb() { ProductOption testProductOption; var product = new Repository.Product { Name = "testName", Description = "testDesc", Price = 356, DeliveryPrice = 13 }; product.Id = await _productRepository.CreateProduct(product); var productOption = new Repository.ProductOption { ProductId = product.Id, Name = "testOption", Description = "desc" }; productOption.Id = await _productRepository.CreateProductOption(productOption); testProductOption = productOption; var beforeDeletingResponse = await _productController.GetOption(testProductOption.Id); ObjectContent content = beforeDeletingResponse.Content as ObjectContent; Assert.IsNotNull((ProductOption)content.Value); var response = await _productController.DeleteOption(testProductOption.Id); var responseGet = await _productController.GetOption(testProductOption.Id); content = responseGet.Content as ObjectContent; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNull((ProductOption)content.Value); }
public async Task UpdateProductOption_ShouldChangeOptionIntoDb() { ProductOption testProductOption; var product = new Repository.Product { Name = "testName", Description = "testDesc", Price = 356, DeliveryPrice = 13 }; product.Id = await _productRepository.CreateProduct(product); var productOption = new Repository.ProductOption { ProductId = product.Id, Name = "testOption", Description = "desc" }; productOption.Id = await _productRepository.CreateProductOption(productOption); testProductOption = productOption; testProductOption.Name = Guid.NewGuid().ToString(); var response = await _productController.UpdateOption(productOption.Id, testProductOption); var responseGet = await _productController.GetOption(testProductOption.Id); ObjectContent content = responseGet.Content as ObjectContent; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(testProductOption.Name, ((ProductOption)content.Value).Name); Assert.AreEqual(testProductOption.Description, ((ProductOption)content.Value).Description); }
public async Task CreateProductOption_ShouldAddProductOptionIntoDb() { var products = await _productRepository.GetAllProducts(); var testProduct = products[0]; string name = Guid.NewGuid().ToString(); var productOption = new Repository.ProductOption { ProductId = testProduct.Id, Name = name, Description = "DESC", }; var response = await _productController.CreateOption(testProduct.Id, productOption); ObjectContent content = response.Content as ObjectContent; var responseGet = await _productController.GetOption((Guid)content.Value); content = responseGet.Content as ObjectContent; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(name, ((ProductOption)content.Value).Name); }