public async Task <IActionResult> Extend(int id, int days)
        {
            var selectedRent = _rentOrders.GetObjectById(id).Result;

            selectedRent.RentPeriod += Convert.ToInt32(days);

            var selectedRentJson = JsonConvert.SerializeObject(selectedRent);

            await _broker.SendMessage(selectedRentJson, "RentToWorker");

            var rentReceived = _broker.ReceiveMessage("WorkerToRent").Result;



            var user = _users.GetAllObjects().Result.FirstOrDefault(u => u.UserName.Equals(HttpContext.Session.GetString("Username")));

            user.Balance -= days * 3f;
            if (user.Balance < 0)
            {
                TempData["Error"] = "You don't have enough money to extend it's rent duartion";
                return(RedirectToAction("DisplayRents", "Rent"));
            }
            HttpContext.Session.SetString("Balance", user.Balance.ToString());
            await _users.Update(user);

            await _rentOrders.Update(rentReceived);

            return(RedirectToAction("DisplayRents", "Rent"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Rent(RentViewModel rentView)
        {
            // database operations
            var rentedGame = _games.GetAllObjects().Result.FirstOrDefault(g => g.Name == rentView.RentedGame);
            var user       = _users.GetAllObjects().Result.FirstOrDefault(u => u.UserName.Equals(HttpContext.Session.GetString("Username")));
            var gameRented = _games.GetAllObjects().Result.FirstOrDefault(g => g.Name.Equals(rentView.RentedGame));

            rentedGame = rentedGame.RentGame();
            if (rentedGame == null)
            {
                SetTempData("Quantity", "No more products left on stock");
                return(View("Index"));
            }
            else
            {
                await _games.Update(gameRented);
            }
            var rent = rentView.Rent;

            user.RentOrders = new Collection <RentOrder>();
            // DDD operations (kind of)
            rent.GameRented = gameRented.Name;
            await rent.CreateRentAsync(_broker);

            user = user.AddRent(rent);

            if (user == null)
            {
                SetTempData("Funds", $"Not enough funds, payment is {rent.TotalPayment}$!");
                return(View("Index"));
            }
            else
            {
                await _rentOrders.Insert(rent);

                await _users.Update(user);

                HttpContext.Session.SetString("Balance", Convert.ToString(user.Balance));
                return(RedirectToAction("Index", "Game"));
            }
        }
        public async Task <IActionResult> Rent(RentOrder rent)
        {
            rent.CurrentRentedDay = DateTime.Today;
            rent.GameRented       = gameRented.Name;

            var rentJson = JsonConvert.SerializeObject(rent);
            await _broker.SendMessage(rentJson, "RentToWorker");

            var rentReceived = _broker.ReceiveMessage("WorkerToRent").Result;

            rent.ExpiringDate = rentReceived.ExpiringDate;
            rent.TotalPayment = rentReceived.TotalPayment;

            var user = _users.GetAllObjects().Result.FirstOrDefault(u => u.UserName.Equals(HttpContext.Session.GetString("Username")));

            user.RentOrders = new Collection <RentOrder>();

            if (user.Balance > rent.TotalPayment && gameRented.Quantity >= 1)
            {
                user.Balance -= rent.TotalPayment;
                // update game quantity
                gameRented.Quantity -= 1;
                await _rentOrders.Insert(rent);

                user.RentOrders.Add(rent);
                await _games.Update(gameRented);

                await _users.Update(user);

                HttpContext.Session.SetString("Balance", Convert.ToString(user.Balance));
                return(RedirectToAction("Index", "Game"));
            }
            else
            {
                TempData["Funds"] = $"Not enough funds, payment is {rent.TotalPayment}$!";
                return(View("Index"));
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Extend(int id, int days)
        {
            var selectedRent = _rentOrders.GetObjectById(id).Result;

            selectedRent = await selectedRent.ExtendRentAsync(days, _broker);

            var user = _users.GetAllObjects().Result.FirstOrDefault(u => u.UserName.Equals(HttpContext.Session.GetString("Username")));

            if (user.Balance < days * 3f)
            {
                SetTempData("Error", "You don't have enough money to extend it's rent duartion");
                return(RedirectToAction("DisplayRents", "Rent"));
            }
            user.Balance -= days * 3f;
            HttpContext.Session.SetString("Balance", user.Balance.ToString());
            await _users.Update(user);

            await _rentOrders.Update(selectedRent);

            return(RedirectToAction("DisplayRents", "Rent"));
        }