public async void Delete(int id)
        {
            var product = _context.Products.Where(_ => _.Id.Equals(id)).FirstOrDefault();

            _context.Products.Remove(product);
            await _context.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task <bool> CheckoutCart(Cart cart)
        {
            if (cart is null)
            {
                throw new ArgumentNullException(nameof(cart));
            }

            decimal orderId = new Random().Next(1, 1000);
            Orders  order   = new Orders()
            {
                Id          = orderId,
                IdUser      = _context.User.FirstOrDefault(u => u.Username.Equals(cart.Username)).Id,
                Status      = "PROCESSED",
                Total       = cart.Total,
                ProcessDate = DateTime.UtcNow
            };

            _context.Orders.Add(order);
            var wasOrderSaved = await _context.SaveChangesAsync();

            if (wasOrderSaved > 0)
            {
                if (!AddProductsToOrder(orderId, cart.Products))
                {
                    throw new Exception("Error al agregar los productos a la orden");
                }
                var updateResult = await UpdateProducts(cart.Products);

                if (updateResult)
                {
                    throw new Exception("Error al actualizar la cantidad en stock");
                }
            }
            return(true);
        }
        public async void Delete(int id)
        {
            var category = _context.Categories.Where(_ => _.Id.Equals(id)).FirstOrDefault();

            _context.Categories.Remove(category);
            await _context.SaveChangesAsync();
        }
        public async void Post(Review review)
        {
            Reviews newReview = _mapper.Map <Reviews>(review);

            newReview.Id = new Random().Next(1, 10000);
            _context.Reviews.Add(newReview);
            await _context.SaveChangesAsync();
        }