From 42f26227690d84cdf5d93f74b5215ba695d787c7 Mon Sep 17 00:00:00 2001 From: rob Date: Fri, 31 Dec 2021 02:09:39 -0500 Subject: [PATCH] featured posts and home page rendering --- app/controllers/admin/post.js | 2 +- app/controllers/home.js | 2 +- app/services/post.js | 21 ++++++++- app/views/admin/post/index.pug | 3 ++ app/views/comment/components/comment.pug | 2 +- app/views/index.pug | 52 +++++++-------------- app/views/post/components/featured-item.pug | 12 +++++ app/views/post/components/list-item.pug | 21 +++++++++ 8 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 app/views/post/components/featured-item.pug create mode 100644 app/views/post/components/list-item.pug diff --git a/app/controllers/admin/post.js b/app/controllers/admin/post.js index 6642427..771bb53 100644 --- a/app/controllers/admin/post.js +++ b/app/controllers/admin/post.js @@ -82,7 +82,7 @@ class PostController extends SiteController { const { post: postService } = this.dtp.services; try { res.locals.pagination = this.getPaginationParameters(req, 20); - res.locals.posts = await postService.getPosts(res.locals.pagination, ['draft', 'published', 'archived']); + res.locals.posts = await postService.getAllPosts(res.locals.pagination); res.render('admin/post/index'); } catch (error) { this.log.error('failed to fetch posts', { error }); diff --git a/app/controllers/home.js b/app/controllers/home.js index 2607107..8a8ba80 100644 --- a/app/controllers/home.js +++ b/app/controllers/home.js @@ -7,7 +7,6 @@ const DTP_COMPONENT_NAME = 'home'; const path = require('path'); -const fs = require('fs'); const express = require('express'); @@ -70,6 +69,7 @@ class HomeController extends SiteController { const { post: postService } = this.dtp.services; try { res.locals.pagination = this.getPaginationParameters(req, 20); + res.locals.featuredPosts = await postService.getFeaturedPosts(3); res.locals.posts = await postService.getPosts(res.locals.pagination); res.render('index'); } catch (error) { diff --git a/app/services/post.js b/app/services/post.js index aad54eb..8d9ef99 100644 --- a/app/services/post.js +++ b/app/services/post.js @@ -94,7 +94,26 @@ class PostService extends SiteService { status = [status]; } const posts = await Post - .find({ status: { $in: status } }) + .find({ status: { $in: status }, 'flags.isFeatured': false }) + .sort({ created: -1 }) + .skip(pagination.skip) + .limit(pagination.cpp) + .lean(); + return posts; + } + + async getFeaturedPosts (maxCount = 3) { + const posts = await Post + .find({ status: 'published', 'flags.isFeatured': true }) + .sort({ created: -1 }) + .limit(maxCount) + .lean(); + return posts; + } + + async getAllPosts (pagination) { + const posts = await Post + .find() .sort({ created: -1 }) .skip(pagination.skip) .limit(pagination.cpp) diff --git a/app/views/admin/post/index.pug b/app/views/admin/post/index.pug index a680e8e..d900b83 100644 --- a/app/views/admin/post/index.pug +++ b/app/views/admin/post/index.pug @@ -30,6 +30,9 @@ block content .uk-width-auto div(uk-grid).uk-grid-small.uk-flex-middle + if post.flags.isFeatured + .uk-width-auto + i(style="color: yellow;").fas.fa-star .uk-width-auto(class={ 'uk-text-info': (post.status === 'draft'), 'uk-text-success': (post.status === 'published'), diff --git a/app/views/comment/components/comment.pug b/app/views/comment/components/comment.pug index a229c38..9c382c2 100644 --- a/app/views/comment/components/comment.pug +++ b/app/views/comment/components/comment.pug @@ -4,7 +4,7 @@ mixin renderComment (comment) header.uk-comment-header div(uk-grid).uk-grid-medium.uk-flex-middle .uk-width-auto - if comment.author.picture.small + if comment.author.picture && comment.author.picture.small img(src= `/image/${comment.author.picture.small._id}`).site-profile-picture.sb-small.uk-comment-avatar else img(src="/img/default-member.png").site-profile-picture.sb-small.uk-comment-avatar diff --git a/app/views/index.pug b/app/views/index.pug index 4362d55..c8a489b 100644 --- a/app/views/index.pug +++ b/app/views/index.pug @@ -2,42 +2,22 @@ extends layouts/main-sidebar block content include components/page-sidebar - - mixin renderBlogPostListItem (post, postIndex = 1, postIndexModulus = 3) - a(href=`/post/${post.slug}`).uk-display-block.uk-link-reset - div(uk-grid).uk-grid-small - - div(class='uk-visible@m', class={ - 'uk-flex-first': ((postIndex % postIndexModulus) === 0), - 'uk-flex-last': ((postIndex % postIndexModulus) !== 0), - }).uk-width-1-3 - img(src="/img/default-poster.jpg").responsive - - div(class='uk-width-1-1 uk-width-2-3@m', class={ - 'uk-flex-first': ((postIndex % postIndexModulus) !== 0), - 'uk-flex-last': ((postIndex % postIndexModulus) === 0), - }) - article.uk-article - h4(style="line-height: 1.1;").uk-article-title.uk-margin-small= post.title - .uk-article-meta - if post.updated - span updated: #{moment(post.updated).format("MMM DD YYYY HH:MM a")} - else - span published: #{moment(post.created).format("MMM DD YYYY HH:MM a")} - - +renderSectionTitle('Featured') - - .uk-margin - div(style="position: relative; overflow: hidden; width: 100%; padding-top: 56.25%") - iframe( - src="https://tv.gab.com/channel/mrjoeprich/embed/what-is-just-joe-radio-61ad9b2165a83d20e95a465d", - width="960", - height="540", - style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%;", - ) - - if featuredEmbed - div.dtp-featured-embed!= featuredEmbed + include post/components/list-item + include post/components/featured-item + + if Array.isArray(featuredPosts) && (featuredPosts.length > 0) + +renderSectionTitle('Featured') + + - var topPost = featuredPosts.shift(); + .uk-margin + +renderBlogPostFeaturedItem(topPost) + + if (featuredPosts.length > 0) + .uk-margin + div(uk-grid).uk-grid-small + each post in featuredPosts + .uk-width-1-2 + +renderBlogPostFeaturedItem(post) //- Blog Posts +renderSectionTitle('Blog Posts') diff --git a/app/views/post/components/featured-item.pug b/app/views/post/components/featured-item.pug new file mode 100644 index 0000000..7171da7 --- /dev/null +++ b/app/views/post/components/featured-item.pug @@ -0,0 +1,12 @@ +mixin renderBlogPostFeaturedItem (post) + a(href=`/post/${post.slug}`).uk-display-block.uk-link-reset + div(class='uk-visible@m').uk-margin-small + img(src="/img/default-poster.jpg").responsive + + article.uk-article + h4(style="line-height: 1.1;").uk-article-title.uk-margin-small= post.title + .uk-article-meta + if post.updated + span updated: #{moment(post.updated).format("MMM DD YYYY HH:MM a")} + else + span published: #{moment(post.created).format("MMM DD YYYY HH:MM a")} \ No newline at end of file diff --git a/app/views/post/components/list-item.pug b/app/views/post/components/list-item.pug new file mode 100644 index 0000000..15f9884 --- /dev/null +++ b/app/views/post/components/list-item.pug @@ -0,0 +1,21 @@ +mixin renderBlogPostListItem (post, postIndex = 1, postIndexModulus = 3) + a(href=`/post/${post.slug}`).uk-display-block.uk-link-reset + div(uk-grid).uk-grid-small + + div(class='uk-visible@m', class={ + 'uk-flex-first': ((postIndex % postIndexModulus) === 0), + 'uk-flex-last': ((postIndex % postIndexModulus) !== 0), + }).uk-width-1-3 + img(src="/img/default-poster.jpg").responsive + + div(class='uk-width-1-1 uk-width-2-3@m', class={ + 'uk-flex-first': ((postIndex % postIndexModulus) !== 0), + 'uk-flex-last': ((postIndex % postIndexModulus) === 0), + }) + article.uk-article + h4(style="line-height: 1.1;").uk-article-title.uk-margin-small= post.title + .uk-article-meta + if post.updated + span updated: #{moment(post.updated).format("MMM DD YYYY HH:MM a")} + else + span published: #{moment(post.created).format("MMM DD YYYY HH:MM a")} \ No newline at end of file