Browse Source

Created recent posts sidebar

pull/4/head
jrtechs 6 years ago
parent
commit
b8e46f6d01
9 changed files with 141 additions and 91 deletions
  1. +0
    -0
      posts/pages.js
  2. +1
    -0
      posts/posts.js
  3. +10
    -3
      server.js
  4. +10
    -14
      sidebar/categoriesSideBar.js
  5. +27
    -54
      sidebar/popularPosts.js
  6. +33
    -0
      sidebar/recentPosts.js
  7. +2
    -2
      sidebar/sidebar.html
  8. +22
    -18
      sidebar/sidebar.js
  9. +36
    -0
      utils/sql.js

+ 0
- 0
posts/pages.js View File


+ 1
- 0
posts/posts.js View File

@ -21,6 +21,7 @@ var renderPost = function(res, requestURL)
//user entered /category/name/ or /category/name //user entered /category/name/ or /category/name
if(splitURL.length == 3 || splitURL.length == 4) if(splitURL.length == 3 || splitURL.length == 4)
{ {
console.log("importante "+ requestURL)
sql.getPost(requestURL).then(function(post) sql.getPost(requestURL).then(function(post)
{ {
if(post != 0) if(post != 0)

+ 10
- 3
server.js View File

@ -29,15 +29,22 @@ http.createServer(function (request, res)
{ {
var file = ""; var file = "";
if(filename.includes("/categories/")) //single category page
var urlSplit = filename.split("/");
console.log(urlSplit);
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js"; file = "./posts/category.js";
else if(filename.includes("/admin")) //top secret admin page
else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
file = "./admin/admin.js"; file = "./admin/admin.js";
else //single post page
else if(urlSplit.length >= 3)//single post page
file = "./posts/posts.js"; file = "./posts/posts.js";
else //single static page?
file = "./posts/pages.js";
console.log(file); console.log(file);
includes.printHeader(res).then(function() includes.printHeader(res).then(function()

+ 10
- 14
sidebar/categoriesSideBar.js View File

@ -12,20 +12,15 @@ module.exports=
*/ */
main: function(res) main: function(res)
{ {
console.log("sidebar called");
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("<div class=\"w3-card w3-margin w3-margin-top\">");
res.write("<div class=\"w3-card w3-margin\">");
res.write(" <div class=\"w3-container w3-padding\">\n" +
"\n" +
" <h3>Categories</h3>\n" +
"\n" +
" </div>");
res.write("<div class=\"w3-container w3-padding\"><h4>Categories</h4></div>");
res.write("<ul class=\"w3-ul w3-hoverable w3-white\">");
res.write("<div class=\"w3-container w3-white\">");
res.write("<ol>");
sql.getCategories().then(function(categories) sql.getCategories().then(function(categories)
{ {
console.log(categories[0].name); console.log(categories[0].name);
@ -33,15 +28,16 @@ module.exports=
{ {
//res.write(cat.name); //res.write(cat.name);
console.log(cat); console.log(cat);
res.write("<li class=\"w3-padding-16\">");
res.write("<a href='\\category\\" + cat.url + "'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("</li>");
res.write("<a href=\"#\#\" class=\"w3-bar-item w3-button\" style=\"width=100%\">" + cat.name + "</a>");
res.write("<br />");
}); });
res.write("</ol>");
res.write("</div></div>");
res.write("</ul>");
resolve(); resolve();
}) })
}); });
} }
};
};

+ 27
- 54
sidebar/popularPosts.js View File

@ -1,70 +1,43 @@
const Promise = require('promise'); const Promise = require('promise');
const sql = require('../utils/sql');
module.exports= module.exports=
{ {
main: function(res) main: function(res)
{ {
console.log("sidebar called");
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
res.write("Popular posts");
resolve();
})
}
};
/*
<!-- Posts -->
<div class="w3-card w3-margin">
<div class="w3-container w3-padding">
<h4>Popular Posts</h4>
</div>
<ul class="w3-ul w3-hoverable w3-white">
<li class="w3-padding-16">
<span class="w3-large">Lorem</span><br>
<span>Sed mattis nunc</span>
</li>
<li class="w3-padding-16">
res.write("<div class=\"w3-card w3-margin\">");
res.write(" <div class=\"w3-container w3-padding\">\n" +
"\n" +
" <h4>Popular Posts</h4>\n" +
"\n" +
" </div>");
<span class="w3-large">Ipsum</span><br>
<span>Praes tinci sed</span>
res.write("<ul class=\"w3-ul w3-hoverable w3-white\">");
</li>
sql.getCategories().then(function(categories)
{
console.log(categories[0].name);
categories.forEach(function(cat)
{
//res.write(cat.name);
console.log(cat);
res.write("<li class=\"w3-padding-16\">");
//res.write("<a href=\"#\#\" class=\"w3-bar-item w3-button\" style=\"width=100%\">" + cat.name + "</a>");
<li class="w3-padding-16">
res.write("<a href='jrtechs.net'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("</li>");
});
res.write("</ul></div>");
resolve();
})
<span class="w3-large">Dorum</span><br>
<span>Ultricies congue</span>
</li>
<li class="w3-padding-16 w3-hide-medium w3-hide-small">
<span class="w3-large">Mingsum</span><br>
<span>Lorem ipsum dipsum</span>
</li>
</ul>
</div>
<hr>
*/
});
}
};

+ 33
- 0
sidebar/recentPosts.js View File

@ -0,0 +1,33 @@
const Promise = require('promise');
const sql = require('../utils/sql');
module.exports=
{
main: function(res)
{
console.log("sidebar called");
return new Promise(function(resolve, reject)
{
res.write("<div class=\"w3-card w3-margin\">");
res.write("<div class=\"w3-container w3-padding\"><h4>Recent Posts</h4></div>");
res.write("<ul class=\"w3-ul w3-hoverable w3-white\">");
sql.getRecentPosts().then(function(posts)
{
posts.forEach(function(p)
{
var url = '\\' + p.category + '\\' + p.name;
res.write("<li class=\"w3-padding-16\">");
res.write("<a href='" + url + "'><span class=\"w3-large\">" + p.name + "</span><br></a>");
res.write("</li>");
});
res.write("</ul></div>");
resolve();
})
});
}
};

+ 2
- 2
sidebar/sidebar.html View File

@ -2,9 +2,9 @@
<div class="w3-container w3-white"> <div class="w3-container w3-white">
<h4><b>My Name</b></h4>
<h4><b>Jrtechs</b></h4>
<p>Just me, myself and I, exploring the universe of uknownment. I have a heart of love and a interest of lorem ipsum and mauris neque quam blog. I want to share my world with you.</p>
<p>fda</p>
</div> </div>

+ 22
- 18
sidebar/sidebar.js View File

@ -2,24 +2,28 @@ const utils = require('../utils/utils.js');
var Promise = require('promise'); var Promise = require('promise');
module.exports= module.exports=
{
main: function(res)
{ {
return new Promise(function(resolve, reject)
main: function(res)
{ {
res.write("<div class=\"w3-col l4\">");
utils.include(res,"sidebar/sidebar.html").then(function()
{
return require("../sidebar/popularPosts.js").main(res);
}).then(function()
return new Promise(function(resolve, reject)
{ {
return require("../sidebar/categoriesSideBar.js").main(res);
}).then(function()
{
res.write("</div>");
resolve();
});
})
}
};
res.write("<div class=\"w3-col l4\">");
utils.include(res,"sidebar/sidebar.html").then(function()
{
return require("../sidebar/popularPosts.js").main(res);
}).then(function()
{
return require("../sidebar/categoriesSideBar.js").main(res);
}).then(function()
{
res.write("</div>");
resolve();
}).catch(function(err)
{
console.log("hit error");
console.log(err);
})
})
}
};

+ 36
- 0
utils/sql.js View File

@ -117,6 +117,11 @@ module.exports=
return fetch(q); return fetch(q);
}, },
/**
* Function which currently returns all posts of a particular category from the database
* @param requestURL
* @return {*|Promise}
*/
getPostsFromCategory: function(requestURL) getPostsFromCategory: function(requestURL)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
@ -135,5 +140,36 @@ module.exports=
} }
}); });
}); });
},
/**
* Helper method which returns a list of objects which contains the url and name of thee ten most recent posts
*
* {[name: , url: ],[name: , url: ],[name: , url: ],...}
*
* @return {*|Promise}
*/
getRecentPosts: function()
{
return new Promise(function(resolve, reject)
{
var q = "select name, category_id from posts order by post_id desc limit 10";
fetch(q).then(function(sqlPosts)
{
var posts = [];
sqlPosts.forEach(function(p)
{
var getCategory = "select url from categories where category_id='" + p.category_id + "'";
fetch(getCategory).then(function(url)
{
var obj = new Object();
obj.name = p.name;
obj.category = url.url;
posts.push(obj);
})
});
resolve(posts);
});
});
} }
}; };

Loading…
Cancel
Save