internal PurchaseClass UpdatePurchase(PurchaseClass purchase) { string crypto = purchase.Crypto; if (purchase.Current <= 0 || (DateTime.Now - purchase.LastUpdate).TotalSeconds > 20) { purchase.Current = Math.Round(App.GetCurrentPrice(crypto, "defaultMarket"), 4); } curr = purchase.Current; purchase.Worth = Math.Round(curr * purchase.CryptoQty, 2); // If the user has also filled the invested quantity, we can calculate everything else if (purchase.InvestedQty > 0) { double priceBought = (1 / purchase.CryptoQty) * purchase.InvestedQty; priceBought = Math.Round(priceBought, 4); double earningz = Math.Round((curr - priceBought) * purchase.CryptoQty, 4); purchase.arrow = earningz < 0 ? "▼" : "▲"; purchase.BoughtAt = priceBought; purchase.Delta = Math.Round(curr / priceBought, 2) * 100; if (purchase.Delta > 100) { purchase.Delta -= 100; } purchase.Profit = Math.Round(Math.Abs(earningz), 2); purchase.ProfitFG = (earningz < 0) ? (SolidColorBrush)App.Current.Resources["pastelRed"] : (SolidColorBrush)App.Current.Resources["pastelGreen"]; } return(purchase); }
public ActionResult Purchase() { decimal sum = 0; int userId = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name).Id; var userCart = db.UserCarts.FirstOrDefault(x => x.UserId == userId && x.Result == null); if (userCart != null) { var cart = userCart.UserCartProducts.Select(x => new Cart() { ProductId = x.ProductId, Price = x.Product.Price, Qty = x.Qty }); foreach (var item in cart) { sum += item.Qty * db.Products.FirstOrDefault(x => x.Id == item.ProductId).Price; } } PurchaseClass pc = new PurchaseClass() { UserID = userId, FirstName = db.Users.FirstOrDefault(x => x.Id == userId).FirstName, LastName = db.Users.FirstOrDefault(x => x.Id == userId).LastName, City = db.Users.FirstOrDefault(x => x.Id == userId).City, PostalCode = db.Users.FirstOrDefault(x => x.Id == userId).PostalCode, Street = db.Users.FirstOrDefault(x => x.Id == userId).Street, Appartments = db.Users.FirstOrDefault(x => x.Id == userId).Appartment, TotalAmount = sum }; return(View(pc)); }