/// <summary> /// Updates the level in the database with the supplied values, provided /// that no game rules are violated. /// </summary> /// <param name="updatedLevel">The updated level (which must include the ID).</param> /// <returns>Task.</returns> /// <exception cref="InvalidOperationException">Thrown if the level update failed for any reason.</exception> public async Task UpdateLevelAsync(MissionLevelViewModel updatedLevel) { Log.Info($"Updating level id={updatedLevel.Id} name={updatedLevel.Name} mission={updatedLevel.MissionId}"); var maybeLevel = unitOfWork.MissionLevels.GetMaybe(updatedLevel.Id); if (maybeLevel.None) { Log.Error($"Update failed because level {updatedLevel.Id} was not found in the database"); throw new InvalidOperationException("The mission to be updated was not found in the database"); } var dbLevel = maybeLevel.Single(); var maybeMission = unitOfWork.Missions.GetMaybe(updatedLevel.MissionId); if (maybeMission.None) { Log.Error($"Update failed because mission {updatedLevel.MissionId} was not found in the database"); throw new InvalidOperationException("The target mission does not exist in the database"); } var targetMission = maybeMission.Single(); if (targetMission.MissionLevels.Any(p => p.Level == updatedLevel.Level && p.Id != updatedLevel.Id)) { Log.Warn($"Update blocked because the target mission {targetMission.Id} already had level {updatedLevel.Level}"); throw new InvalidOperationException($"The target mission already has a level {updatedLevel.Level}"); } if (LevelHasAssociatedObservations(updatedLevel.Id)) { Log.Warn($"Update blocked because the level has associated observations"); throw new InvalidOperationException("Cannot move a level that has observations associated with it"); } mapper.Map(updatedLevel, dbLevel); await unitOfWork.CommitAsync(); Log.Info($"Successfully updated level id={dbLevel.Id}"); }
// GET: Admin/MissionLevels/Create public ActionResult Create() { var model = new MissionLevelViewModel(); PopulateMissionPicker(model); return(View(model)); }
/// <summary> /// Creates the specified level according to game rules. /// Throws an exception if the level was invalid or could not be created. /// </summary> /// <param name="newLevel">The new level.</param> public async Task CreateLevelAsync(MissionLevelViewModel newLevel) { Log.Info($"Creating new level id={newLevel.Id} name={newLevel.Name} in mission {newLevel.MissionId}"); var specification = new LevelExistsInMission(newLevel.Level, newLevel.MissionId); var maybeHasLevel = unitOfWork.MissionLevels.GetMaybe(specification); if (maybeHasLevel.Any()) { Log.Warn($"Create failed because mission {newLevel.MissionId} already has a level {newLevel.Level}"); throw new InvalidOperationException($"There is already a level number {newLevel.Level} in the mission"); } var levelToAdd = mapper.Map <MissionLevelViewModel, MissionLevel>(newLevel); unitOfWork.MissionLevels.Add(levelToAdd); await unitOfWork.CommitAsync(); Log.Info($"Successfully created level id={newLevel.Id} name={newLevel.Name} in mission {newLevel.MissionId}"); }
public async Task <ActionResult> Create( [Bind(Include = "Id,Name,Level,AwardTitle,Precondition,MissionId")] MissionLevelViewModel model) { if (!ModelState.IsValid) { PopulateMissionPicker(model); return(View(model)); } try { await gameEngine.CreateLevelAsync(model); return(RedirectToAction("Index")); } catch (InvalidOperationException e) { ModelState.AddModelError(string.Empty, e.Message); PopulateMissionPicker(model); return(View(model)); } }
private void PopulateMissionPicker(MissionLevelViewModel model) { model.MissionPicker = uow.Missions.PickList.ToSelectList(); }