FunctionProject/api/routes/admin.js

199 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-03-25 16:23:43 +01:00
const { Router } = require('express');
const fileUpload = require('express-fileupload');
const { body } = require('express-validator');
2020-03-25 16:23:43 +01:00
const adminController = require('../controllers/admin');
const isAuth = require('../middlewares/isAuth');
const isAdmin = require('../middlewares/isAdmin');
const Functions = require('../models/functions');
const Categories = require('../models/categories');
2020-03-25 16:23:43 +01:00
const AdminRouter = Router();
AdminRouter.route('/functions')
// Récupère les fonctions
.get(isAuth, isAdmin, adminController.getFunctions)
// Permet de créé une fonction
.post(isAuth, isAdmin,
fileUpload({
useTempFiles: true,
safeFileNames: true,
preserveExtension: Number,
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
parseNested: true
}),
[
body('title')
.not()
.isEmpty()
.withMessage("La fonction doit avoir un titre.")
.isLength({ max: 100 })
.withMessage("Le titre est trop long.")
.custom(((title) => {
if (title === 'undefined') {
return Promise.reject("La fonction doit avoir un titre.");
}
return true;
})),
body('slug')
.not()
.isEmpty()
.withMessage("La fonction doit avoir un slug.")
.isLength({ max: 100 })
.withMessage("Le slug est trop long.")
.custom(((slug) => {
if (slug === 'undefined') {
return Promise.reject("La fonction doit avoir un slug.");
}
return true;
}))
.custom((async (slug) => {
try {
const FunctionSlug = await Functions.findOne({ where: { slug } });
if (FunctionSlug) {
return Promise.reject("Le slug existe déjà...");
}
} catch (error) {
console.log(error);
}
return true;
})),
body('description')
.not()
.isEmpty()
.withMessage("La fonction doit avoir une description.")
.isLength({ max: 255, min: 1 })
.withMessage("La description est trop longue.")
.custom(((description) => {
if (description === 'undefined') {
return Promise.reject("La fonction doit avoir une description.");
}
return true;
})),
body('categorieId')
.not()
.isEmpty()
.withMessage("La fonction doit avoir une catégorie.")
.custom(async (categorieId) => {
try {
const categorieFound = await Categories.findOne({ where: { id: parseInt(categorieId) } });
if (!categorieFound) {
return Promise.reject("La catégorie n'existe pas!");
}
} catch (error) {
console.log(error);
}
return true;
}),
body('type')
.custom((type) => {
if (!(type === 'article' || type === 'form' || type === 'page')) {
return Promise.reject('Le type de la fonction peut être : article, form ou page.');
}
return true;
})
], adminController.postFunction);
AdminRouter.route('/functions/:slug')
2020-03-25 16:23:43 +01:00
// Récupère les informations d'une fonction
.get(isAuth, isAdmin, adminController.getFunctionBySlug);
AdminRouter.route('/functions/:id')
// Modifie information basique d'une fonction
2020-04-13 22:11:34 +02:00
.put(isAuth, isAdmin,
fileUpload({
useTempFiles: true,
safeFileNames: true,
preserveExtension: Number,
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
parseNested: true
}),
[
body('title')
.not()
.isEmpty()
.withMessage("La fonction doit avoir un titre.")
.isLength({ max: 100 })
.withMessage("Le titre est trop long.")
.custom(((title) => {
if (title === 'undefined') {
return Promise.reject("La fonction doit avoir un titre.");
}
return true;
})),
body('slug')
.not()
.isEmpty()
.withMessage("La fonction doit avoir un slug.")
.isLength({ max: 100 })
.withMessage("Le slug est trop long.")
.custom(((slug) => {
if (slug === 'undefined') {
return Promise.reject("La fonction doit avoir un slug.");
}
return true;
})),
body('description')
.not()
.isEmpty()
.withMessage("La fonction doit avoir une description.")
.isLength({ max: 255, min: 1 })
.withMessage("La description est trop longue.")
.custom(((description) => {
if (description === 'undefined') {
return Promise.reject("La fonction doit avoir une description.");
}
return true;
})),
body('categorieId')
.not()
.isEmpty()
.withMessage("La fonction doit avoir une catégorie.")
.custom(async (categorieId) => {
try {
const categorieFound = await Categories.findOne({ where: { id: parseInt(categorieId) } });
if (!categorieFound) {
return Promise.reject("La catégorie n'existe pas!");
}
} catch (error) {
console.log(error);
}
return true;
}),
body('type')
.custom((type) => {
if (!(type === 'article' || type === 'form' || type === 'page')) {
return Promise.reject('Le type de la fonction peut être : article, form ou page.');
}
return true;
})
], adminController.putFunction)
// Supprime une fonction avec son id
.delete(isAuth, isAdmin, adminController.deleteFunction);
2020-03-25 23:30:33 +01:00
AdminRouter.route('/functions/article/:id')
.put(isAuth, isAdmin, adminController.putFunctionArticle);
AdminRouter.route('/functions/form/:id')
.put(isAuth, isAdmin, adminController.putFunctionForm);
2020-04-12 22:38:13 +02:00
AdminRouter.route('/categories')
// Crée une catégorie
.post(isAuth, isAdmin, adminController.postCategory);
AdminRouter.route('/categories/:id')
// Modifier une catégorie avec son id
.put(isAuth, isAdmin, adminController.putCategory)
// Supprime une catégorie avec son id
2020-04-12 22:38:13 +02:00
.delete(isAuth, isAdmin, adminController.deleteCategory);
2020-03-25 16:23:43 +01:00
module.exports = AdminRouter;