Esempio n. 1
0
        public async Task <IEnumerable <OrderViewModel> > Checkout(OrderDto model, CancellationToken cancellationToken = default)
        {
            AppUser user = await _authService.GetUser(_user, cancellationToken);

            IQueryable <CartItem> cart = _context.CartItems
                                         .Include(ci => ci.Product)
                                         .ThenInclude(p => p.Images)
                                         .Where(ci => ci.UserId == user.UserId);

            IEnumerable <Order> orders = await cart
                                         .ProjectTo <Order>(_mapper.ConfigurationProvider)
                                         .ToListAsync(cancellationToken);

            PaymentMethod paymentMethod = await _context.PaymentMethods
                                          .SingleOrDefaultAsync(pm => pm.PaymentMethodId == model.PaymentMethodId, cancellationToken);

            foreach (Order order in orders)
            {
                order.PaymentMethodId = paymentMethod.PaymentMethodId;
            }

            await _context.Orders.AddRangeAsync(orders, cancellationToken);

            _context.RemoveRange(cart);

            await _context.SaveChangesAsync(cancellationToken);

            IEnumerable <OrderViewModel> viewModels = orders
                                                      .AsQueryable()
                                                      .ProjectTo <OrderViewModel>(_mapper.ConfigurationProvider)
                                                      .ToList();

            return(viewModels);
        }
Esempio n. 2
0
 public Task DeleteProductsAsync(ICollection <CartProduct> cartProducts)
 {
     _dbContext.RemoveRange(cartProducts);
     return(_dbContext.SaveChangesAsync());
 }