public async Task <ProblemWithBasket> AddProductsToCatalogFromBasket(IAddProductsToBasket items) { //// Getting basket from Db //var basketFromDb = await GetBasketById(items.BasketId); //var catalogFromBasket = basketFromDb.Catalogs.FirstOrDefault(x => x.Name.Name == items.Name); //var catalogInWarehouse = await DB.CatalogDB.FirstOrDefaultAsync(x => x.Id == items.CatalogId); //var productsFromDb = await DB.ProductDB.Where(x => x.CatalogId == catalogFromBasket.Id).ToListAsync(); //var productList = CheckForNeededAmountOfProducts(productsFromDb, items.ProductAmount); //if (productsFromDb != null) //{ // // Adding all products to catalogs in basket // await AddProductsToCatalogsFromBasket(basketFromDb, catalogFromBasket, catalogInWarehouse, productList); // return ProblemWithBasket.AllIsOkey; //} //else //{ // return ProblemWithBasket.NotEnoughProductsInCatalog; //} var basketFromDb = await GetBasketById(items.BasketId); var catalogFromBasket = basketFromDb.Catalogs.FirstOrDefault(x => x.Name.Name == items.Name); var catalogInWarehouse = await DB.CatalogDB.FirstOrDefaultAsync(x => x.Id == items.CatalogId); if (catalogFromBasket.CurrentAmount < items.ProductAmount) { return(ProblemWithBasket.NotEnoughProductsInCatalog); } else { catalogFromBasket.CurrentAmount -= items.ProductAmount; catalogInWarehouse.CurrentAmount += items.ProductAmount; var warehouse = await DB.WarehouseDB.FirstOrDefaultAsync(x => x.Id == catalogInWarehouse.WarehouseId); if (catalogInWarehouse.CurrentAmount <= catalogInWarehouse.MinimumAmount) { warehouse.HasMinCatalogs = true; } else { warehouse.HasMinCatalogs = false; } if (catalogFromBasket.CurrentAmount == 0) { DB.CatalogDB.Remove(catalogFromBasket); } else { DB.CatalogDB.Update(catalogFromBasket); } DB.CatalogDB.Update(catalogInWarehouse); await DB.SaveChangesAsync(); return(ProblemWithBasket.AllIsOkey); } }
public async Task <ProblemWithBasket> RemoveProductsFromCatalogManually(IAddProductsToBasket items) { var getCatalog = await GetCatatolById(items.CatalogId); if (getCatalog.CurrentAmount >= items.ProductAmount) { //var productsToRemove = CheckForNeededAmountOfProducts(productListFromCatalog, items.ProductAmount); // Adding all products to catalogs in basket await RemoveProductsManually(items.ProductAmount, getCatalog); return(ProblemWithBasket.AllIsOkey); } else { return(ProblemWithBasket.NotEnoughProductsInCatalog); } }
public async Task <ProblemWithBasket> AddProductsToCatalogManually(IAddProductsToBasket items) { // Getting basket from Db var getCatalog = await GetCatatolById(items.CatalogId); if (getCatalog != null) { // Adding all products to catalogs in basket await AddProductsManually(getCatalog, items.ProductAmount); return(ProblemWithBasket.AllIsOkey); } else { return(ProblemWithBasket.NotEnoughProductsInCatalog); } }
public async Task <ActionResult> AddProductsManually([FromBody] IAddProductsToBasket items) { items.ProductAmount = Math.Abs(items.ProductAmount); var username = User.Claims.FirstOrDefault(x => x.Type == "FullName").Value; var situation = await CatalogService.AddProductsToCatalogManually(items); if (situation == ProblemWithBasket.AllIsOkey) { var catalogFromDB = await DB.CatalogDB.Include(x => x.Warehouse).FirstOrDefaultAsync(x => x.Id == items.CatalogId); var catalogVM = new CatalogVM { Id = catalogFromDB.Id, ProductPrice = catalogFromDB.ProductPrice, CurrentAmount = catalogFromDB.CurrentAmount, MaximumAmount = catalogFromDB.MaximumAmount, MinimumAmount = catalogFromDB.MinimumAmount, WarehouseId = catalogFromDB.WarehouseId, Name = catalogFromDB.Name.Name, }; var log = new SimpleLogTable() { Date = DateTime.Now, UserName = username, Action = "Pievienots", What = items.Name, Amount = items.ProductAmount, Manually = "Manuāli", WarehouseId = catalogFromDB.WarehouseId.Value, Where = catalogFromDB.Warehouse.Name }; await SimpleLogTableService.AddLog(log); return(Ok(catalogVM)); } else { return(BadRequest(new { message = "Something went wrong." })); } }