// GET: MyBots/Edit/5 public async Task<IActionResult> Edit(int? id, int version, bool run = false) { var authorBot = await GetAuthorBot(id, version); if (authorBot == null) return NotFound(); var rootId = authorBot.GetRootId(); var versions = await _context.AuthorBots .Where(b => b.AuthorId == Author.Id && (b.RootId == rootId || b.Id == rootId)) .ToListAsync(); var bot = await _context.Bots.FirstOrDefaultAsync(b => b.Id == authorBot.BotId); if (bot == null) return NotFound(); var status = authorBot.Status.ToBotStatus(); var model = new EditBotViewModel { Id = rootId, Version = authorBot.Version, Created = authorBot.Created, Status = status, Name = bot.Name, Code = bot.Code, ContinueEdit = true, Run = run, Versions = versions .OrderBy(b => b.Version) .ToArray(), Statuses = status.GetAllowedStatuses() .Select(s => new SelectListItem(s.ToString(), s.ToString())) .ToList() }; if (run) { model.Game = _gameService.PlayEditModeGame(bot.Id).Json; } return View(model); }
public async Task<IActionResult> Edit(int id, int version, [Bind("Id,Version,Status,Code,ContinueEdit,Run")] EditBotViewModel model) { if (id != model.Id || model.Version != version) return NotFound(); if (!ModelState.IsValid) return View(model); try { var authorBot = await GetAuthorBot(model.Id, model.Version); if (authorBot == null) return NotFound(); var bot = await _context.Bots.FirstOrDefaultAsync(b => b.Id == authorBot.BotId); if (bot == null) return NotFound(); var rootId = authorBot.GetRootId(); var status = authorBot.Status.ToBotStatus(); // if the code has changed if (bot.Code != model.Code) { // if the bot is in Draft status = change current bot if (status == BotStatus.Draft) { bot.Code = model.Code; _context.Update(bot); await _context.SaveChangesAsync(); } else // For other statuses - create new version { var versions = await _context.AuthorBots .Where(b => b.AuthorId == Author.Id && (b.RootId == rootId || b.Id == rootId)) .ToListAsync(); var newBotVersion = await CreateBot(bot.Name, model.Code, rootId, authorBot.Id, versions.Max(v => v.Version) + 1); return RedirectToAction(nameof(Edit), new { id = model.Id, version = newBotVersion.Version, run = model.Run }); } } if (status.IsAllowedStatus(model.Status)) { authorBot.Status = (int)model.Status; _context.Update(authorBot); // if it is a public status - make all other versions private if (model.Status.IsPublicStatus()) { var versions = await _context.AuthorBots .Where(b => b.AuthorId == Author.Id && (b.RootId == rootId || b.Id == rootId) && b.Id != authorBot.Id) .ToListAsync(); foreach (var botVersion in versions.Where(v => v.Status.ToBotStatus().IsPublicStatus())) { botVersion.Status = (int)BotStatus.Private; _context.Update(version); } } await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!AuthorBotExists(model.Id)) return NotFound(); throw; } if (model.ContinueEdit) return RedirectToAction(nameof(Edit), new { id = model.Id, version = model.Version, run = model.Run }); return RedirectToAction(nameof(Index)); }