public async Task <Result <BasketView> > GetAllDishesByUserIdAsync(string userIdFromIdentity, CancellationToken cancellationToken = default) { UserDB user = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); AppUser userIdentity = await _userManager.FindByIdAsync(userIdFromIdentity); if (user is null || userIdentity is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.USER_WAS_NOT_FOUND)); } BasketDB basket = await _context.Baskets.Where(_ => _.Id == user.BasketId).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (basket is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.BASKET_WAS_NOT_FOUND)); } try { var dishList = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id).AsNoTracking().ToListAsync(cancellationToken); BasketView view = CollectBasket(basket, dishList, userIdentity).Result.Data; return(Result <BasketView> .Ok(_mapper.Map <BasketView>(view))); } catch (ArgumentNullException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }
public async void Basket_AddUpdateDishAsync_Positive_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Basket_AddUpdateDishAsync_Positive_Test") .Options; int count = 10; var storeManager = new Mock <IUserStore <AppUser> >(); storeManager.Setup(x => x.FindByIdAsync(_userData.Id, CancellationToken.None)) .ReturnsAsync(new AppUser() { PersonalDiscount = _userData.PersonalDiscount, }); var mgr = new UserManager <AppUser>(storeManager.Object, null, null, null, null, null, null, null, null); using (var context = new DreamFoodDeliveryContext(options)) { _user.IdFromIdentity = _userData.Id; context.Add(_user); context.AddRange(_dishes); BasketDB basket = new BasketDB() { Id = _user.BasketId, UserId = _user.Id }; context.Baskets.Add(basket); await context.SaveChangesAsync(); } var storeDishService = new Mock <IDishService>(); storeDishService.Setup(x => x.GetByIdAsync(_dishes.FirstOrDefault().Id.ToString(), CancellationToken.None)) .ReturnsAsync(Result <DishView> .Ok(_mapper.Map <DishView>(_dishes.FirstOrDefault()))); using (var context = new DreamFoodDeliveryContext(options)) { var dish = _dishes.FirstOrDefault(); double basketPrice_first = (dish.Price * (100 - dish.Sale) / 100 * count * (100 - _userData.PersonalDiscount) / 100).Value; basketPrice_first = Math.Round(basketPrice_first, 2); var service = new BasketService(_mapper, mgr, context, storeDishService.Object); var resultPositive = await service.AddUpdateDishAsync(dish.Id.ToString(), _userData.Id, count); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.BasketCost.Should().Be(basketPrice_first); } }
public async Task <Result <BasketView> > RemoveDishByIdAsync(string dishId, string userIdFromIdentity, CancellationToken cancellationToken = default) { UserDB user = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); AppUser userIdentity = await _userManager.FindByIdAsync(userIdFromIdentity); if (user is null || userIdentity is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.USER_WAS_NOT_FOUND)); } BasketDB basket = await _context.Baskets.Where(_ => _.UserId == user.Id).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (basket is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.BASKET_WAS_NOT_FOUND)); } BasketDishDB connection = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id && _.DishId == Guid.Parse(dishId)).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (connection is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.DISHES_IN_BASKET_WAS_NOT_FOUND)); } basket.ModificationTime = DateTime.Now; _context.Entry(basket).Property(c => c.ModificationTime).IsModified = true; _context.BasketDishes.Remove(connection); try { await _context.SaveChangesAsync(cancellationToken); var dishList = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id).AsNoTracking().ToListAsync(cancellationToken); BasketView view = CollectBasket(basket, dishList, userIdentity).Result.Data; return(Result <BasketView> .Ok(_mapper.Map <BasketView>(view))); } catch (DbUpdateConcurrencyException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (ArgumentNullException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }
public async Task <Result> RemoveAllByUserIdAsync(string userIdFromIdentity, CancellationToken cancellationToken = default) { UserDB user = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (user is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.USER_WAS_NOT_FOUND)); } BasketDB basket = await _context.Baskets.Where(_ => _.UserId == user.Id).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (basket is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.BASKET_WAS_NOT_FOUND)); } var connections = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id).AsNoTracking().ToListAsync(cancellationToken); if (!connections.Any()) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.DISHES_IN_BASKET_WAS_NOT_FOUND)); } basket.ModificationTime = DateTime.Now; _context.Entry(basket).Property(c => c.ModificationTime).IsModified = true; _context.BasketDishes.RemoveRange(connections); try { await _context.SaveChangesAsync(cancellationToken); BasketView view = _mapper.Map <BasketView>(basket); return(Result <BasketView> .Ok(_mapper.Map <BasketView>(view))); } catch (DbUpdateConcurrencyException ex) { return(await Task.FromResult(Result.Fail(ExceptionConstants.CANNOT_DELETE_DISHES + ex.Message))); } catch (DbUpdateException ex) { return(await Task.FromResult(Result.Fail(ExceptionConstants.CANNOT_DELETE_DISHES + ex.Message))); } }
public async Task <Result <BasketView> > CollectBasket(BasketDB basket, List <BasketDishDB> dishList, AppUser userIdentity) { BasketView view = _mapper.Map <BasketView>(basket); view.Dishes = new HashSet <DishView>(); foreach (var dishListItem in dishList) { var dish = await _dishService.GetByIdAsync(dishListItem.DishId.ToString()); if (dish.IsError) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.UNABLE_TO_RETRIEVE_DATA)); } dish.Data.Quantity = dishListItem.Quantity.GetValueOrDefault(); if (dish.Data.Sale > 0) { view.BasketCost += dish.Data.Price * (1 - dish.Data.Sale / 100) * dish.Data.Quantity; } else { view.BasketCost += dish.Data.Price * dish.Data.Quantity; } view.Dishes.Add(dish.Data); } view.BasketCost *= 1 - userIdentity.PersonalDiscount / 100; view.BasketCost = Math.Round(view.BasketCost, 2); if (view.BasketCost < NumberСonstants.FREE_DELIVERY_BORDER) { view.DeliveryCost = NumberСonstants.DELIVERY_PRICE; } else { view.DeliveryCost = 0; } return(Result <BasketView> .Ok(_mapper.Map <BasketView>(view))); }
public async Task <Result <UserView> > CreateAccountAsyncById(string userIdFromIdentity, CancellationToken cancellationToken = default) { UserGeneration newProfile = new UserGeneration() { IdFromIdentity = userIdFromIdentity }; UserDB userToAdd = _mapper.Map <UserDB>(newProfile); userToAdd.BasketId = Guid.NewGuid(); _context.Users.Add(userToAdd); BasketDB basketToAdd = new BasketDB() { Id = userToAdd.BasketId, UserId = userToAdd.Id }; _context.Baskets.Add(basketToAdd); try { await _context.SaveChangesAsync(cancellationToken); UserView view = GetUserByIdFromIdentityAsync(userToAdd.IdFromIdentity).Result.Data; return(Result <UserView> .Ok(view)); } catch (DbUpdateConcurrencyException ex) { return(Result <UserView> .Fail <UserView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <UserView> .Fail <UserView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (ArgumentNullException ex) { return(Result <UserView> .Fail <UserView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }
public async Task <Result <BasketView> > AddUpdateDishAsync(string dishId, string userIdFromIdentity, int quantity, CancellationToken cancellationToken = default) { DishDB dishToAdd = await _context.Dishes.Where(_ => _.Id == Guid.Parse(dishId)).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (dishToAdd is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.DISH_WAS_NOT_FOUND)); } UserDB user = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); AppUser userIdentity = await _userManager.FindByIdAsync(userIdFromIdentity); if (user is null || userIdentity is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.USER_WAS_NOT_FOUND)); } BasketDB basket = await _context.Baskets.Where(_ => _.UserId == user.Id).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (basket is null) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.BASKET_WAS_NOT_FOUND)); } var connection = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id && _.DishId == Guid.Parse(dishId)).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (connection is null) { BasketDishDB basketDish = new BasketDishDB() { BasketId = basket.Id, DishId = dishToAdd.Id, Quantity = quantity, DishPrice = dishToAdd.Price, Sale = dishToAdd.Sale }; _context.BasketDishes.Add(basketDish); } else { if (quantity > 0) { connection.Quantity = quantity; _context.Entry(connection).Property(c => c.Quantity).IsModified = true; } else // quantity == 0 { _context.BasketDishes.Remove(connection); } } basket.ModificationTime = DateTime.Now; _context.Entry(basket).Property(c => c.ModificationTime).IsModified = true; try { await _context.SaveChangesAsync(cancellationToken); var dishList = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id).AsNoTracking().ToListAsync(cancellationToken); //BasketView view = CollectBasket(basket, dishList, userIdentity).Result.Data; //return Result<BasketView>.Ok(view); return(CollectBasket(basket, dishList, userIdentity).Result); } catch (DbUpdateConcurrencyException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (ArgumentNullException ex) { return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }