Get article meta data
This commit is contained in:
parent
0eed7176ba
commit
f394d86369
|
@ -7,11 +7,17 @@ var id,
|
|||
author,
|
||||
content,
|
||||
date,
|
||||
img,
|
||||
slug;
|
||||
|
||||
class Article {
|
||||
constructor(id, title, author, date, slug){
|
||||
|
||||
constructor(id, title, author, date, img, slug){
|
||||
this.setId(id);
|
||||
this.setTitle(title);
|
||||
this.setAuthor(author);
|
||||
this.setDate(date);
|
||||
this.setImg(img);
|
||||
this.setSlug(slug);
|
||||
}
|
||||
|
||||
setId(id){
|
||||
|
@ -30,6 +36,10 @@ class Article {
|
|||
this.content = content;
|
||||
}
|
||||
|
||||
setImg(img){
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
setDate(date){
|
||||
this.date = date;
|
||||
}
|
||||
|
@ -58,6 +68,10 @@ class Article {
|
|||
return this.date;
|
||||
}
|
||||
|
||||
getImg(){
|
||||
return this.img;
|
||||
}
|
||||
|
||||
getSlug(){
|
||||
return this.slug;
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ const log = require('color-logs')(true, true, "Categorie.class.js");
|
|||
var id,
|
||||
title,
|
||||
uri,
|
||||
description,
|
||||
articles = [];
|
||||
articles,
|
||||
description;
|
||||
|
||||
class Categorie {
|
||||
|
||||
|
@ -19,6 +19,7 @@ class Categorie {
|
|||
this.setTitle(title);
|
||||
this.setUri(uri);
|
||||
this.setDescription(description);
|
||||
this.articles = new Array();
|
||||
}
|
||||
|
||||
setId(id){
|
||||
|
@ -39,10 +40,10 @@ class Categorie {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param {object <Article>} articles : array of article object owned by this categorie
|
||||
* @param {object <Article>} article : array of article object owned by this categorie
|
||||
*/
|
||||
setArticles(articles){
|
||||
this.articles.push(articles);
|
||||
setArticle(article){
|
||||
this.articles.push(article);
|
||||
}
|
||||
|
||||
getId(){
|
||||
|
|
|
@ -4,13 +4,15 @@ const log = require('color-logs')(true, true, "Categories.class.js"),
|
|||
promise = require('promise'),
|
||||
dbConnector = require('../../lib/db/'+config.db+"/db.js");
|
||||
fs = require("fs"),
|
||||
Categorie = require('../Categorie.class.js');
|
||||
Categorie = require('../Categorie.class.js');
|
||||
Article = require('../Article.class.js');
|
||||
|
||||
hbs.registerHelper('listArticle', function(tabs, options) {
|
||||
var i = 1;
|
||||
var out = '';
|
||||
|
||||
for(var x in options['data']['root']){
|
||||
|
||||
//log.debug(options.fn(options['data']['root'][x]));
|
||||
out = out + "" + options.fn(options['data']['root'][x]) + "";
|
||||
if(i == 3){
|
||||
i= 1;
|
||||
|
@ -94,6 +96,49 @@ class Categories {
|
|||
});
|
||||
}
|
||||
|
||||
getListArticles(categorie){
|
||||
var db = new dbConnector();
|
||||
return db.get({
|
||||
"select" : {
|
||||
"articles" : [
|
||||
"id",
|
||||
"title",
|
||||
"slug",
|
||||
"author",
|
||||
"date",
|
||||
"img"
|
||||
]
|
||||
},
|
||||
"condition" : {
|
||||
"categorie_id" : categorie.getId()
|
||||
},
|
||||
"limit" : ""
|
||||
}).then((result) => {
|
||||
return new Promise((resolve) => {
|
||||
var data = JSON.parse(result);
|
||||
for(var i in data){
|
||||
categorie.setArticle(new Article(data[i].id,data[i].title, data[i].author, data[i].date, data[i].img, data[i].slug))
|
||||
}
|
||||
log.debug(categorie);
|
||||
resolve(this.listArticlesToHTM(categorie));
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
listArticlesToHTM(categorie){
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile('./views/articles/list.hbs', 'utf-8', (error, source) => {
|
||||
hbs.registerHelper('articles', (articles) =>{
|
||||
return articles;
|
||||
});
|
||||
var template = hbs.compile(source);
|
||||
var html = template(categorie.getArticles());
|
||||
resolve(html);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Promise} the list of article
|
||||
|
@ -132,7 +177,7 @@ class Categories {
|
|||
* @param {type} slug : url of categorie
|
||||
* @returns {Promise} the list of article in this categorie
|
||||
*/
|
||||
getListArticles(slug){
|
||||
/*getListArticles(slug){
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile('./views/articles/list.hbs', 'utf-8', (error, source) => {
|
||||
var pathOfArticles = "./ressources/"+slug+"";
|
||||
|
@ -178,7 +223,7 @@ class Categories {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -63,26 +63,27 @@ router.get("/:uri/", function(req, res){
|
|||
var categories = new Categories();
|
||||
Promise.all([
|
||||
categories.getNav(),
|
||||
categories.getCategorie(req.params.uri)
|
||||
/*categories.getListArticles(req.params.uri),
|
||||
categories.getCategorie(req.params.slug)*/
|
||||
categories.getCategorie(req.params.uri),
|
||||
]).then(data => {
|
||||
var nav = data[0];
|
||||
var categorie = data[1];
|
||||
categories.getListArticles(categorie).then((result) => {
|
||||
log.debug(result)
|
||||
res.setHeader("Content-type", "text/html");
|
||||
var tplData = {
|
||||
"blogName": config.siteTitle,
|
||||
"title": categorie.getTitle() + " - "+config.siteHomeTitle,
|
||||
"nav": nav,
|
||||
"content": result,
|
||||
"disclaimer": ''
|
||||
};
|
||||
res.render('template.hbs', tplData);
|
||||
})
|
||||
/*var content = data[1];
|
||||
var dataCategories = data[2];*/
|
||||
res.setHeader("Content-type", "text/html");
|
||||
var tplData = {
|
||||
"blogName": config.siteTitle,
|
||||
"title": categorie.getTitle() + " - "+config.siteHomeTitle,
|
||||
"nav": nav,
|
||||
"content": '',
|
||||
"disclaimer": ''
|
||||
};
|
||||
res.render('template.hbs', tplData);
|
||||
|
||||
}).catch(err => {
|
||||
res.setHeader("Content-type", "text/plain");
|
||||
log.debug(err);
|
||||
res.end("erreur");
|
||||
});
|
||||
});
|
||||
|
|
Reference in New Issue