diff --git a/api/app.js b/api/app.js index 5b78647..f5e693d 100644 --- a/api/app.js +++ b/api/app.js @@ -27,6 +27,7 @@ app.use('/admin', require('./routes/admin')); app.use('/favorites', require('./routes/favorites')); app.use('/comments', require('./routes/comments')); app.use('/quotes', require('./routes/quotes')); +app.use('/tasks', require('./routes/tasks')); /* Errors Handling */ app.use((_req, _res, next) => errorHandling(next, { statusCode: 404, message: "La route n'existe pas!" })); // 404 @@ -37,12 +38,13 @@ app.use((error, _req, res, _next) => { }); /* Database Relations */ -const Functions = require('./models/functions'); -const Categories = require('./models/categories'); -const Users = require('./models/users'); -const Favorites = require('./models/favorites'); -const Comments = require('./models/comments'); -const Quotes = require('./models/quotes'); +const Functions = require('./models/functions'); +const Categories = require('./models/categories'); +const Users = require('./models/users'); +const Favorites = require('./models/favorites'); +const Comments = require('./models/comments'); +const Quotes = require('./models/quotes'); +const Tasks = require('./models/tasks'); // A function has a category Categories.hasOne(Functions, { constraints: true, onDelete: 'CASCADE'}); @@ -64,6 +66,10 @@ Comments.belongsTo(Functions, { constraints: false }); Users.hasMany(Quotes); Quotes.belongsTo(Users, { constraints: false }); +// Users can have tasks +Users.hasMany(Tasks); +Tasks.belongsTo(Users, { constraints: false }); + /* Server */ // sequelize.sync({ force: true }) sequelize.sync() diff --git a/api/controllers/tasks.js b/api/controllers/tasks.js new file mode 100644 index 0000000..e747ec2 --- /dev/null +++ b/api/controllers/tasks.js @@ -0,0 +1,68 @@ +const errorHandling = require('../assets/utils/errorHandling'); +const { serverError, requiredFields } = require('../assets/config/errors'); +const Tasks = require('../models/tasks'); + +exports.getTasks = async (req, res, next) => { + try { + const tasks = await Tasks.findAll({ + where: { + userId: req.userId + }, + order: [['createdAt', 'DESC']] + }); + return res.status(200).json(tasks); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} + +exports.postTask = async (req, res, next) => { + const { task } = req.body; + try { + if (!task) { + return errorHandling(next, requiredFields); + } + const taskResult = await Tasks.create({ task, userId: req.userId }); + return res.status(201).json(taskResult); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} + +exports.putTask = async (req, res, next) => { + const { id } = req.params; + const { isCompleted } = req.body; + try { + if (typeof isCompleted !== 'boolean') { + return errorHandling(next, { message: "isCompleted doit être un booléen.", statusCode: 400 }); + } + + const taskResult = await Tasks.findOne({ where: { id, userId: req.userId } }); + if (!taskResult) { + return errorHandling(next, { message: `La "tâche à faire" n'existe pas.`, statusCode: 404 }); + } + taskResult.isCompleted = isCompleted; + const taskSaved = await taskResult.save(); + return res.status(200).json(taskSaved); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} + +exports.deleteTask = async (req, res, next) => { + const { id } = req.params; + try { + const taskResult = await Tasks.findOne({ where: { id, userId: req.userId } }); + if (!taskResult) { + return errorHandling(next, { message: `La "tâche à faire" n'existe pas.`, statusCode: 404 }); + } + await taskResult.destroy(); + return res.status(200).json({ message: `La "tâche à faire" a bien été supprimée!` }); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} \ No newline at end of file diff --git a/api/models/tasks.js b/api/models/tasks.js new file mode 100644 index 0000000..ff74671 --- /dev/null +++ b/api/models/tasks.js @@ -0,0 +1,20 @@ +const Sequelize = require('sequelize'); +const sequelize = require('../assets/utils/database'); + +module.exports = sequelize.define('task', { + id: { + type: Sequelize.INTEGER, + allowNull: false, + autoIncrement: true, + primaryKey: true + }, + task: { + type: Sequelize.STRING, + allowNull: false, + }, + isCompleted: { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: 0 + } +}); \ No newline at end of file diff --git a/api/routes/tasks.js b/api/routes/tasks.js new file mode 100644 index 0000000..0f7c721 --- /dev/null +++ b/api/routes/tasks.js @@ -0,0 +1,23 @@ +const { Router } = require('express'); +const tasksController = require('../controllers/tasks'); +const isAuth = require('../middlewares/isAuth'); + +const TasksRouter = Router(); + +TasksRouter.route('/') + + // Récupère les tâches à faire d'un user + .get(isAuth, tasksController.getTasks) + + // Poster une nouvelle tâche à faire + .post(isAuth, tasksController.postTask); + +TasksRouter.route('/:id') + + // Permet de mettre une tâche à faire en isCompleted ou !isCompleted + .put(isAuth, tasksController.putTask) + + // Supprimer une tâche à faire + .delete(isAuth, tasksController.deleteTask); + +module.exports = TasksRouter; \ No newline at end of file