public async Task <IActionResult> BuyItem(string id) { if (!_signInManager.IsSignedIn(User)) { ViewBag.ErrorMessage = "You need to be signed in before you can buy this item"; return(View("Error")); } ApplicationUser user = _userManager.FindByNameAsync(User.Identity.Name).Result; PlayerItem item = _playerItemRepository.FindByID(id); if (user.Blyats < item.Price) { return(RedirectToAction("Shop", new { message = "You do not have enough Blyats to purchase: " + item.Name })); } user.Blyats = user.Blyats - item.Price; _playerPurchasesRepository.Add(user, item); await _userManager.UpdateAsync(user); return(RedirectToAction("Index")); }
public async Task <IActionResult> PlayerEdit(string id) { var user = await _userManager.FindByIdAsync(id); if (user == null) { ViewBag.ErrorMessage = $"User with Id = {id} cannot be found"; return(View("NotFound")); } if (_userManager.FindByNameAsync(User.Identity.Name).Result != user) { ViewBag.ErrorMessage = "You are not allowed to do this action."; return(View("Error")); } var model = new PlayerEditViewModel { Id = user.Id, Username = user.UserName, Email = user.Email, ProfilePicturePath = user.ProfilePicturePath, Password = user.PasswordHash, ConfirmPassword = user.PasswordHash, ProfileTitle = user.ProfileTitle, BackgroundName = user.BackGround }; var playerPurchaseList = _playerPurchasesRepository.GetPlayerPurchasesByUser(user).ToList(); List <PlayerItem> profileTitleUser = new List <PlayerItem>(); List <PlayerItem> backgroundPictureUser = new List <PlayerItem>(); for (int i = 0; i < playerPurchaseList.Count(); i++) { var item = _playerItemRepository.FindByID(playerPurchaseList[i].ItemID); if (item == null) { ViewBag.ErrorMessage = "Something went wrong while trying to find the user's items. Please try again later or contact the support team"; return(View("Error")); } if (item.Type == ItemType.ProfileTitle) { profileTitleUser.Add(item); } else { backgroundPictureUser.Add(item); } } List <SelectListItem> profileTitlesSelect = new List <SelectListItem>(); List <SelectListItem> backgroundsSelect = new List <SelectListItem>(); foreach (PlayerItem profileTitle in profileTitleUser) { profileTitlesSelect.Add(new SelectListItem(profileTitle.Name, profileTitle.Name)); } foreach (PlayerItem backgroundPicture in backgroundPictureUser) { backgroundsSelect.Add(new SelectListItem(backgroundPicture.Name, backgroundPicture.Name)); } model.ProfileTitles = profileTitlesSelect; model.BackgroundNames = backgroundsSelect; return(View(model)); }