public async Task <IActionResult> Register(User user) { if (ModelState.IsValid) { await _userRepository.Insert(user); return(RedirectToAction("Index", "Game")); } return(RedirectToAction("RegisterView")); }
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> AddGame(Game game) { if (ModelState.IsValid) { if (HttpContext.Request.Form.Files != null) { var files = HttpContext.Request.Form.Files; foreach (var Image in files) { if (Image != null && Image.Length > 0) { var file = Image; var uploads = Path.Combine(_environment.WebRootPath, "images\\uploads\\"); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse (file.ContentDisposition).FileName.Trim('"'); System.Console.WriteLine(fileName); using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create)) { await file.CopyToAsync(fileStream); game.CoverImage = file.FileName; } } } } } await _games.Insert(game); return(RedirectToAction("Index")); } else { var errors = ModelState.Values.SelectMany(v => v.Errors); } return(View("Index")); }
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")); } }