Browse Source

Updated admin site to set pinned posts.

pull/43/head
jrtechs 6 years ago
parent
commit
6a2a29fedb
3 changed files with 71 additions and 63 deletions
  1. +0
    -8
      includes/html/incorrectHost.html
  2. +14
    -0
      templates/admin/adminPosts.html
  3. +57
    -55
      utils/sql.js

+ 0
- 8
includes/html/incorrectHost.html View File

@ -1,8 +0,0 @@
<div class="row p-lg-0">
<h1 class="align-content-center">Incorrect Host name</h1>
<p>Someone is trying to rip off my website.</p>
<div class="align-content-center">
<img src="https://jrtechs.net/img/website/404.jpg" alt="Page not found" width="40%" />
</div>
<p>Click <a href="https://jrtechs.net">here</a> to go to the genuine site.</p>
</div>

+ 14
- 0
templates/admin/adminPosts.html View File

@ -20,6 +20,12 @@
<input class="form-control" type="date" name="edit_date" value='{post.published}' required>
<label class="w3-label w3-validate">Published Date</label>
</div>
<div class="form-group">
<input class="" type="checkbox" value="" name="pinned_checkbox" id="pinnedCheckBox" {if post.pinned}checked{/if}>
<label class="form-check-label" for="pinnedCheckBox">
Pinned
</label>
</div>
<div>
<input type="submit" name="submit" value="Edit" class="btn btn-lg btn-secondary"/>
</div>
@ -42,6 +48,7 @@
<td>Name</td>
<td>Header Picture</td>
<td>Date</td>
<td>Pinned</td>
<td>Edit</td>
</tr>
</thead>
@ -52,6 +59,13 @@
<td>{post.name}</td>
<td>{post.picture_url}</td>
<td>{post.published}</td>
<td>
{if post.pinned}
True
{else}
False
{/if}
</td>
<td>
<form action="/admin/posts" method ="post" >
<input type="submit" name="submit" value="Edit" class="btn btn-secondary"/>

+ 57
- 55
utils/sql.js View File

@ -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,74 +245,32 @@ module.exports=
"by post_id desc limit 10";
fetch(q).then(function(sqlPosts)
{
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)
fetchWithCategoryInformation(sqlPosts).then(function(data)
{
resolve(goodies);
});
resolve(data);
})
});
});
},
/**
* Returns a list of all the pinned posts in the database.
*
* @returns {Promise}
*/
getPinnedPosts: function()
{
return new Promise(function(resolve, reject)
{
var q = "select name,url, category_id from posts where pinned=1 order " +
"by post_id desc limit 10";
console.log(q);
fetch(q).then(function (sqlPosts) {
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);
});
});
});
},
/**
* TODO
* @returns {*|Promise}
*/
getPopularPosts: function()
{
return new Promise(function(resolve, reject)
{
var q = "select * from popular_posts";
fetch(q).then(function(sqlPosts)
{
fetchWithCategoryInformation(sqlPosts).then(function(data)
{
resolve(data);
})
});
});
},
@ -468,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);
},

Loading…
Cancel
Save