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")); }
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")); }