Exemplo n.º 1
0
        public Guid Create(NewCategoryViewModel model)
        {
            Category newCategory = new Category();

            newCategory.CategoryId = Guid.NewGuid();
            newCategory.Name       = model.Name;

            _dbContext.Categories.Add(newCategory);
            _dbContext.SaveChanges();

            return(newCategory.CategoryId);
        }
Exemplo n.º 2
0
        public Guid Create(NewSubcategoryViewModel model)
        {
            Subcategory newSubcategory = new Subcategory()
            {
                SubcategoryId = Guid.NewGuid(),
                Name          = model.Name,
                CategoryId    = model.CategoryId
            };

            _dbContext.Subcategories.Add(newSubcategory);
            _dbContext.SaveChanges();

            return(newSubcategory.SubcategoryId);
        }
Exemplo n.º 3
0
        public Guid Create(NewProductViewModel model /*, ApplicationUser user*/)
        {
            Product newProduct = new Product();

            newProduct.ProductId      = Guid.NewGuid();
            newProduct.Name           = model.Name;
            newProduct.ItemsAvailable = model.ItemsAvailable;
            newProduct.Price          = model.Price;
            newProduct.SubcategoryId  = model.SubcategoryId;

            _dbContext.Products.Add(newProduct);
            _dbContext.SaveChanges();

            return(newProduct.ProductId);
        }
Exemplo n.º 4
0
        public Guid Create(Guid deliveryAddressId, ApplicationUser user)
        {
            List <Product> products = new List <Product>();

            foreach (ItemInCartViewModel item in GetCart())
            {
                if (item.Quantity <= item.Product.ItemsAvailable)
                {
                    for (int i = 0; i < item.Quantity; i++)
                    {
                        products.Add(item.Product);
                    }
                }
                else
                {
                    throw new Exception("Not enough products in stock");
                }
            }
            Order newOrder = new Order();

            newOrder.OrderId           = Guid.NewGuid();
            newOrder.DateOfPurchase    = DateTime.Now;
            newOrder.OrderState        = OrderState.New;
            newOrder.DeliveryAddressId = deliveryAddressId;
            newOrder.UserId            = user.Id;
            _dbContext.Orders.Add(newOrder);
            foreach (Product product in products)
            {
                Product tempProd = _dbContext.Products.FirstOrDefault(p => p.ProductId == product.ProductId);
                if (tempProd.ItemsAvailable > 0)
                {
                    ProductCopy newCopy = new ProductCopy();
                    newCopy.ProductCopyId = Guid.NewGuid();
                    newCopy.ProductId     = product.ProductId;
                    newCopy.Price         = product.Price;
                    newCopy.OrderId       = newOrder.OrderId;
                    _dbContext.ProductsCopies.Add(newCopy);
                    tempProd.ItemsAvailable--;
                    _dbContext.Entry(tempProd).Property(p => p.ItemsAvailable).IsModified = true;
                    _dbContext.SaveChanges();
                }
            }
            _dbContext.SaveChanges();
            EmptyCart();
            return(newOrder.OrderId);
        }