public ActionResult EditQuest(int id, EditQuestViewModel model) { //Create a new Unit of Work UnitOfWork work = new UnitOfWork(); model.EditorID = WebSecurity.CurrentUserId; //Check to make sure the Quest isn't being put back into draft mode JPPConstants.AchievementQuestStates currentQuestState = (JPPConstants.AchievementQuestStates)work.QuestRepository.GetQuestState(id); JPPConstants.AchievementQuestStates modelQuestState = (JPPConstants.AchievementQuestStates)model.State; if (!currentQuestState.Equals(JPPConstants.AchievementQuestStates.Draft) && modelQuestState.Equals(JPPConstants.AchievementQuestStates.Draft)) ModelState.AddModelError(String.Empty, "This Achievement was already moved out of draft mode, it cannot be moved back"); //Make sure the quest has associated achievements if (model.SelectedAchievementsList.Count <= 0) ModelState.AddModelError(String.Empty, "No Achievements were selected for this quest"); //Make sure the Threshold value doesn't exceed the number of selected achievements if (model.Threshold > model.SelectedAchievementsList.Count) ModelState.AddModelError("Threshold", "The Threshold value was greater than the number of achievements selected for this quest."); model.Threshold = model.Threshold == null || model.Threshold <= 0 ? model.SelectedAchievementsList.Count : model.Threshold; if (model.Icon == null && model.UploadedIcon == null) ModelState.AddModelError(String.Empty, "An icon must be selected for this achievement"); if (model.Title != null) if (work.QuestRepository.QuestTitleExists(model.Title, id)) ModelState.AddModelError("", "A quest with that title already exists"); if (ModelState.IsValid) { if (model.UploadedIcon != null) { String filePath = Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.NewIconUpload); model.Icon = filePath.Replace("~/Content/Images/Icons/", ""); model.Icon = model.Icon.Replace(".png", ""); JPPImage.Save(Server, filePath, model.UploadedIcon.InputStream, 400, 400, true); } quest_template template = work.EntityContext.quest_template.Find(id); model.IconFilePath = template == null ? Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.QuestIcon) : template.icon; if (JPPImage.SaveQuestIcons(model.IconFilePath, model.Icon, false)) { //Add the Quest to the Database work.QuestRepository.AdminEditQuest(id, model); //Return to the Admin index page TempData["Message"] = "Changes to " + model.Title + " were successfully saved."; return RedirectToAction("EditQuestList"); } } //ModelState was invalid, refresh the AchievementsList to prevent NullReferenceException AddQuestViewModel refreshModel = AddQuestViewModel.Populate(); model.AchievementsList = refreshModel.AchievementsList; model.IconList = refreshModel.IconList; return View(model); }
/// <summary> /// Gets the specified quest_template and updates it with any edits /// </summary> /// <param name="id">The id of the quest_template</param> /// <param name="model">The EditQuestViewModel passed in from the controller</param> public void AdminEditQuest(int id, EditQuestViewModel model) { List<LoggerModel> logChanges = new List<LoggerModel>(); quest_template currentQuest = _dbContext.quest_template.Find(id); if (currentQuest == null) return; #region// Title if (currentQuest.title != model.Title && !String.IsNullOrWhiteSpace(model.Title)) { logChanges.Add(new LoggerModel() { Action = "Edit Quest: " + Logger.EditQuestLogType.Title.ToString(), UserID = model.EditorID, IPAddress = HttpContext.Current.Request.UserHostAddress, TimeStamp = DateTime.Now, IDType1 = Logger.LogIDType.QuestTemplate.ToString(), ID1 = id, Value1 = currentQuest.title, Value2 = model.Title }); currentQuest.title = model.Title; } #endregion #region// Description if (currentQuest.description != model.Description && !String.IsNullOrWhiteSpace(model.Description)) { logChanges.Add(new LoggerModel() { Action = "Edit Quest: " + Logger.EditQuestLogType.Description.ToString(), UserID = model.EditorID, IPAddress = HttpContext.Current.Request.UserHostAddress, TimeStamp = DateTime.Now, IDType1 = Logger.LogIDType.QuestTemplate.ToString(), ID1 = id, Value1 = currentQuest.description, Value2 = model.Description }); currentQuest.description = model.Description; } #endregion #region// Icon if (currentQuest.icon != model.IconFilePath && !String.IsNullOrWhiteSpace(model.IconFilePath)) { logChanges.Add(new LoggerModel() { Action = "Edit Quest: " + Logger.EditQuestLogType.Icon.ToString(), UserID = model.EditorID, IPAddress = HttpContext.Current.Request.UserHostAddress, TimeStamp = DateTime.Now, IDType1 = Logger.LogIDType.QuestTemplate.ToString(), ID1 = id, Value1 = currentQuest.icon, Value2 = model.IconFilePath }); currentQuest.icon = model.IconFilePath; currentQuest.icon_file_name = model.Icon; } #endregion #region// Posted Date if (currentQuest.state != model.State && model.State.Equals((int)JPPConstants.AchievementQuestStates.Active) && currentQuest.posted_date == null) currentQuest.posted_date = DateTime.Now; #endregion #region// Retire Date if (currentQuest.state != model.State && model.State.Equals((int)JPPConstants.AchievementQuestStates.Retired) && currentQuest.retire_date == null) currentQuest.retire_date = DateTime.Now; if (currentQuest.state != model.State && currentQuest.state.Equals((int)JPPConstants.AchievementQuestStates.Retired)) currentQuest.retire_date = null; #endregion #region// State if (currentQuest.state != model.State) { logChanges.Add(new LoggerModel() { Action = "Edit Quest: " + Logger.EditQuestLogType.State.ToString(), UserID = model.EditorID, IPAddress = HttpContext.Current.Request.UserHostAddress, TimeStamp = DateTime.Now, IDType1 = Logger.LogIDType.QuestTemplate.ToString(), ID1 = id, Value1 = currentQuest.state.ToString(), Value2 = model.State.ToString() }); currentQuest.state = model.State; } #endregion #region//Featured if (currentQuest.state != (int)JPPConstants.AchievementQuestStates.Active) currentQuest.featured = false; #endregion #region// Last Modified By And Date Last Modified currentQuest.last_modified_by_id = model.EditorID; currentQuest.last_modified_date = DateTime.Now; #endregion #region// Threshold if (currentQuest.threshold != model.Threshold) { logChanges.Add(new LoggerModel() { Action = "Edit Quest: " + Logger.EditQuestLogType.Threshold.ToString(), UserID = model.EditorID, IPAddress = HttpContext.Current.Request.UserHostAddress, TimeStamp = DateTime.Now, IDType1 = Logger.LogIDType.QuestTemplate.ToString(), ID1 = id, Value1 = currentQuest.threshold.ToString(), Value2 = model.Threshold.ToString() }); currentQuest.threshold = model.Threshold; } #endregion #region// Replace achievement steps IEnumerable<quest_achievement_step> oldQuestAchievementSteps = _dbContext.quest_achievement_step.Where(q => q.quest_id == id); foreach (quest_achievement_step step in oldQuestAchievementSteps) _dbContext.quest_achievement_step.Remove(step); List<quest_achievement_step> newQuestAchievementSteps = new List<quest_achievement_step>(); foreach (int i in model.SelectedAchievementsList) { quest_achievement_step q = new quest_achievement_step { achievement_id = i, quest_id = id }; newQuestAchievementSteps.Add(q); } AddAchievementStepsToDatabase(newQuestAchievementSteps); #endregion if (logChanges.Count > 0) Logger.LogMultipleEntries(logChanges, _dbContext); Save(); CheckAllUserQuestCompletion(currentQuest.id); }