private async Task <ProductType> GetProductTypeByName(ProductsServiceModel productsServiceModel) { var productTypeNameFromDb = await this.dbContext.ProductTypes .FirstOrDefaultAsync(productType => productType.Name == productsServiceModel.ProductType.Name); if (productTypeNameFromDb == null) { throw new ArgumentNullException(nameof(productTypeNameFromDb)); } return(productTypeNameFromDb); }
public async Task <int> CreateProductAsync(ProductsServiceModel productsServiceModel) { var productTypesNameFromDb = await this.GetProductTypeByName(productsServiceModel); var product = AutoMapperConfig.MapperInstance.Map <Product>(productsServiceModel); product.ProductType = productTypesNameFromDb; await this.dbContext.Products.AddAsync(product); await this.dbContext.SaveChangesAsync(); return(product.Id); }
public async Task <int> EditAsync(int id, ProductsServiceModel productsServiceModel) { var productTypeNameFromDb = await this.GetProductTypeByName(productsServiceModel); var product = await this.GetProductById(id); product.Name = productsServiceModel.Name; product.Picture = productsServiceModel.Picture; product.Description = productsServiceModel.Description; product.ProductType = productTypeNameFromDb; this.dbContext.Products.Update(product); await this.dbContext.SaveChangesAsync(); return(product.Id); }
public async Task Create_WithNonExistentProductType_ShouldThrowArgumentNullException() { var dbContext = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedData(dbContext); this.productsService = new ProductsService(dbContext); var testProductService = new ProductsServiceModel { Name = "cleaning", Description = "cleaning desc", Picture = "default", ProductType = new ProductTypesServiceModel { Name = "nonExistent", }, }; await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.productsService.CreateProductAsync(testProductService)); }
public async Task Create_WithCorrectData_ShouldSuccsessfullyCreate() { var dbContext = ApplicationDbContextInMemoryFactory.InitializeContext(); await this.SeedData(dbContext); this.productsService = new ProductsService(dbContext); var testProductService = new ProductsServiceModel { Name = "cleaning", Description = "cleaning desc", Picture = "default", ProductType = new ProductTypesServiceModel { Name = "cleaning", }, }; var expectedResultId = 3; var actualResultId = await this.productsService.CreateProductAsync(testProductService); Assert.Equal(actualResultId, expectedResultId); }