Commit 60f84e30 authored by Inès Yeterian's avatar Inès Yeterian
Browse files

Merge branch 'url/year/title' into 'master'

Url/year/title

See merge request !4
parents 9ccb0181 926086e9
......@@ -3,13 +3,27 @@ var fs = require("fs");
var path = require("path");
var { celebrate } = require("celebrate");
var mongoose = require("mongoose");
var { newToucan, validId, validGet, validTitle } = require("../utils/schema");
var { newToucan, validId, validGet, validTitle, validURL} = require("../utils/schema");
var env = require("../.env");
var isLogged = require("../utils/authentification");
var upload = require("../utils/fileSaver");
var Toucan = require("../models/modelToucan");
var router = express.Router();
/* const urltoucans = Toucan.aggregate([
{$addFields: {titlelc: {$split: [{$toLower:"$title"}, " "]}}},
{$addFields:{year:{$dateToString: {format: "%Y", date: "$date"}}}},
{$addFields:
{titlelcnospace:
{$reduce:
{input: "$titlelc",initialValue: "",
in: { $concat : ["$$value", "$$this"]}}} }},
{$addFields:
{urlId:
{$concat:["$titlelcnospace","-","$year"]}}},
{$unset:["titlelc","year","titlelcnospace"]}
]); */
router.route("/toucans")
/**
* Une route qui renvoie un json avec tous les toucans, si limit est défini et vaut n, on renvoie les n derniers toucans
......@@ -124,20 +138,30 @@ router.route("/img/:id")
}
});
router.route("/pdf/:title")
.get(celebrate({params: validTitle}), function(req,res) {
Toucan.findOne({title: req.params.title}, (err,data) => {
if (err) {
res.end(500);
return;
} else if (!data) {
res.status(404).send("Toucan non trouvé");
return;
}
const id = data._id;
res.sendFile(path.resolve(env.savedExtensions[1].path,id+".pdf"));
}
);});
router.get('/pdf/:year/:title', (req, res) => {
var title = req.params.title;
var year = parseInt(req.params.year);
var nextyear = year + 1;
var title = new RegExp((title.split("-")).join("."));
console.log(title);
Toucan.find(
{
title : { $regex: title, $options : 'i' },
date : {
$gte: new Date(year+"-01-01T00:00:00.000Z"),
$lt: new Date(nextyear+"-01-01T00:00:00.000Z"),
}
})
.exec(function (err, toucan) {
if (err) {res.send(err);}
else {
const id = toucan[0]._id;
res.sendFile(path.resolve(env.savedExtensions[1].path,id+".pdf"));
}
});
});
module.exports = router;
\ No newline at end of file
......@@ -13,11 +13,19 @@ var validTitle = Joi.object({
.regex(/^[a-zA-Z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ._\s'-]{1,60}$/) //Autorise la plupart des acents
.required(),
});
var validId = Joi.object({id: Joi.string().hex().length(24).required()});
var validGet = Joi.object({
limit: Joi.number().integer(),
before: Joi.date().timestamp(),
after: Joi.date().timestamp()
});
module.exports = { newToucan, validTitle, validId, validGet };
\ No newline at end of file
var validURL = Joi.object({
urlID: Joi.string()
.regex(/^[a-z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœ._'-]{1,60}\d{4}$/) //
.required()
});
module.exports = { newToucan, validTitle, validId, validURL, validGet };
\ No newline at end of file
......@@ -7,6 +7,14 @@ import ToucanCard from './ToucanCard'
const date = new Date()
const beginYear = 1900 + (date.getMonth() < 7 ? date.getYear()-1 : date.getYear());
function formToucanUrlId(toucan){
const urlId = (new Date(toucan.date)).getFullYear()+
"/"+
toucan.title.toLowerCase().replace(/\s/g, "-");
console.log(urlId);
return urlId;
}
function AllToucan(props){
const [toucans,setToucans] = useState([]);
const [before,setBefore] = useState((new Date(beginYear+1,6,31)).getTime());
......@@ -47,9 +55,10 @@ function AllToucan(props){
/>
<Card.Group centered >
{toucans.map( toucan => {
console.log(formToucanUrlId(toucan))
return <ToucanCard
image={`${props.backURL}/toucan/img/${toucan["_id"]}`}
link={`${props.backURL}/toucan/pdf/${toucan.title}`}
link={`${props.backURL}/toucan/pdf/${formToucanUrlId(toucan)}`}
header={toucan.title}
toucanId={toucan._id}
isAdmin={props.isAdmin}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment