Exemplo n.º 1
0
        public async Task <IActionResult> Create(CancellationToken cancellationToken = default)
        {
            var quote = new Quote(Guid.NewGuid(), DateTime.UtcNow);

            _dbContext.Quotes.Add(quote);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(CreatedAtRoute("quote-details", new { id = quote.Id }, quote.Id));
        }
Exemplo n.º 2
0
        public async Task <bool> AddImageAsync(Image image)
        {
            if (image == null)
            {
                return(false);
            }
            _db.Images.AddAsync(image);
            await _db.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 3
0
        public async Task <bool> AddCategoryAsync(Category category)
        {
            if (category != null)
            {
                _db.Categories.Add(category);
                await _db.SaveChangesAsync();

                return(true);
            }

            return(false);
        }
Exemplo n.º 4
0
        public async Task <string> CreateOrder(Order order)
        {
            var result = await _context.Orders.AddAsync(order);

            if (result == null)
            {
                return("Basket Item not created");
            }
            await _context.SaveChangesAsync();

            return("Basket Item created");
        }
        public async Task CreateProduct(Product product)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("creating product...");

            _dbContext.Products.Add(product);
            await _dbContext.SaveChangesAsync();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"product {product.Id} created!");

            Console.ResetColor();
        }
        public async Task <Product> AddProduct(string description, decimal price)
        {
            var product = new Product
            {
                Description = description,
                Price       = price
            };

            _commerceDbContext.Products.Add(product);
            await _commerceDbContext.SaveChangesAsync();

            return(product);
        }
Exemplo n.º 7
0
        public async Task <bool> CrateAsync(Person entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            await _db.Users.AddAsync(entity);

            await _db.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 8
0
 public async Task <bool> CrateAsync(Product entity)
 {
     if (entity == null)
     {
         throw new ArgumentException(nameof(entity));
     }
     return(await Task <bool> .Factory.StartNew(() =>
     {
         _db.Products.AddAsync(entity);
         _db.SaveChangesAsync();
         return true;
     }));
 }
Exemplo n.º 9
0
        public async Task <IActionResult> Register(User user)
        {
            if (ModelState.IsValid)
            {
                var check = _context.Users.Count(s => s.Username.Equals(user.Username));
                if (check == 0)
                {
                    if (user.Password.Equals(user.Confirm))
                    {
                        _context.Users.Add(user);
                        await _context.SaveChangesAsync();

                        HttpContext.Session.Set("username", Encoding.ASCII.GetBytes(user.Username));
                        return(Redirect("/home"));
                    }
                }
            }
            return(Redirect("/home"));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> CreateFromQuote([FromRoute] Guid quoteId,
                                                          CancellationToken cancellationToken = default)
        {
            var quote = await _dbContext.Quotes
                        .Include(q => q.Items)
                        .ThenInclude(qi => qi.Product).FirstOrDefaultAsync(q => q.Id == quoteId, cancellationToken);

            if (null == quote)
            {
                return(BadRequest($"invalid quote id: {quoteId}"));
            }

            var order = Order.FromQuote(quote);

            _dbContext.Orders.Add(order);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(CreatedAtRoute("order-details", new { id = order.Id }, order.Id));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Create(CreateProduct dto, CancellationToken cancellationToken = default)
        {
            if (null == dto)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currency = Currency.FromCode(dto.Currency);

            var product = new Product(Guid.NewGuid(), new Money(currency, dto.Price), dto.Name, DateTime.UtcNow);

            _dbContext.Products.Add(product);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(CreatedAtRoute("product-details", new { id = product.Id }, product.Id));
        }
Exemplo n.º 12
0
        public async Task AddItem(int userId, int productId, int quantity)
        {
            if (quantity <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(quantity), quantity, "Must be greater than 0.");
            }

            _logger.LogInformation("User {UserId} request to add the item {ProductId}.", userId, productId);

            _logger.LogInformation("Calling database");

            Product product = await _commerceDbContext.Products
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.ProductId == productId);

            if (product == null)
            {
                throw new Exception($"Product id {productId} not found!");
            }

            Func <CartItem> newCartItem = () => new CartItem
            {
                ProductId   = product.ProductId,
                Description = product.Description,
                Price       = product.Price,
                Quantity    = quantity
            };

            Cart cart = await _commerceDbContext.Carts
                        .FirstOrDefaultAsync(x => x.UserId == userId && x.PurchaseDate == null);

            if (cart == null)
            {
                cart = new Cart
                {
                    UserId = userId,
                    Items  = new List <CartItem>
                    {
                        newCartItem()
                    }
                };
                _commerceDbContext.Carts.Add(cart);
            }
            else
            {
                CartItem cartItem = await _commerceDbContext.CartItems
                                    .FirstOrDefaultAsync(x => x.CartId == cart.CartId && x.ProductId == productId);

                if (cartItem != null)
                {
                    cartItem.Quantity = cartItem.Quantity + quantity;
                }
                else
                {
                    cartItem = newCartItem();
                    cart.Items.Add(cartItem);
                }
            }

            await _commerceDbContext.SaveChangesAsync();

            _logger.LogInformation("Cart item has beed added.");
        }