public async Task UpdateBotAsync(string userId, PlayerBotViewModel model) { using (var db = new ApplicationDbContext()) { var user = await db.Users.FirstOrDefaultAsync(x => x.Id == userId); if (user != null) { var botToUpdate = db.PlayerBots.FirstOrDefault(x => x.Id == model.Id && x.OwnerId == user.Id); if (botToUpdate != null) { if (db.PlayerBots.Any(x => x.Name == model.Name && x.Id != model.Id)) { throw new ArgumentException("A bot with that name already exists"); } botToUpdate.Name = model.Name; botToUpdate.URL = model.Url; botToUpdate.Image = model.Image; botToUpdate.Private = model.Private; await db.SaveChangesAsync(); } else { throw new ArgumentException("Could not find bot"); } } else { throw new ArgumentException("Could not find user"); } } }
public async Task <ActionResult> EditBot(int id) { var userBots = await UserManager.GetPlayerBotsAsync(User.Identity.GetUserId()); var foundBot = userBots.FirstOrDefault(x => x.Id == id); if (foundBot != null) { var model = new PlayerBotViewModel(foundBot); return(View(model)); } return(RedirectToAction("Index")); }
private PlayerBotViewModel GetPlayerBotViewModel(PlayerBot bot, string userName) { var matchHistory = db.GameSummaries.Where(x => x.Player1.Id == bot.Id || x.Player2.Id == bot.Id).ToList(); var model = new PlayerBotViewModel() { Id = bot.Id, Image = bot.Image, Name = bot.Name, Owner = bot.Owner != null ? bot.Owner.UserName : "", Rank = 0, OwnedByUser = bot.Owner != null && bot.Owner.UserName == userName, MatchHistory = matchHistory }; return(model); }
public async Task <ActionResult> EditBot(PlayerBotViewModel model) { try { if (model.Delete) { await UserManager.DeleteBot(User.Identity.GetUserId(), model.Id); } else { await UserManager.UpdateBotAsync(User.Identity.GetUserId(), model); } } catch (ArgumentException ex) { TempData["ErrorMessage"] = ex.Message; } return(RedirectToAction("Index")); }