Develop get categorie and improve db select query
This commit is contained in:
parent
fbcffec3c2
commit
27bbd490ea
|
@ -1,6 +1,7 @@
|
|||
const log = require('color-logs')(true, true, "Categories.class.js");
|
||||
const log = require('color-logs')(true, true, "Categorie.class.js");
|
||||
|
||||
var title,
|
||||
var id,
|
||||
title,
|
||||
uri,
|
||||
description,
|
||||
articles = [];
|
||||
|
@ -13,12 +14,17 @@ class Categorie {
|
|||
* @param {string} uri : uri of categorie
|
||||
* @param {string} description : description of categorie
|
||||
*/
|
||||
constructor(title, uri, description){
|
||||
constructor(id, title, uri, description){
|
||||
this.setId(id);
|
||||
this.setTitle(title);
|
||||
this.setUri(uri);
|
||||
this.setDescription(description);
|
||||
}
|
||||
|
||||
setId(id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
setTitle(title){
|
||||
this.title = title;
|
||||
}
|
||||
|
@ -39,6 +45,10 @@ class Categorie {
|
|||
this.articles.push(articles);
|
||||
}
|
||||
|
||||
getId(){
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string} title of categorie
|
||||
|
|
|
@ -3,7 +3,8 @@ const log = require('color-logs')(true, true, "Categories.class.js"),
|
|||
hbs = require('handlebars'),
|
||||
promise = require('promise'),
|
||||
dbConnector = require('../../lib/db/'+config.db+"/db.js");
|
||||
fs = require("fs");
|
||||
fs = require("fs"),
|
||||
Categorie = require('../Categorie.class.js');
|
||||
|
||||
hbs.registerHelper('listArticle', function(tabs, options) {
|
||||
var i = 1;
|
||||
|
@ -52,14 +53,11 @@ class Categories {
|
|||
return nav;
|
||||
});
|
||||
var template = hbs.compile(source);
|
||||
log.Debug(result)
|
||||
var html = template(result);
|
||||
var html = template(JSON.parse(result));
|
||||
resolve(html);
|
||||
});
|
||||
});
|
||||
log.Info("work");
|
||||
}).catch((err) => {
|
||||
log.error(err);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
@ -70,14 +68,29 @@ class Categories {
|
|||
* @returns {Promise} data of categories
|
||||
*/
|
||||
getCategorie(slug){
|
||||
return new Promise((resolve, reject) => {
|
||||
var path = "./ressources/"+slug+"/params.json";
|
||||
fs.access(path, fs.constants.F_OK || fs.constants.R_OK, (error) => {
|
||||
if(error)
|
||||
resolve(new Error("File not exist"));
|
||||
else
|
||||
resolve(require("."+path));
|
||||
var db = new dbConnector();
|
||||
return db.get({
|
||||
"select" : {
|
||||
"categories" : [
|
||||
"id",
|
||||
"title",
|
||||
"uri",
|
||||
"description"
|
||||
]
|
||||
},
|
||||
"condition" : {
|
||||
"uri" : slug
|
||||
},
|
||||
"limit" : ""
|
||||
}).then((result) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
var data = JSON.parse(result);
|
||||
result = null;
|
||||
var categorie = new Categorie(data[0].id, data[0].title, data[0].uri, data[0].description);
|
||||
resolve(categorie);
|
||||
});
|
||||
}).catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,9 @@ class rethinkdb{
|
|||
*/
|
||||
var formatedQuery = r.table(Object.keys(query.select)[0]);
|
||||
|
||||
if(query.condition !== ""){
|
||||
formatedQuery = formatedQuery.filter(query.condition);
|
||||
}
|
||||
/**
|
||||
* select collumn of table
|
||||
*/
|
||||
|
@ -43,15 +46,27 @@ class rethinkdb{
|
|||
/**
|
||||
* run query
|
||||
*/
|
||||
formatedQuery.run(dbConnexion, (err, result) => {
|
||||
formatedQuery.run(dbConnexion).then((cursor) => {
|
||||
return cursor.toArray();
|
||||
}).then((result) => {
|
||||
resolve(JSON.stringify(result));
|
||||
}).catch((err)=>{
|
||||
throw err;
|
||||
log.error(err);
|
||||
});
|
||||
/*formatedQuery.run(dbConnexion, (err, result) => {
|
||||
if(err) throw err;
|
||||
var jsonReturn = [];
|
||||
console.log(typeof result);
|
||||
result.each(function(err, row) {
|
||||
if (err) throw err;
|
||||
log.debug(row);
|
||||
jsonReturn.push(row);
|
||||
//console.log(i);
|
||||
});
|
||||
resolve(jsonReturn);
|
||||
});
|
||||
|
||||
});*/
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -57,31 +57,34 @@ router.get("/", function(req, res){
|
|||
|
||||
|
||||
});
|
||||
/*router.get("/:slug", function(req, res){
|
||||
|
||||
router.get("/:uri/", function(req, res){
|
||||
var categories = new Categories();
|
||||
Promise.all([
|
||||
template.getNav(),
|
||||
categories.getListArticles(req.params.slug),
|
||||
categories.getCategorie(req.params.slug)
|
||||
categories.getNav(),
|
||||
categories.getCategorie(req.params.uri)
|
||||
/*categories.getListArticles(req.params.uri),
|
||||
categories.getCategorie(req.params.slug)*/
|
||||
]).then(data => {
|
||||
var nav = data[0];
|
||||
var content = data[1];
|
||||
var dataCategories = data[2];
|
||||
var categorie = data[1];
|
||||
/*var content = data[1];
|
||||
var dataCategories = data[2];*/
|
||||
res.setHeader("Content-type", "text/html");
|
||||
var tplData = {
|
||||
"blogName": config.blogName,
|
||||
"title": dataCategories.title+" - "+ config.blogName,
|
||||
"blogName": config.siteTitle,
|
||||
"title": categorie.getTitle() + " - "+config.siteHomeTitle,
|
||||
"nav": nav,
|
||||
"content": content,
|
||||
"content": '',
|
||||
"disclaimer": ''
|
||||
};
|
||||
res.render('template.hbs', tplData);
|
||||
}).catch(err => {
|
||||
res.setHeader("Content-type", "text/plain");
|
||||
|
||||
log.debug(err);
|
||||
res.end("erreur");
|
||||
});
|
||||
});*/
|
||||
});
|
||||
|
||||
/*router.get("/:categories/:slug", function(req, res){
|
||||
var categories = new Categories();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<nav>
|
||||
|
||||
{{#listNav data}}<div class="navItem"><a href="{{slug}}">{{title}}</a></div>{{/listNav}}
|
||||
{{#listNav data}}<div class="navItem"><a href="{{uri}}/">{{title}}</a></div>{{/listNav}}
|
||||
<!--<div class="navItem"><a href="index.html">Accueil</a></div>
|
||||
<div class="navItem"><a href="portfolio.html">Portfolio</a></div>
|
||||
<div class="navItem">Contact</div>-->
|
||||
|
|
Reference in New Issue