public ActionResult EditPost(Models.PostModel item) { if (ModelState.IsValid) { var getByid = postRepository.FindBy(x => x.post == item.post).FirstOrDefault().post;//new PostManager().GetById(item.post).post; if (getByid == "" || getByid == null) { var result = postRepository.Add(new Post { post = item.post }); //new PostManager().Create(new Post { post = item.post }); //ViewBag.Message = result.msg; } else { var result = postRepository.Edit(new Post { post = item.post }); //new PostManager().Update(new Post { post = item.post }); ///ViewBag.Message = result.msg; } return(RedirectToAction("EditPost", "Home")); } else { return(View(item)); } }
public IActionResult Post(int id) { if (!IsLoggedIn()) { return(Redirect("/admin")); } var post = _postService.GetPost(id); var categories = _categoryService.GetCategories(id); var tags = _tagService.GetTags(id); var allCategories = SlickCMS.Core.Caching.MemoryCache.Get <List <SlickCMS.Data.Entities.CategorySummary> >("Categories|Posts"); var allTags = SlickCMS.Core.Caching.MemoryCache.Get <List <SlickCMS.Data.Entities.TagSummary> >("Tags"); var postModel = new Models.PostModel { Post = post, Comments = null,// not loading comments on the Admin page Categories = categories, Tags = tags, AllCategories = allCategories ?? new List <Data.Entities.CategorySummary>(), AllTags = allTags ?? new List <Data.Entities.TagSummary>(), }; return(View(postModel)); }
public IActionResult Index(string url) { // posts will be prefixed /post/[url] // pages will just be /[url] if (url == string.Empty) { url = HttpContext.Request.Path.ToString(); } // https://stackoverflow.com/questions/41577376/how-to-read-values-from-the-querystring-with-asp-net-core /*if (HttpContext.Request.QueryString("url") != "") * url = Request.QueryString("url");*/ var post = _postService.GetPost(url); var comments = _commentService.GetPublished(post.PostId); var categories = _categoryService.GetCategories(post.PostId); var tags = _tagService.GetTags(post.PostId); var postModel = new Models.PostModel { Post = post, Comments = comments, Categories = categories, Tags = tags, }; return(View(postModel)); }
public JsonResult EditMyPost(int id, Models.PostModel post) { // TODO: Add update logic here Post newPost = new Post(); var image = post.postImage; if (image != null) { var fileName = Path.GetFileName(image.FileName); var extension = Path.GetExtension(image.FileName); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(image.FileName); var contentType = image.ContentType.ToString(); newPost.mediaType = contentType; newPost.mediaPath = fileName.ToString(); image.SaveAs(Server.MapPath("/images/") + image.FileName); } User user = GetUserSession(); if (user != null) { newPost.creatorId = user.userId; } newPost.categoryId = post.categoryId; newPost.updateTime = DateTime.Now; newPost.postTitle = post.postTitle; newPost.postContent = post.postContent; postService.Edit(id, newPost); string result = "ok success"; return(Json(result, JsonRequestBehavior.AllowGet)); }
public JsonResult Create(Models.PostModel post) {//check if the model coming from our view contains any image too var image = post.postImage; Post savePost = new Post(); if (image != null) {//if post contains some image, grab the following data form the image var fileName = Path.GetFileName(image.FileName); var extension = Path.GetExtension(image.FileName); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(image.FileName); image.SaveAs(Server.MapPath("/images/") + image.FileName); savePost.mediaPath = fileName.ToString(); var contentType = image.ContentType.ToString(); savePost.mediaType = contentType; } //if not, just leave it blank else { savePost.mediaPath = "nothing"; } //get the user id as well because we want to store userId for this post User user = GetUserSession(); if (user != null) { savePost.creatorId = user.userId; } savePost.categoryId = post.categoryId; savePost.createTime = DateTime.Now; savePost.postTitle = post.postTitle; savePost.postContent = post.postContent; string result = ""; try { postService.Add(savePost); result = "Post Created successfully!"; } catch (Exception e) { result = e.ToString(); } //return the json result to the view return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult EditPost(string id) { Models.PostModel postModel = new Models.PostModel(); var itemsPosts = postRepository.GetAll();//new PostManager().Read(); postModel.postList = itemsPosts.Select(x => new SelectListItem { Text = x.post, Value = x.post }); if (id == null || id == "") { return(View(postModel)); } else { postModel.post = postRepository.FindBy(x => x.post == id.ToString()).FirstOrDefault().post;// new PostManager().GetById(id.ToString()).post; return(View(postModel)); } }
/// <summary> /// Renders the permalink for the given post. /// </summary> /// <param name="post">The post</param> /// <returns>The generated permalink</returns> public string Permalink(Models.PostModel post) { return(App.Env.Url("~/" + post.Type + "/" + post.Slug)); }
/// <summary> /// Gets the post specified by the given id. /// </summary> /// <param name="id">The post id</param> /// <returns>The post</returns> internal Post GetInternal(string id) { try { Models.PostModel pm = Models.PostModel.GetById(new Guid(id)); if (pm != null) { Post post = new Post() { Id = pm.Post.Id, TemplateName = pm.Post.TemplateName, Title = pm.Post.Title, Permalink = pm.Post.Permalink, Excerpt = pm.Post.Excerpt, Body = pm.Post.Body.ToHtmlString(), Created = pm.Post.Created.ToString(), Updated = pm.Post.Updated.ToString(), Published = pm.Post.Published.ToString(), LastPublished = pm.Post.LastPublished.ToString() }; // Categories foreach (var cat in pm.Categories) { post.Categories.Add(new Category() { Id = cat.Id, Permalink = cat.Permalink, Name = cat.Name, Description = cat.Description, Created = cat.Created.ToString(), Updated = cat.Updated.ToString() }); } // Properties foreach (var key in ((IDictionary <string, object>)pm.Properties).Keys) { post.Properties.Add(new Property() { Name = key, Value = (string) ((string)((IDictionary <string, object>)pm.Properties)[key]) }); } // Attachments foreach (var content in pm.Attachments) { post.Attachments.Add(new Attachment() { Id = content.Id, IsImage = content.IsImage }); } // Extensions post.ExpandedExtensions = pm.Extensions; foreach (var key in ((IDictionary <string, object>)pm.Extensions).Keys) { if (((IDictionary <string, object>)pm.Extensions)[key] is HtmlString) { post.Extensions.Add(new Extension() { Name = key, Body = ((HtmlString)((IDictionary <string, object>)pm.Extensions)[key]).ToHtmlString() }); } else { post.Extensions.Add(new Extension() { Name = key, Body = ((IDictionary <string, object>)pm.Extensions)[key] }); } } return(post); } } catch {} return(null); }
public async Task <IActionResult> OnGetAsync([Required] int id) { if (ModelState.IsValid) { var post = await S.Db.Posts .AsNoTracking() .Include(p => p.Author) .ThenInclude(a => a.Profile) .Include(p => p.ModerationInfo) .Include(p => p.ViewStatistic) .Include(p => p.Edits) .ThenInclude(e => e.Author) .FirstOrDefaultAsync(p => p.Id == id); if (post == null) { throw new NotFoundException(); } else { await S.Permissions.ValidateViewPostAsync(post); var commentaries = await S.Repository.GetCommentaryModelsAsync( S.Db.Commentaries .AsNoTracking() .Where(c => c.Post.Id == id)); Post = new Models.PostModel() { Author = post.Author.UserName, AuthorId = post.Author.Id, Body = post.Body, PostId = post.Id, CreationTime = post.CreationTime, Title = post.Title, CommentarySectionModel = new CommentarySectionModel() { Commentaries = commentaries.ToArray() }, Edits = post.Edits.Select(e => new Models.PostEditModel() { Author = e.Author.UserName, Reason = e.Reason, AuthorId = e.Author.Id, EditTime = e.EditTime }).ToArray(), AuthorBiography = post.Author.Profile.About, IsAuthentificated = S.HttpContext.User?.Identity?.IsAuthenticated ?? false, TotalViews = post.ViewStatistic.TotalViews, ModerationState = post.ModerationInfo.State, AuthorProfileImage = new ProfileImageModel() { RelativeUri = post.Author.Profile.Image }, IsDeleted = post.IsDeleted, DeleteReason = post.DeleteReason, CanAddCommentary = await S.Permissions.CanAddCommentaryAsync(post), CanDelete = await S.Permissions.CanDeletePostAsync(post), CanRestore = await S.Permissions.CanRestorePostAsync(post), CanReport = await S.Permissions.CanReportAsync(post), CanReportViolation = await S.Permissions.CanReportViolationAsync(post), CanEdit = await S.Permissions.CanEditPostAsync(post), CanMarkAsModerated = await S.Permissions.CanMarkAsModeratedAsync(post), CanMarkAsNotPassedModeration = await S.Permissions.CanMarkAsNotPassedModerationAsync(post), }; var viewStatistics = new Enumerable <IViewStatistic> { post.ViewStatistic, commentaries.Select(c => c.ViewStatistic), }.ToArray(); await S.CacheManager.CacheManager.SetRequestDataAsync(viewStatistics); await updateViewStatisticAsync(S.DbUpdator, S.HttpContext.User?.Identity?.IsAuthenticated ?? false, viewStatistics); NewCommentary.PostId = id; return(Page()); } } else { throw new Exception(); } }