/// <summary> /// Creates a new quest_template and adds it to the database /// </summary> /// <param name="model">The AddQuestViewModel passed in from the controller</param> public void AddQuest(AddQuestViewModel model) { quest_template newQuest = new quest_template { created_date = DateTime.Now, creator_id = model.CreatorID, description = model.Description, featured = false, icon = model.IconFilePath, icon_file_name = model.Icon, last_modified_by_id = null, last_modified_date = null, posted_date = null, retire_date = null, state = (int)JPPConstants.AchievementQuestStates.Draft, threshold = model.Threshold, title = model.Title, user_generated = model.UserGenerated, keywords = "", }; List<quest_achievement_step> questAchievementSteps = new List<quest_achievement_step>(); foreach (int i in model.SelectedAchievementsList) { quest_achievement_step q = new quest_achievement_step { achievement_id = i, quest_id = newQuest.id }; questAchievementSteps.Add(q); } AddQuestTemplateToDatabase(newQuest); AddAchievementStepsToDatabase(questAchievementSteps); Save(); }
public ActionResult AddQuest(AddQuestViewModel model) { //Create a new Unit of Work UnitOfWork work = new UnitOfWork(); //Add the current logged in user to the model (They are the ones creating it) model.CreatorID = WebSecurity.CurrentUserId; model.UserGenerated = false; //Make sure the quest has associated achievements if (model.SelectedAchievementsList == null || 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.SelectedAchievementsList == null || 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)) 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); } //Make Sure the Directories Exist Utilities.JPPDirectory.CheckAndCreateAchievementAndQuestDirectory(Server); //Create the file path and save the image model.IconFilePath = Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.QuestIcon); if (JPPImage.SaveQuestIcons(model.IconFilePath, model.Icon, false)) { //Add the Quest work.QuestRepository.AddQuest(model); TempData["Message"] = "Quest: " + model.Title + " successfully created and is in draft mode awaiting approval."; return RedirectToAction("EditQuestList"); } } //ModelState was invalid, refrech the Achievements list to prevent NullRefrenceException AddQuestViewModel refreshModel = AddQuestViewModel.Populate(); model.AchievementsList = refreshModel.AchievementsList; model.IconList = refreshModel.IconList; return View(model); }