public ActionResult Add(SuccessStoryModel model, string add) { if (!_permissionService.Authorize(PermissionProvider.ManageSuccessStories)) return AccessDeniedView(); if (ModelState.IsValid) { try { // Build the story entity SuccessStory success = model.ToEntity(); success.Active = (add.ToLower() == "publish"); success.Author = _userService.GetUserById(model.AuthorId); // Insert the story entity _successStoryService.InsertSuccessStory(success); _slugService.InsertSlug(new Slug{ SlugUrl = SeoExtensions.GetSeoName(success.Title), SuccessStoryId = success.Id}); // Build the uploaded file name and store the file string path = HttpContext.Server.MapPath("/Uploads/Media/"); string filename = _webHelper.GetUniqueFileName(path, model.UploadedFile.FileName); model.UploadedFile.SaveAs(path + filename); // Build the media entity var media = new Media { EntityId = success.Id, EntityType = EntityType.SuccessStory, FileName = filename }; // Insert the media entity _mediaService.InsertMedia(media); SuccessNotification("The success story details have been added successfully."); return RedirectToAction("Index"); } catch (Exception ex) { ErrorNotification(ex.ToString()); } } PrepareBreadcrumbs(); AddBreadcrumb("Add New Story", null); return View(PrepareSuccessStoryModel(null)); }
public ActionResult Edit(SuccessStoryModel model, string save) { if (!_permissionService.Authorize(PermissionProvider.ManageSuccessStories)) return AccessDeniedView(); save = save.ToLower(); // get the category var story = _successStoryService.GetSuccessStoryById(model.Id); // check we have a category and they are not deleted if (story == null || story.Deleted) { ErrorNotification("The story couldn't be found or has been deleted."); return RedirectToAction("Index"); } story.Active = (save == "update" ? story.Active : (save == "publish")); story.Author = _userService.GetUserById(model.AuthorId); story.Title = model.Title; story.ShortSummary = model.ShortSummary; story.Article = model.Article; story.MetaTitle = model.MetaTitle; story.MetaDescription = model.MetaDescription; story.MetaKeywords = model.MetaKeywords; story.MetaTitle = model.MetaTitle; if (ModelState.IsValid) { try { _successStoryService.UpdateSuccessStory(story); _slugService.UpdateSlug(story.Id, SeoExtensions.GetSeoName(story.Title)); if (model.UploadedFile != null && model.UploadedFile.ContentLength > 0) { Media currentMedia = _mediaService.GetAllMediaByEntityId(EntityType.SuccessStory, model.Id).FirstOrDefault(); if (currentMedia != null) _mediaService.DeleteMedia(currentMedia); string path = HttpContext.Server.MapPath("/Uploads/Media/"); string filename = _webHelper.GetUniqueFileName(path, model.UploadedFile.FileName); Media media = new Media { EntityId = model.Id, EntityTypeId = (int) EntityType.SuccessStory, EntityType = EntityType.SuccessStory, FileName = filename }; model.UploadedFile.SaveAs(path + filename); _mediaService.InsertMedia(media); } SuccessNotification("The success story details have been updated successfully."); return RedirectToAction("Edit", story.Id); } catch (Exception) { ErrorNotification("An error occurred saving the success story details, please try again."); } } else { ErrorNotification("We were unable to make the change, please review the form and correct the errors."); } PrepareBreadcrumbs(); AddBreadcrumb("Edit Story", null); return View(PrepareSuccessStoryModel(story)); }
private SuccessStoryModel PrepareSuccessStoryModel(SuccessStory successStory) { var model = new SuccessStoryModel(); if (successStory != null) model = successStory.ToModel(); model.Authors = _userService.GetAllUsers().Select(u => u.ToModel()).ToList(); return model; }
public static SuccessStoryModel ToModel(this SuccessStory entity) { if (entity == null) return null; var mediaService = EngineContext.Current.Resolve<IMediaService>(); var model = new SuccessStoryModel { Active = entity.Active, CreatedBy = entity.CreatedBy, CreatedDate = entity.CreatedDate, Deleted = entity.Deleted, Id = entity.Id, LastModifiedBy = entity.LastModifiedBy, LastModifiedDate = entity.LastModifiedDate, Title = entity.Title, Author = entity.Author.ToModel(), AuthorId = entity.Author.Id, ShortSummary = entity.ShortSummary, Article = entity.Article, ProjectId = entity.ProjectId, MetaTitle = entity.MetaTitle, MetaDescription = entity.MetaDescription, MetaKeywords = entity.MetaKeywords, SeName = SeoExtensions.GetSeoName(entity.Title), Media = mediaService.GetAllMediaByEntityId(EntityType.SuccessStory, entity.Id).FirstOrDefault().ToModel() }; return model; }