private string UploadExportFile(HttpPostedFileBase exportFile) { //first read the file var b = new BinaryReader(exportFile.InputStream); var binData = b.ReadBytes(exportFile.ContentLength); var text = System.Text.Encoding.UTF8.GetString(binData); var missionlink = ""; Mission mission = null; //Get the mission object try { mission = MissionExportHelper.ParseExportToMission(db, text); } catch { throw new Exception("Error parsing upload file. Please contact us at [email protected] or @STOMissions on twitter so we can determine what is wrong."); } //now that we have the mission we need to first check if it exists or not. var samemissions = db.Missions.Where(m => m.Name.Equals(mission.Name)).ToList(); if (samemissions.Count > 0) { foreach (var samemission in samemissions) { //We check with name check.then author check //if the author is null we cannot add it as we can't make sure it is the same if (samemission.Author == null) { throw new Exception("Another mission with the same name and ambiguous author already exists. Please add your mission manually. If you believe that mission belongs to you please contact us at [email protected] or @STOMissions on twitter so we can link it to your account."); } //now check author if (mission.AuthorUserId.ToUpper().Equals(samemission.Author.CrypticTag.ToUpper())) { missionlink = samemission.MissionLink; //if it is the same this is the exact same mission now make sure that the person uploading it is the author, or an admit if (User.Identity.Name.Equals(samemission.Author.UserName) || User.IsInRole(ConstantsHelper.AdminRole)) { samemission.DateLastUpdated = DateTime.Now; db.SaveChanges(); break; } else { //if it is not the same user name they are not allowed to update it. //just go to the mission return(missionlink); } } } } //if mission link is still empty means we haven't found the same mission so lets add it if (string.IsNullOrWhiteSpace(missionlink)) { missionlink = MissionHelper.GetMissionLink(db, mission); mission.MissionLink = missionlink; mission.DateLastUpdated = DateTime.Now; mission.DateAdded = DateTime.Now; mission.CrypticId = MissionExportHelper.GenerateRandomID(); mission.Length = MissionLength.FifteenToThirtyMinutes; mission.Published = false; mission.Spotlit = false; mission.Status = MissionStatus.Unpublished; db.Missions.Add(mission); db.SaveChanges(); } //get the mission's id var id = db.Missions.Where(m => m.MissionLink.Equals(missionlink)).FirstOrDefault().Id; //set the export file MissionExportHelper.SaveExportFile(text, id); return(missionlink); }
public ActionResult Edit(string link) { if (string.IsNullOrEmpty(link)) { return(RedirectToAction("index", "home")); } Mission mission = db.Missions.Where(m => m.MissionLink.Equals(link)).FirstOrDefault(); if (mission == null) { return(HttpNotFound()); } //only people who can edit a mission are the author or an admin if (!mission.Author.UserName.Equals(User.Identity.Name) && !User.IsInRole(ConstantsHelper.AdminRole)) { return(HttpNotFound()); } //if the user is not an admin and it is removed it doesn't exist if (mission.Status == MissionStatus.Removed && !User.IsInRole(ConstantsHelper.AdminRole)) { return(HttpNotFound()); } var publishedSelectItems = new List <SelectListItem>(); #region Published Select List publishedSelectItems.Add(new SelectListItem() { Value = "false", Text = "No", }); publishedSelectItems.Add(new SelectListItem() { Value = "true", Text = "Yes", }); #endregion var editModel = new EditMissionViewModel(); editModel.Id = mission.Id; editModel.CrypticId = mission.CrypticId; editModel.Name = mission.Name; editModel.Description = mission.Description; editModel.Length = mission.Length; editModel.Faction = mission.Faction; editModel.MinimumLevel = mission.MinimumLevel; editModel.Spotlit = mission.Spotlit; editModel.Published = mission.Published; editModel.Status = mission.Status; editModel.Author = mission.Author; editModel.AutoApprove = mission.Author.AutoApproval; editModel.Tags = mission.Tags.Select(t => t.TagName).ToList(); editModel.OldImages = mission.Images.OrderBy(i => i.Order).Select(i => i.Filename).ToList(); editModel.OldVideos = mission.Videos.OrderBy(v => v.Order).Select(v => v.YoutubeVideoId).ToList(); mission.MissionLink = MissionHelper.GetMissionLink(db, mission); var unselectedTags = db.MissionTagTypes.Select(t => t.TagName).ToList(); foreach (var tags in editModel.Tags) { unselectedTags.Remove(tags); } ViewBag.AvailableTags = unselectedTags; ViewBag.PublishedSelectList = new SelectList(publishedSelectItems, "Value", "Text"); ViewBag.MinimumLevelSelectList = new SelectList(MissionHelper.GetMinimumLevelSelectList(), "Value", "Text"); return(View(editModel)); }
public ActionResult Submit([Bind(Include = "CrypticId,Name,Description,Length,Faction,MinimumLevel,Spotlit,Published,Tags,Images,Videos")] SubmitMissionViewModel missionViewModel, string submitButton) { if (ModelState.IsValid) { //check if cryptic id is already used if (db.Missions.Any(m => m.CrypticId.Equals(missionViewModel.CrypticId))) { ModelState.AddModelError("DuplicateCrypticID", "Cryptic ID already exists."); List <SelectListItem> publishedSelectItems = MissionHelper.GetYesNoSelectList(); ViewBag.AvailableTags = db.MissionTagTypes.Select(t => t.TagName).ToList(); ViewBag.PublishedSelectList = new SelectList(publishedSelectItems, "Value", "Text"); ViewBag.MinimumLevelSelectList = new SelectList(MissionHelper.GetMinimumLevelSelectList(), "Value", "Text"); return(View(missionViewModel)); } ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name)); Mission mission = new Mission(); #region Copy Info mission.CrypticId = missionViewModel.CrypticId.ToUpper(); mission.Description = missionViewModel.Description; mission.Faction = missionViewModel.Faction; mission.Length = missionViewModel.Length; mission.MinimumLevel = missionViewModel.MinimumLevel; mission.Name = missionViewModel.Name; mission.Published = missionViewModel.Published; mission.Spotlit = missionViewModel.Spotlit; #endregion if (missionViewModel.Tags != null && missionViewModel.Tags.Count > 0) { mission.Tags = db.MissionTagTypes.Where(t => missionViewModel.Tags.Contains(t.TagName)).ToList(); } else { mission.Tags = new List <MissionTagType>(); } mission.MissionLink = MissionHelper.GetMissionLink(db, mission); mission.Author = user; mission.DateAdded = DateTime.Now; mission.DateLastUpdated = DateTime.Now; if (submitButton.Equals("Save and Publish")) { if (user.AutoApproval) { mission.Status = Models.FoundryMissionModels.Enums.MissionStatus.Published; } else { mission.Status = Models.FoundryMissionModels.Enums.MissionStatus.InReview; //mission.Status = Models.FoundryMissionModels.Enums.MissionStatus.Published; try { EmailHelper.ReportMissionNeedsApproval(mission); } catch { } } } else //if (submitButton.Equals("Save")) { //don't do anything, leave it at the current status (default = unpublished) //mission.Status = Models.FoundryMissionModels.Enums.MissionStatus.Unpublished; } mission.Spotlit = false; db.Missions.Add(mission); db.SaveChanges(); //remove null if there is a null if (missionViewModel.Images.Count > 0) { if (missionViewModel.Images[missionViewModel.Images.Count - 1] == null) { missionViewModel.Images.RemoveAt(missionViewModel.Images.Count - 1); } } if (missionViewModel.Images.Count > 0) { try { var images = MissionImagesHelper.ValidateImages(missionViewModel.Images); MissionImagesHelper.AddImages(images, mission); db.SaveChanges(); } catch { TempData["Message"] = "An error occured while adding images."; } } //remove null if there is a null if (missionViewModel.Videos.Count > 0) { if (missionViewModel.Videos[missionViewModel.Videos.Count - 1] == null) { missionViewModel.Videos.RemoveAt(missionViewModel.Videos.Count - 1); } } if (missionViewModel.Videos.Count > 0) { try { var videos = MissionVideosHelper.GetVideoIds(missionViewModel.Videos); MissionVideosHelper.AddVideos(videos, mission); db.SaveChanges(); } catch { if (!string.IsNullOrEmpty(TempData[""] as string)) { TempData["Message"] = "Errors when adding images and videos."; } else { TempData["Message"] = "An error occured while adding videos."; } } } return(RedirectToAction("details", new { link = mission.MissionLink })); } return(View(missionViewModel)); }