Exemplo n.º 1
0
        public async Task <IActionResult> Post(
            [
                FromBody,
                Bind
                (
                    nameof(ProviderCreateModel.Name),
                    nameof(ProviderCreateModel.BaseApiUrl)
                )
            ] ProviderCreateModel providerCreateModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var provider = new Provider(Guid.NewGuid(), providerCreateModel.Name, providerCreateModel.BaseApiUrl);

            scvContext.Add(provider);

            await scvContext.SaveChangesAsync();

            providerCreateModel.Id = provider.Id;

            return(Created($"providers/{providerCreateModel.Id}", providerCreateModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] AddToCartModel addToCartModel)
        {
            var order = await scvContext.Orders
                        .Include(o => o.OrderItems)
                        .ThenInclude(o => o.Product)
                        .Where(o => o.UserId == addToCartModel.UserId && o.OrderStatus == OrderStatus.Open)
                        .Cacheable()
                        .SingleOrDefaultAsync();

            if (order == null)
            {
                order = new Order
                        (
                    Guid.NewGuid(),
                    OrderStatus.Open,
                    addToCartModel.UserId
                        );
                scvContext.Orders.Add(order);
            }

            var product = await scvContext.Products
                          .Where(p => p.Id == addToCartModel.ProductId)
                          .Cacheable()
                          .SingleAsync();

            var orderItem = new OrderItem(Guid.NewGuid(), 1, product.SellPrice, order.Id, product.Id);

            scvContext.OrderItems.Add(orderItem);

            await scvContext.SaveChangesAsync();

            var cartItemModel = new CartItemModel
            {
                Id          = orderItem.Id,
                ProductName = product.Name,
                Quantity    = orderItem.Quantity,
                Price       = orderItem.Price
            };

            return(Ok(cartItemModel));
        }
Exemplo n.º 3
0
        public async Task SellProductsAsync(Order order)
        {
            foreach (var orderItem in order.OrderItems)
            {
                var product = await scvContext.Products
                              .Where(p => p.Id == orderItem.ProductId)
                              .Cacheable()
                              .SingleAsync();

                product.Quantity -= orderItem.Quantity;

                scvContext.Products.Update(product);

                await scvContext.SaveChangesAsync();
            }
        }