private async Task <StoreStruct> CreatStoreStruct(StoresEntity storeEntity) { StoreStruct storeStruct = new StoreStruct { Name = storeEntity.Name, Address = storeEntity.Address, WorkingHours = storeEntity.WorkingHours, Id = storeEntity.Id }; if (storeStruct.Products == null) { storeStruct.Products = new List <ProductStruct>(); } if (storeEntity.StoreProduct != null) { foreach (var product in storeEntity.StoreProduct) { var productEntity = await context.Products.SingleOrDefaultAsync(p => p.Id == product.ProductId); ProductStruct productStruct = new ProductStruct { Id = productEntity.Id, Name = productEntity.Name, Description = productEntity.Description }; storeStruct.Products.Add(productStruct); } } return(storeStruct); }
public async Task <bool> AddProductToStoreAsync(string storeName, string productName) { StoresEntity stores = await context.Stores.FirstOrDefaultAsync(s => s.Name == storeName); if (stores == null) { throw new NoExistInDbException(ExceptionMessages.CannotAddProductToStoreSTORE()); } ProductsEntity products = await context.Products.FirstOrDefaultAsync(p => p.Name == productName); if (products == null) { throw new NoExistInDbException(ExceptionMessages.CannotAddProductToStorePRODUCT()); } stores.StoreProduct = new List <ProductsAndStoresEntity>(); stores.StoreProduct.Add(new ProductsAndStoresEntity { ProductId = products.Id }); try { return((await context.SaveChangesAsync()) > 0); } catch (DbUpdateException dbe) { throw new ExistsInDBException(ExceptionMessages.CannotAddProductToStore(), dbe); } }
public async Task AddProductToStoreAsyncTestOk2Async(string databasename, string description, string name, string address, string name1) { using (InMemoryContext context = new InMemoryContext(databasename)) { Manager manager = new Manager(context); StoreManager storeManager = new StoreManager(context); await context.Stores.AddAsync(new StoresEntity { Name = name, Address = address }); await context.Products.AddAsync(new ProductsEntity { Name = name }); context.SaveChanges(); StoresEntity stores = await context.Stores.SingleOrDefaultAsync(s => s.Name == name); await manager.AddProductToStoreAsync(name, name); IEnumerable <StoreStruct> storeStructs = await storeManager.GetAllAsync(); foreach (var item in storeStructs) { foreach (var product in item.Products) { Assert.AreEqual(product.Name, name); } } } }
public async Task <StoreStruct> GetOneAsync(string name) { StoresEntity storesEntity = await context.Stores.Include(c => c.StoreProduct).SingleOrDefaultAsync(s => s.Name == name); if (storesEntity == null) { throw new NoExistInDbException(ExceptionMessages.CannotFindStore()); } return(await CreatStoreStruct(storesEntity)); }
public async Task <bool> RemoveProductFromStoreAsync(string storeName, string productName) { StoresEntity stores = await context.Stores.Include(c => c.StoreProduct).FirstOrDefaultAsync(s => s.Name == storeName); if (stores == null) { throw new NoExistInDbException(ExceptionMessages.CannotRemoveProductFromStoreSTORE()); } ProductsEntity products = await context.Products.Include(c => c.ProductStore).FirstOrDefaultAsync(p => p.Name == productName); if (products == null) { throw new NoExistInDbException(ExceptionMessages.CannotRemoveProductFromStoreSTORE()); } ProductsAndStoresEntity ps = stores.StoreProduct.Single(s => s.ProductId == products.Id && s.StoreId == stores.Id); stores.StoreProduct.Remove(ps); context.Stores.Update(stores); return((await context.SaveChangesAsync()) > 0); }