diff --git a/blog/sidebar.js b/blog/sidebar.js index ac82c1a..55950fc 100644 --- a/blog/sidebar.js +++ b/blog/sidebar.js @@ -45,6 +45,26 @@ const getInformationForCategories = function(templateContext) }; +const getInformationForPinnedPosts = function(templateContext) +{ + return new Promise(function(resolve, reject) + { + sql.getPinnedPosts().then(function(posts) + { + posts.forEach(function(p) + { + p.url = '/' + p.category + '/' + p.url; + }); + templateContext.pinnedPosts = posts; + resolve(); + }).catch(function(error) + { + reject(error); + }) + }); +}; + + module.exports= { main: function(templateContext) @@ -53,6 +73,7 @@ module.exports= { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), getInformationForRecentPosts(templateContext), + getInformationForPinnedPosts(templateContext), getInformationForCategories(templateContext)]) .then(function(content) { diff --git a/docs/sqlConfig.md b/docs/sqlConfig.md index cc438d6..867fdb2 100644 --- a/docs/sqlConfig.md +++ b/docs/sqlConfig.md @@ -29,9 +29,11 @@ picture_url varchar(100) not null, published datetime not null, name varchar(100) not null, url varchar(100) not null, +pinned BIT, primary key(post_id) ); +ALTER TABLE posts ADD pinned BIT; create table downloads( download_id mediumint unsigned not null AUTO_INCREMENT, diff --git a/includes/html/incorrectHost.html b/includes/html/incorrectHost.html deleted file mode 100644 index 3822599..0000000 --- a/includes/html/incorrectHost.html +++ /dev/null @@ -1,8 +0,0 @@ -
-

Incorrect Host name

-

Someone is trying to rip off my website.

-
- Page not found -
-

Click here to go to the genuine site.

-
\ No newline at end of file diff --git a/templates/admin/adminPosts.html b/templates/admin/adminPosts.html index d49ebc8..dfe40e9 100644 --- a/templates/admin/adminPosts.html +++ b/templates/admin/adminPosts.html @@ -20,6 +20,12 @@ +
+ + +
@@ -42,6 +48,7 @@ Name Header Picture Date + Pinned Edit @@ -52,6 +59,13 @@ {post.name} {post.picture_url} {post.published} + + {if post.pinned} + True + {else} + False + {/if} +
diff --git a/templates/blog/sideBar.html b/templates/blog/sideBar.html index e79eb2f..7565656 100644 --- a/templates/blog/sideBar.html +++ b/templates/blog/sideBar.html @@ -34,3 +34,15 @@
+
+
+ +
Pinned Posts
+
+ {for pinnedPost in pinnedPosts} + {pinnedPost.name}
+ {/for} +
+
+
+ diff --git a/utils/sql.js b/utils/sql.js index 183eaeb..6e48364 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -56,6 +56,43 @@ const fetch = function(sqlStatement) }; +/** + * Helper function which fetches the category url for all the + * posts returned in the posts table and appends them to the + * posts json objects. + * + * @param sqlPosts + * @returns {Promise} + */ +const fetchWithCategoryInformation = function(sqlPosts) +{ + return new Promise(function(resolve, reject) + { + var promises = []; + sqlPosts.forEach(function(post) + { + promises.push(new Promise(function(res, rej) + { + var getCategory = "select url from categories where " + + "category_id='" + post.category_id + "'"; + fetch(getCategory).then(function(urls) + { + var obj = new Object(); + obj.name = post.name; + obj.url = post.url; + obj.category = urls[0].url; + res(obj); + }); + })); + }); + Promise.all(promises).then(function(goodies) + { + resolve(goodies); + }); + }); +}; + + module.exports= { /** @@ -192,7 +229,6 @@ module.exports= return fetch("select * from posts order by post_id desc"); }, - /** * Helper method which returns a list of objects which contains the url * and name of thee ten most recent posts @@ -209,44 +245,32 @@ module.exports= "by post_id desc limit 10"; fetch(q).then(function(sqlPosts) { - var promises = []; - sqlPosts.forEach(function(post) + fetchWithCategoryInformation(sqlPosts).then(function(data) { - promises.push(new Promise(function(res, rej) - { - var getCategory = "select url from categories where " + - "category_id='" + post.category_id + "'"; - fetch(getCategory).then(function(urls) - { - var obj = new Object(); - obj.name = post.name; - obj.url = post.url; - obj.category = urls[0].url; - res(obj); - }); - })); - }); - Promise.all(promises).then(function(goodies) - { - resolve(goodies); - }); + resolve(data); + }) }); }); }, /** - * TODO - * @returns {*|Promise} + * Returns a list of all the pinned posts in the database. + * + * @returns {Promise} */ - getPopularPosts: function() + getPinnedPosts: function() { return new Promise(function(resolve, reject) { - var q = "select * from popular_posts"; + var q = "select name,url, category_id from posts where pinned=1 order " + + "by post_id desc limit 10"; fetch(q).then(function(sqlPosts) { - + fetchWithCategoryInformation(sqlPosts).then(function(data) + { + resolve(data); + }) }); }); }, @@ -438,14 +462,22 @@ module.exports= { const url = postData.edit_name_new.split(" ").join("-").toLowerCase(); + console.log(postData); + + var pinned = ("pinned_checkbox" in postData) == false ? "NULL": "1"; + console.log(pinned); + const q = "update posts " + "set category_id='" + postData.edit_cat_num + "' " + ",name='" + postData.edit_name_new + "' " + ",url='" + url + "' " + ",picture_url='" + postData.edit_pic + "' " + ",published='" + postData.edit_date + "' " + + ",pinned=" + pinned+ " where post_id='" + postData.edit_post_2 + "'"; + console.log(q); + return module.exports.insert(q); },