Example #1
0
        public async Task <string> Checkout(string StreetAddress, string?Complex, string Suburb, string City, string Province, string PostCode)
        {
            ACMEDbContext context   = new ACMEDbContext();
            string        email     = HttpContext.Session.GetString("Email");
            var           user      = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First();
            ShoppingCart  cart      = user.ShoppingCart;
            var           cartItems = context.ProductShoppingCarts.Where(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID).Include(psc => psc.Product).ToList();

            string shippingAddress = StreetAddress + ", " + ((Complex != null) ? (Complex + ", ") : "") + Suburb + ", " + City + ", " + Province + ", " + PostCode;
            Order  order           = new Order();

            order.OrderDate       = DateTime.Now.Date;
            order.ETA             = DateTime.Now.AddDays(2).Date;
            order.ShippingAddress = shippingAddress;
            order.User            = user;
            context.Orders.Add(order);

            await context.SaveChangesAsync();

            foreach (var item in cartItems)
            {
                ProductOrder po = new ProductOrder();
                po.Order    = order;
                po.Product  = item.Product;
                po.Quantity = item.Quantity;

                context.Stock.First(s => s.Product == item.Product).Quantity -= item.Quantity;
                context.ProductOrders.Add(po);
                context.ProductShoppingCarts.Remove(item);
            }

            await context.SaveChangesAsync();

            return("Ok");
        }
Example #2
0
        public async Task <string> Create(string Name, string Description, string Price, IFormFile ProductImage, int CategoryID)
        {
            ACMEDbContext context = new ACMEDbContext();

            Product product = new Product()
            {
                Name        = Name,
                Description = Description,
                Price       = Convert.ToDouble(Price.Replace('.', ',')),
                Category    = context.Categories.First(c => c.CategoryID == CategoryID),
                Active      = true
            };

            //https://stackoverflow.com/questions/42741170/how-to-save-images-to-database-using-asp-net-core
            using (var ms = new MemoryStream())
            {
                ProductImage.CopyTo(ms);
                product.Image = ms.ToArray();
            }

            context.Add(product);
            await context.SaveChangesAsync();

            Stock stock = new Stock
            {
                Product  = product,
                Quantity = 5
            };

            context.Stock.Add(stock);
            await context.SaveChangesAsync();

            return("OK");
        }
Example #3
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            ACMEDbContext context = new ACMEDbContext();
            var           product = await context.Products.FindAsync(id);

            product.Active = false;
            await context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #4
0
        public async Task <string> Edit(int ProductID, string Name, string Description, string Price, IFormFile ProductImage, int CategoryID)
        {
            ACMEDbContext context = new ACMEDbContext();
            Product       product = context.Products.First(p => p.ProductCode == ProductID);

            product.Name        = Name;
            product.Description = Description;
            product.Price       = Convert.ToDouble(Price.Replace('.', ','));
            product.Category    = context.Categories.ToList().First(c => c.CategoryID == CategoryID);
            product.Active      = true;

            //If image is null, means value of file select wasnt changed
            if (ProductImage != null)
            {
                using (var ms = new MemoryStream())
                {
                    ProductImage.CopyTo(ms);
                    product.Image = ms.ToArray();
                }
            }

            try
            {
                context.Update(product);
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!context.Products.Any(e => e.ProductCode == ProductID))
                {
                    return("404. Product not found");
                }
                else
                {
                    throw;
                }
            }
            return("OK");
        }