public async Task <IActionResult> Return(int id)
        {
            RentOrder selectedRent = _rentOrders.GetObjectById(id).Result;

            if (selectedRent.CurrentRentedDay == DateTime.Today)
            {
                TempData["Error"] = "You can't return a game on the same day you rent it!";
                return(RedirectToAction("DisplayRents", "Rent"));
            }
            Game returnedGame = _games.GetAllObjects().Result.Where(g => g.Name.Equals(selectedRent.GameRented)).FirstOrDefault();

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

            var selectedRentJson = JsonConvert.SerializeObject(selectedRent, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }
                                                               );

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

            var receivedRent = await _broker.ReceiveMessage("ReturnToWeb");

            user.Balance += receivedRent.TotalPayment;

            await _users.Update(user);

            await _rentOrders.Delete(selectedRent.Id);

            HttpContext.Session.SetString("Balance", user.Balance.ToString());

            return(RedirectToAction("DisplayRents", "Rent"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Return(int id)
        {
            RentOrder selectedRent = _rentOrders.GetObjectById(id).Result;

            if (selectedRent.CurrentRentedDay == DateTime.Today)
            {
                SetTempData("Error", "You can't return a game on the same day you rent it!");
                return(RedirectToAction("DisplayRents", "Rent"));
            }
            Game returnedGame = _games.GetAllObjects().Result.Where(g => g.Name.Equals(selectedRent.GameRented)).FirstOrDefault();
            var  user         = _users.GetAllObjects().Result.FirstOrDefault(u => u.UserName.Equals(HttpContext.Session.GetString("Username")));

            // DDD operations (kind of)
            returnedGame = returnedGame.ReturnGame();
            selectedRent = await selectedRent.InterruptRentAsync(_broker);

            user = user.RemoveRent(selectedRent);

            // database operations
            await _users.Update(user);

            await _rentOrders.Delete(selectedRent.Id);

            HttpContext.Session.SetString("Balance", user.Balance.ToString());

            return(RedirectToAction("DisplayRents", "Rent"));
        }
        public IActionResult Index(int id)
        {
            if (HttpContext.Session.GetString("Username") == null)
            {
                TempData["Error"] = "You need to login First!";
                return(RedirectToAction("Index", "Game"));
            }

            rentedGameid     = id;
            gameRented       = _games.GetObjectById(rentedGameid).Result;
            ViewBag.GameName = gameRented.Name;
            if (gameRented.Quantity < 1)
            {
                TempData["Error"] = "There are no copies available!";
                return(RedirectToAction("Index", "Game"));
            }
            return(View());
        }