From 9c49040f65fd714a0221e68ec8009f66612a7451 Mon Sep 17 00:00:00 2001 From: Willtock Date: Tue, 13 Mar 2018 10:05:44 +0100 Subject: [PATCH 1/4] Ajout CRUD des tags --- backend/src/api/index.js | 1 + backend/src/api/tags/index.js | 15 ++++++++ backend/src/api/tags/tag.controller.js | 52 ++++++++++++++++++++++++++ backend/src/api/tags/tag.model.js | 10 +++++ 4 files changed, 78 insertions(+) create mode 100644 backend/src/api/tags/index.js create mode 100644 backend/src/api/tags/tag.controller.js create mode 100644 backend/src/api/tags/tag.model.js diff --git a/backend/src/api/index.js b/backend/src/api/index.js index 7208907..31951c8 100644 --- a/backend/src/api/index.js +++ b/backend/src/api/index.js @@ -5,5 +5,6 @@ const router = new Router(); router.use('/documents', require('./documents')); router.use('/categories', require('./categories')); router.use('/users', require('./users')); +router.use('/tags', require('./tags')); module.exports = router; diff --git a/backend/src/api/tags/index.js b/backend/src/api/tags/index.js new file mode 100644 index 0000000..942ae3b --- /dev/null +++ b/backend/src/api/tags/index.js @@ -0,0 +1,15 @@ +const { Router } = require('express'); + +const router = new Router(); + +const controller = require('./tag.controller'); + +router.get('/', controller.getAll); + +router.get('/:id', controller.getOne); + +router.post('/', controller.create); + +router.delete('/:id', controller.delete); + +module.exports = router; diff --git a/backend/src/api/tags/tag.controller.js b/backend/src/api/tags/tag.controller.js new file mode 100644 index 0000000..d5356da --- /dev/null +++ b/backend/src/api/tags/tag.controller.js @@ -0,0 +1,52 @@ +const Tag = require('./tag.model'); + +module.exports = {}; + +module.exports.getAll = (req, res) => { + Tag.find({}, (err, tags) => { + if (err) { + return res.status(500).json(err); + } + return res.json(tags); + }); +}; + +module.exports.create = (req, res) => { + const tag = new Tag(req.body); + tag.save((err) => { + if (err) { + return res.status(500).json(err); + } + return res.status(201).json(tag); + }); +}; + +module.exports.getOne = (req, res) => { + Tag.findOne( + { _id: req.params.id }, + (err, tag) => { + if (err) { + return res.status(500).json(err); + } + if (!tag) { + return res.status(404).json({ + code: 'TAG_NOT_FOUND', + message: 'L\'utilisateur n\'a pas pu être trouvé', + }); + } + return res.status(200).json(tag); + }, + ); +}; + +module.exports.delete = (req, res) => { + Tag.deleteOne( + { _id: req.params.id }, + (err) => { + if (err) { + return res.status(500).json(err); + } + return res.status(204).end(); + }, + ); +}; diff --git a/backend/src/api/tags/tag.model.js b/backend/src/api/tags/tag.model.js new file mode 100644 index 0000000..9cfe552 --- /dev/null +++ b/backend/src/api/tags/tag.model.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); + +const TagSchema = new mongoose.Schema({ + name: { + type: String, + required: true, + }, +}); + +module.exports = mongoose.model('Tag', TagSchema); From 7ce5eae4ea094914fe676a6f83b5ebb2decb41c2 Mon Sep 17 00:00:00 2001 From: Hazegard Date: Tue, 13 Mar 2018 11:07:49 +0100 Subject: [PATCH 2/4] add tags apidoc --- backend/src/api/tags/index.js | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/backend/src/api/tags/index.js b/backend/src/api/tags/index.js index 942ae3b..36aa416 100644 --- a/backend/src/api/tags/index.js +++ b/backend/src/api/tags/index.js @@ -4,12 +4,80 @@ const router = new Router(); const controller = require('./tag.controller'); +/** + * @api {get} /tags Get all tags + * @apiName GetTags + * @apiGroup Tags + * @apiDescription Cette URL affiche un JSON contenant tous les tags + * + * @apiSuccessExample {json} Success-Response: + *[ + * { + * "_id": "5aa79c3dac53d23f45960e82", + * "name": "SQL", + * "__v": 0 + * }, + * { + * "_id": "5aa79c91ac53d23f45960e83", + * "name": "Javascript", + * "__v": 0 + * } + *] + */ router.get('/', controller.getAll); +/** + * @api {get} /tag/:id Get a tag + * @apiName GetOneTag + * @apiGroup Tags + * @apiDescription Cette URL affiche un JSON contenant le tag + * correspondant à l'ID + * + * @apiParam {String} id ID du tag à afficher + * @apiParamExample {String} Request-Example: + * id: 5aa79c3dac53d23f45960e82 + * + * @apiSuccessExample {json} Success-Response: + * { + * "_id": "5aa79c3dac53d23f45960e82", + * "name": "SQL", + * "__v": 0 + * } + */ router.get('/:id', controller.getOne); +/** + * @api {post} /users Create a tag + * @apiName PostOneTag + * @apiGroup Tags + * @apiDescription Crée un tag et l'ajoute dans la BDD + * + * @apiParam {String} name Nom du tag + * @apiParamExample {json} Request-Example: + * { + * "name": "Jean-Victor", + * } + * + * @apiSuccessExample {json} Success-Response: + * { + * "name": "Jean-Victor", + * "creationTime": "2018-03-06T15:08:46.039Z", + * "_id": "5a9eaefe3597423cb1c4376e", + * "__v": 0 + * } + */ router.post('/', controller.create); +/** + * @api {delete} /tags/:id Delete a tag + * @apiName DeleteOneTag + * @apiGroup Users + * @apiDescription Supprime le tag correspondant à l'ID + * + * @apiParam {String} id ID du tag à supprimer + * @apiParamExample {String} Request-Example: + * id: 5aa79c3dac53d23f45960e82 + */ router.delete('/:id', controller.delete); module.exports = router; From 4a7d1c8380a9379f476c349a2abf447f761fa939 Mon Sep 17 00:00:00 2001 From: Hazegard Date: Wed, 14 Mar 2018 17:17:59 +0100 Subject: [PATCH 3/4] migrate from callback to promise --- backend/src/api/tags/tag.controller.js | 49 ++++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/backend/src/api/tags/tag.controller.js b/backend/src/api/tags/tag.controller.js index d5356da..932bd6b 100644 --- a/backend/src/api/tags/tag.controller.js +++ b/backend/src/api/tags/tag.controller.js @@ -2,41 +2,42 @@ const Tag = require('./tag.model'); module.exports = {}; +const getAll = () => Tag.find({}); +const getTagById = id => Tag.findOne({ _id: id }); + module.exports.getAll = (req, res) => { - Tag.find({}, (err, tags) => { - if (err) { - return res.status(500).json(err); - } - return res.json(tags); - }); + getAll() + .then(tags => res.status(200) + .json(tags)) + .catch(err => res.status(500) + .json(err)); }; module.exports.create = (req, res) => { const tag = new Tag(req.body); tag.save((err) => { if (err) { - return res.status(500).json(err); + return res.status(500) + .json(err); } - return res.status(201).json(tag); + return res.status(201) + .json(tag); }); }; module.exports.getOne = (req, res) => { - Tag.findOne( - { _id: req.params.id }, - (err, tag) => { - if (err) { - return res.status(500).json(err); - } + getTagById(req.params.id) + .then((tag) => { if (!tag) { - return res.status(404).json({ - code: 'TAG_NOT_FOUND', - message: 'L\'utilisateur n\'a pas pu être trouvé', - }); + return res.status(404) + .json({ + code: 'TAG_NOT_FOUND', + message: 'L\'utilisateur n\'a pas pu être trouvé', + }); } - return res.status(200).json(tag); - }, - ); + return res.status(200) + .json(tag); + }); }; module.exports.delete = (req, res) => { @@ -44,9 +45,11 @@ module.exports.delete = (req, res) => { { _id: req.params.id }, (err) => { if (err) { - return res.status(500).json(err); + return res.status(500) + .json(err); } - return res.status(204).end(); + return res.status(204) + .end(); }, ); }; From 00085234199e0b0d50328dcad8c549d1ef435d16 Mon Sep 17 00:00:00 2001 From: Ekar Date: Mon, 19 Mar 2018 08:54:59 +0100 Subject: [PATCH 4/4] move tags in resources --- backend/src/api/index.js | 2 +- backend/src/api/{ => resources}/tags/index.js | 0 backend/src/api/{ => resources}/tags/tag.controller.js | 0 backend/src/api/{ => resources}/tags/tag.model.js | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename backend/src/api/{ => resources}/tags/index.js (100%) rename backend/src/api/{ => resources}/tags/tag.controller.js (100%) rename backend/src/api/{ => resources}/tags/tag.model.js (100%) diff --git a/backend/src/api/index.js b/backend/src/api/index.js index 78924af..271d601 100644 --- a/backend/src/api/index.js +++ b/backend/src/api/index.js @@ -5,7 +5,7 @@ const router = new Router(); router.use('/documents', require('./resources/documents')); router.use('/categories', require('./resources/categories')); router.use('/users', require('./resources/users')); -router.use('/tags', require('./tags')); +router.use('/tags', require('./resources/tags')); router.use('/search', require('./search')); router.use('/download', require('./download')); diff --git a/backend/src/api/tags/index.js b/backend/src/api/resources/tags/index.js similarity index 100% rename from backend/src/api/tags/index.js rename to backend/src/api/resources/tags/index.js diff --git a/backend/src/api/tags/tag.controller.js b/backend/src/api/resources/tags/tag.controller.js similarity index 100% rename from backend/src/api/tags/tag.controller.js rename to backend/src/api/resources/tags/tag.controller.js diff --git a/backend/src/api/tags/tag.model.js b/backend/src/api/resources/tags/tag.model.js similarity index 100% rename from backend/src/api/tags/tag.model.js rename to backend/src/api/resources/tags/tag.model.js