Browse Source

Merge pull request #43 from jrtechs/PinnedPosts

Pinned posts
pull/46/head
Jeffery Russell 6 years ago
committed by GitHub
parent
commit
2db359e5dd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 34 deletions
  1. +21
    -0
      blog/sidebar.js
  2. +2
    -0
      docs/sqlConfig.md
  3. +0
    -8
      includes/html/incorrectHost.html
  4. +14
    -0
      templates/admin/adminPosts.html
  5. +12
    -0
      templates/blog/sideBar.html
  6. +58
    -26
      utils/sql.js

+ 21
- 0
blog/sidebar.js View File

@ -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)
{

+ 2
- 0
docs/sqlConfig.md View File

@ -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,

+ 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"/>

+ 12
- 0
templates/blog/sideBar.html View File

@ -34,3 +34,15 @@
</div>
<br>
<div class="container">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<h5 class="mb-1">Pinned Posts</h5>
</a>
{for pinnedPost in pinnedPosts}
<a class="list-group-item" href='{pinnedPost.url}'>{pinnedPost.name}<br></a>
{/for}
</div>
</div>
<br>

+ 58
- 26
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,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);
},

Loading…
Cancel
Save