예제 #1
0
        private ActionResult Edit(EditMissionViewModel missionViewModel)
        {
            if (ModelState.IsValid)
            {
                //check if cryptic id is already used
                if (db.Missions.Any(m => m.CrypticId.Equals(missionViewModel.CrypticId) && m.Id != missionViewModel.Id))
                {
                    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("edit", missionViewModel));
                }


                var mission = db.Missions.Find(missionViewModel.Id);
                var user    = mission.Author;
                mission.CrypticId       = missionViewModel.CrypticId.ToUpper();
                mission.Name            = missionViewModel.Name;
                mission.Description     = missionViewModel.Description;
                mission.Length          = missionViewModel.Length;
                mission.Faction         = missionViewModel.Faction;
                mission.MinimumLevel    = missionViewModel.MinimumLevel;
                mission.Spotlit         = missionViewModel.Spotlit;
                mission.Status          = missionViewModel.Status;
                mission.DateLastUpdated = DateTime.Now;


                // in case the tags list is null create a blank one
                if (missionViewModel.Tags == null)
                {
                    missionViewModel.Tags = new List <string>();
                }
                //first remove the tags that don't exist anymore
                var currentTags = mission.Tags.ToList();
                foreach (var tag in currentTags)
                {
                    if (!missionViewModel.Tags.Contains(tag.TagName))
                    {
                        mission.Tags.Remove(tag);
                    }
                }
                //now add the tags that don't exist
                foreach (var tag in missionViewModel.Tags)
                {
                    var tagType = db.MissionTagTypes.FirstOrDefault(t => t.TagName.Equals(tag));
                    if (tagType != null)
                    {
                        if (!mission.Tags.Contains(tagType))
                        {
                            mission.Tags.Add(tagType);
                        }
                    }
                }

                db.SaveChanges();

                //now do images
                try
                {
                    var changedImages = false;
                    //set some defaults in case its null
                    #region Defaults
                    if (missionViewModel.OldImages == null)
                    {
                        missionViewModel.OldImages = new List <string>();
                    }
                    if (missionViewModel.Images == null)
                    {
                        missionViewModel.Images = new List <HttpPostedFileBase>();
                    }
                    if (mission.Images == null)
                    {
                        mission.Images = new List <MissionImage>();
                    }
                    #endregion
                    //check for removed images first
                    //if its different then a change was made, need to remove the images
                    if (missionViewModel.OldImages.Count != mission.Images.Count)
                    {
                        MissionImagesHelper.CheckForRemovedImages(db, mission, missionViewModel.OldImages);
                        changedImages = true;
                    }

                    //add new images
                    if (missionViewModel.Images.Count > 0)
                    {
                        var images = MissionImagesHelper.ValidateImages(missionViewModel.Images);
                        MissionImagesHelper.AddImages(images, mission);
                        changedImages = true;
                    }

                    if (changedImages)
                    {
                        db.SaveChanges();
                    }
                }
                catch
                {
                    TempData["Message"] = "An error occured while editing images.";
                }


                //now do videos
                try
                {
                    var changedVideos = false;
                    //set some defaults in case its null
                    #region Defaults
                    if (missionViewModel.OldVideos == null)
                    {
                        missionViewModel.OldVideos = new List <string>();
                    }
                    if (missionViewModel.Videos == null)
                    {
                        missionViewModel.Videos = new List <string>();
                    }
                    if (mission.Videos == null)
                    {
                        mission.Videos = new List <YoutubeVideo>();
                    }
                    #endregion
                    //check for removed videos first
                    //if its different then a change was made, need to remove the videos
                    if (missionViewModel.OldVideos.Count != mission.Videos.Count)
                    {
                        MissionVideosHelper.CheckForRemovedVideos(db, mission, missionViewModel.OldVideos);
                        changedVideos = true;
                    }

                    //add new videos
                    if (missionViewModel.Videos.Count > 0)
                    {
                        var videos = MissionVideosHelper.GetVideoIds(missionViewModel.Videos);
                        MissionVideosHelper.AddVideos(videos, mission);
                        changedVideos = true;
                    }

                    if (changedVideos)
                    {
                        db.SaveChanges();
                    }
                }
                catch
                {
                    TempData["Message"] = "An error occured while editing images.";
                }

                return(RedirectToAction("details", new { link = mission.MissionLink }));
            }
            return(View("edit", missionViewModel));
        }
예제 #2
0
        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));
        }