public PostIndexViewModel(ISbDatabase db, string category = "", int page = 0) : base(db) { CurrentCategory = category ?? ""; Title = db.Site.Title; About = db.Site.BriefAbout; PageSize = db.Site.PageSize; var categoryPosts = FilterPostsByCategory(db.ActivePosts, CurrentCategory); int totalPostCount = categoryPosts.Count(); Page = Math.Min(Math.Max(page, 0), (int)Math.Floor((double)totalPostCount / (double)PageSize)); Posts = FilterPostsByPage(categoryPosts, PageSize, Page); var cat = PostCategories.FirstOrDefault(c => string.Compare(c.Name, CurrentCategory, true) == 0); if (cat != null) { CurrentCategoryName = cat.DisplayName; } else { CurrentCategoryName = CurrentCategory; } NewerPostCount = Page * PageSize; OlderPostCount = Math.Max(0, totalPostCount - (Page + 1) * PageSize); }
// GET: Global/Post public ActionResult Index(int?page) { OpPostSelect op = new OpPostSelect(); OperationResult result = _manager.ExecuteOperation(op); OpCategoriesSelect opCategories = new OpCategoriesSelect(); OperationResult res = _manager.ExecuteOperation(opCategories); PostCategories postCategories = new PostCategories(); postCategories.Categories = res.Items as CategoryDto[]; postCategories.Posts = result.Items as PostDto[]; OpMenuBase menus = new OpMenuBase(); OperationResult res1 = _manager.ExecuteOperation(menus); ViewBag.menus = res1.Items as MenuDto[]; int pageSize = 3; int pageNumber = (page ?? 1); IPagedList <PostDto> posts = postCategories.Posts.ToPagedList(pageNumber, pageSize); postCategories.ListaPaged = posts; return(View(postCategories)); }
public void AddPostCategoriesService(PostCategoriesDTO entity) { PostCategories postCategories = new PostCategories { PostCategoryName = entity.PostCategoryName, PostCategoryPhoto = entity.PostCategoryPhoto }; postCategoriesRepository.Add(postCategories); }
public async Task <ServiceResponse <Post> > UpdatePost(int postId, AddPostDto postToBeUpdated) { ServiceResponse <Post> response = new ServiceResponse <Post>(); try { var post = await FindPostByIdAsync(postId); if (post == null) { response.Message = "Post not found"; } post.Title = postToBeUpdated.Title; post.Summary = postToBeUpdated.Summary; post.Body = postToBeUpdated.Body; post.LastModified = postToBeUpdated.LastModified; if (!String.IsNullOrEmpty(postToBeUpdated.Category)) { var query = await _categoryService.FindCategoryByName(postToBeUpdated.Category); if (!query.Success) { response.Message = "There has been a problem retrieving the category"; } var category = query.Data.FirstOrDefault(); var currentPostCategory = post.PostCategories .FirstOrDefault(); post.PostCategories.Remove(currentPostCategory); var postCategory = new PostCategories { Post = post, Category = category }; _context.PostCategories.Update(postCategory); } await _context.SaveChangesAsync(); response.Data = _mapper.Map <Post>(post); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
/// <summary> /// Refreshes the current model. /// </summary> public void Refresh() { if (Post != null) { if (!Post.IsNew) { Post = Piranha.Models.Post.GetSingle(Post.Id, true); Permalink = Permalink.GetSingle(Post.PermalinkId); Category.GetByPostId(Post.Id).ForEach(c => PostCategories.Add(c.Id)); Categories = new MultiSelectList(Category.GetFields("category_id, category_name", new Params() { OrderBy = "category_name" }), "Id", "Name", PostCategories); } Template = PostTemplate.GetSingle(Post.TemplateId); GetRelated(); } }
public async Task <ServiceResponse <GetPostDto> > AddPostCategory(AddPostCategoryDto newPostCategory) { ServiceResponse <GetPostDto> response = new ServiceResponse <GetPostDto>(); try { Post post = await _context.Posts .FirstOrDefaultAsync(c => c.PostId == newPostCategory.PostId); if (post == null) { response.Success = false; response.Message = "Post not found."; return(response); } Category category = await _context.Categories .FirstOrDefaultAsync(s => s.CategoryId == newPostCategory.CategoryId); if (category == null) { response.Success = false; response.Message = "Category not found."; return(response); } PostCategories postCategory = new PostCategories { Post = post, Category = category }; await _context.PostCategories.AddAsync(postCategory); await _context.SaveChangesAsync(); response.Data = _mapper.Map <GetPostDto>(post); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public ActionResult Custom(int?page, int id) { OpPostSelect select = new OpPostSelect(); select.Postdto.CategoryID = id; OperationResult result = _manager.ExecuteOperation(select); OpCategoriesSelect opCategories = new OpCategoriesSelect(); OperationResult res = _manager.ExecuteOperation(opCategories); PostCategories postCategories = new PostCategories(); postCategories.Categories = res.Items as CategoryDto[]; postCategories.Posts = result.Items as PostDto[]; int pageSize = 3; int pageNumber = (page ?? 1); IPagedList <PostDto> posts = postCategories.Posts.ToPagedList(pageNumber, pageSize); postCategories.ListaPaged = posts; return(View(postCategories)); }
/// <summary> /// Wraps the returning posts into Post objects. Uses JSON.net for deserialization /// </summary> /// <param name="site">the site url. insert without http:// prefix</param> /// <param name="type">the type of posts that shall be returned (post or page)</param> /// <param name="status">the status of posts that shall be returned</param> /// <param name="number">the number of posts that shall be returned</param> /// <param name="offset">the 0-indexed offset for the request. Default value goes to 0. Use this parameter for pagination.</param> /// <returns>List of all posts that matching the query</returns> public async Task <PostsList> GetPostList(string site, PostType type, PostStatus status, int?number = null, int?offset = null) { PostsList post_list = new PostsList(); var response = await getPosts(site, type, status, number, offset); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var responseString = await response.Content.ReadAsStringAsync(); post_list = JsonConvert.DeserializeObject <PostsList>(responseString); if (post_list.posts_total_count != 0) { foreach (var item in post_list.posts_list) { //getting categories as string but handled as object to keep deserializing of posts possible if (item.categories != null) { var cat_object = item.categories; item.categories = PostCategories.GetString(cat_object); } //getting tags as string but handled as object to keep deserializing of posts possible if (item.tags != null) { var tags_object = item.tags; item.tags = PostTags.GetString(tags_object); } //getting attachments as List but handled as object to keep deserializing of posts possible if (item.attachments != null) { var attachments_obj = item.attachments; item.attachments = PostAttachments.GetList(attachments_obj); } //getting metadata as List but handled as object to keep deserializing of posts possible if (item.metadata != null) { var metadata_obj = item.metadata; item.metadata = PostMetaData.GetList(metadata_obj); } } } else { Debug.WriteLine("WARNING: GetPostList returned 0 results. Wrong parameters?"); } } else if (response.StatusCode != System.Net.HttpStatusCode.OK) { var responseString = await response.Content.ReadAsStringAsync(); var Error = JsonConvert.DeserializeObject <apiError>(responseString); Debug.WriteLine(string.Format("ERROR on GetPostList: The site returned: {0}. JetPack not installed on WordPress or JetPack JSON API not active?", Error.message)); } return(post_list); }
public async Task <ServiceResponse <Post> > CreatePost(AddPostDto newPost) { ServiceResponse <Post> response = new ServiceResponse <Post>(); try { var author = await _authorService.FindAuthorById(newPost.AuthorId); var post = new Post() { Title = newPost.Title, Summary = newPost.Summary, Body = newPost.Body, LastModified = newPost.LastModified }; _context.Posts.Add(post); post.Author = author; await _context.SaveChangesAsync(); //now checks which category has been specified if (!string.IsNullOrEmpty(newPost.Category)) { ////finds the category object that corresponds to the category name received ////just one category is added from the frontend var query = await _categoryService.FindCategoryByName(newPost.Category); if (!query.Success) { response.Message = "There has been a problem retrieving the category"; } var category = query.Data.FirstOrDefault(); var postCategory = new PostCategories { Post = post, Category = category }; _context.PostCategories.Add(postCategory); } else { // //assign the default category as General that is the category with Id 1 var category = await _context.Categories.Where(c => c.CategoryId == 1) .ToListAsync(); var postCategory = new PostCategories { Post = post, Category = category.FirstOrDefault() }; _context.PostCategories.Add(postCategory); } await _context.SaveChangesAsync(); response.Data = _mapper.Map <Post>(newPost); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new SuetiaeBloggDbContext( serviceProvider.GetRequiredService < DbContextOptions <SuetiaeBloggDbContext> >())) { if (context.Posts.Any()) { return; // DB has been seeded } var category1 = new Category { Name = "General" }; var category2 = new Category { Name = "Event" }; var category3 = new Category { Name = "Sverige" }; var category4 = new Category { Name = "Nationaldag" }; var category5 = new Category { Name = "Historia" }; context.Categories.AddRange(category1, category2, category3, category4, category5); var tag1 = new Tag { Name = "General" }; var tag2 = new Tag { Name = "Väder" }; var tag3 = new Tag { Name = "Natur" }; context.Tags.AddRange(tag1, tag2, tag3); var author1 = new Author { FirstName = "Anna" }; var author2 = new Author { FirstName = "Evelyn" }; var author3 = new Author { FirstName = "Niloufar" }; context.Authors.AddRange(author1, author2, author3); var post1 = new Post { Author = author1, Title = "This is the first post with complete model and author", Summary = "This is what I want to see in the homepage", Body = "Write something here!", }; var post2 = new Post { Author = author2, Title = "This is the first post with complete model and author", Summary = "This is what I want to see in the homepage", Body = "Write something here!", }; var postCategory1 = new PostCategories { Post = post1, Category = category1 }; var postTag1 = new PostTags { Post = post1, Tag = tag1 }; var postTag2 = new PostTags { Post = post1, Tag = tag2 }; var post3 = new Post { Author = new Author { FirstName = "AlbertoM" }, Title = "This is the second post with complete model and author", Summary = "This is what I want to see in the homepage", Body = "Alla Västtrafiks expressbussar till Göteborg ska byta namn och utseende." + "Först ut är Gul, Blå och Rosa express som får nya namn och rutter redan i december. " + "Förändringar som inte tas emot väl av alla." + "– Det känns som ett misslyckande i planeringen" + "säger Anna Andrén som är en av dem som drabbas.!", }; var post4 = new Post { Author = author3, Title = "This is the third post with complete model and author", Summary = "This is what I want to see in the homepage", Body = "Flera av stadens anställda varnar för att smittspridningen åter tagit fart inom stadens verksamheter" + ". Även fast antalet smittade fortfarande är lågt jämfört med sommarens siffror, så uppmanar nu Babbs Edberg, " + "stadsdelsdirektör i Majorna-Linné och Centrum, till att ta riskerna på allvar.", }; var comment1 = new Comment { Body = "I would like to write something mre original but anyway this a comment in the first post", Author = author3, Post = post1 }; var comment2 = new Comment { Body = "I think the comment goes with every post.", Author = author1, Post = post1 }; var comment3 = new Comment { Body = "I completely disagree, it is a complotto!", Author = author2, Post = post3 }; var comment4 = new Comment { Body = "ok, loooks nice. Where is the author name for the comment?", Author = author1, Post = post4 }; var postCategory2 = new PostCategories { Post = post1, Category = category2 }; var postCategory3 = new PostCategories { Post = post1, Category = category3 }; var postCategory4 = new PostCategories { Post = post2, Category = category4 }; var postCategory5 = new PostCategories { Post = post2, Category = category5 }; var postCategory6 = new PostCategories { Post = post2, Category = category2 }; var postCategory7 = new PostCategories { Post = post3, Category = category4 }; var postCategory8 = new PostCategories { Post = post3, Category = category2 }; var postCategory9 = new PostCategories { Post = post3, Category = category3 }; var postCategory10 = new PostCategories { Post = post4, Category = category1 }; var postTag3 = new PostTags { Post = post2, Tag = tag2 }; var postTag4 = new PostTags { Post = post3, Tag = tag3 }; var postTag5 = new PostTags { Post = post3, Tag = tag2 }; //context.Comments.AddRange(comment1, comment2, comment3, comment4); context.PostCategories.AddRange(postCategory1, postCategory2, postCategory3); context.PostCategories.AddRange(postCategory4, postCategory5, postCategory6); context.PostCategories.AddRange(postCategory7, postCategory8, postCategory9, postCategory10); context.PostTags.AddRange(postTag1, postTag2, postTag3, postTag4, postTag5); context.AddRange(post1, post2, post3, post4); context.SaveChanges(); } }
/// <summary> /// Saves the model. /// </summary> /// <returns>Weather the action was successful</returns> public bool SaveAll(bool draft = true) { using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) { try { bool permalinkfirst = Post.IsNew; // Save permalink before the post if this is an insert if (permalinkfirst) { // Permalink if (Permalink.IsNew) { Permalink.Name = Permalink.Generate(Post.Title); } Permalink.Save(tx); } // Post if (draft) { Post.Save(tx); } else { Post.SaveAndPublish(tx); } // Save permalink after the post if this is an update if (!permalinkfirst) { Permalink.Save(tx); } // Properties Properties.ForEach(p => { p.IsDraft = true; p.Save(tx); if (!draft) { if (Property.GetScalar("SELECT COUNT(property_id) FROM property WHERE property_id=@0 AND property_draft=0", p.Id) == 0) { p.IsNew = true; } p.IsDraft = false; p.Save(tx); } }); // Update categories Relation.DeleteByDataId(Post.Id, tx, true); List <Relation> relations = new List <Relation>(); PostCategories.ForEach(pc => relations.Add(new Relation() { DataId = Post.Id, RelatedId = pc, Type = Relation.RelationType.POSTCATEGORY }) ); relations.ForEach(r => r.Save(tx)); // Publish categories if (!draft) { Relation.DeleteByDataId(Post.Id, tx, false); relations.ForEach(r => { r.IsDraft = false; r.IsNew = true; }); relations.ForEach(r => r.Save(tx)); } tx.Commit(); } catch { tx.Rollback(); throw; } } return(true); }