Beispiel #1
0
        public void SetUserCart([FromBody] string cartJson)
        {
            if (cartJson == null)
            {
                return;
            }

            //Get id of currently authorized user
            string id = User.FindFirstValue(ClaimTypes.NameIdentifier);

            //Get saved cart data, if any, from database
            UserCart cart = _context.UserCart
                            .Where(c => c.UserId.Equals(id))
                            .FirstOrDefault();

            //If the cart data is null, create a new UserCart object and save to the database. Otherwise update the existing.
            if (cart == null)
            {
                cart = new UserCart()
                {
                    UserId   = id,
                    CartData = cartJson
                };

                _context.Add(cart);
            }
            else
            {
                cart.CartData = cartJson;
                _context.Update(cart);
            }

            _context.SaveChanges();
        }
        public IActionResult CreateNewInvoice([FromBody] string invoiceRequestJson)
        {
            try
            {
                //Deserialize the request
                InvoiceRequestDTO request = JsonConvert.DeserializeObject <InvoiceRequestDTO>(invoiceRequestJson);

                //Get the id of the currently authorized user
                string id = User.FindFirstValue(ClaimTypes.NameIdentifier);

                //Create new invoice model object
                Invoice invoice = new Invoice()
                {
                    TransactionId    = request.TransactionId,
                    TotalPaid        = request.TotalPaid,
                    UserId           = id,
                    InvoiceDate      = DateTime.Now,
                    ShippingName     = request.BuyerName,
                    ShippingStreet   = request.BuyerStreet,
                    ShippingCity     = request.BuyerCity,
                    ShippingPostcode = request.BuyerPostcode
                };

                //Loop through products in cart and add to InvoiceProduct table
                foreach (CartProductResponse cartProduct in request.CartProducts)
                {
                    invoice.InvoiceProduct.Add(new InvoiceProduct()
                    {
                        PriceAtTime = cartProduct.Product.CurrentPrice,
                        ProductId   = cartProduct.Product.ProductId,
                        Quantity    = cartProduct.Quantity
                    });

                    //If product is a physical item, adjust the stock level
                    if (cartProduct.Product.IsPhysical)
                    {
                        //Find product associated with this line item
                        Product product = _context.Product
                                          .Where(p => p.ProductId == cartProduct.Product.ProductId)
                                          .FirstOrDefault();

                        //If product is not null...
                        if (product != null)
                        {
                            //Adjust stock level
                            product.StockLevel -= cartProduct.Quantity;

                            //Just a check to ensure stock level doesn't go below zero, this shouldn't happen
                            if (product.StockLevel < 0)
                            {
                                product.StockLevel = 0;
                            }

                            //Update product in database
                            _context.Product.Update(product);
                        }
                    }
                }

                //Get saved cart data, if any, from database
                UserCart cart = _context.UserCart
                                .Where(c => c.UserId.Equals(id))
                                .FirstOrDefault();

                //If cart data is not null remove the entry from the database
                if (cart != null)
                {
                    _context.Remove(cart);
                }

                //Add invoice to database
                _context.Invoice.Add(invoice);
                _context.SaveChanges();

                return(Ok());
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("ERROR: An error occurred in InvoiceController.cs\n" + e.Message);
                return(BadRequest());
            }
        }