Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Ajout CRUD des tags #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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('./resources/tags'));
router.use('/search', require('./search'));
router.use('/download', require('./download'));

Expand Down
83 changes: 83 additions & 0 deletions backend/src/api/resources/tags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const { Router } = require('express');

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;
55 changes: 55 additions & 0 deletions backend/src/api/resources/tags/tag.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Tag = require('./tag.model');

module.exports = {};

const getAll = () => Tag.find({});
const getTagById = id => Tag.findOne({ _id: id });

module.exports.getAll = (req, res) => {
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(201)
.json(tag);
});
};

module.exports.getOne = (req, res) => {
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(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();
},
);
};
10 changes: 10 additions & 0 deletions backend/src/api/resources/tags/tag.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose');

const TagSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
});

module.exports = mongoose.model('Tag', TagSchema);