📦 NEW: backend: Tasks
This commit is contained in:
parent
de44b1db98
commit
2c69526d28
18
api/app.js
18
api/app.js
@ -27,6 +27,7 @@ app.use('/admin', require('./routes/admin'));
|
|||||||
app.use('/favorites', require('./routes/favorites'));
|
app.use('/favorites', require('./routes/favorites'));
|
||||||
app.use('/comments', require('./routes/comments'));
|
app.use('/comments', require('./routes/comments'));
|
||||||
app.use('/quotes', require('./routes/quotes'));
|
app.use('/quotes', require('./routes/quotes'));
|
||||||
|
app.use('/tasks', require('./routes/tasks'));
|
||||||
|
|
||||||
/* Errors Handling */
|
/* Errors Handling */
|
||||||
app.use((_req, _res, next) => errorHandling(next, { statusCode: 404, message: "La route n'existe pas!" })); // 404
|
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 */
|
/* Database Relations */
|
||||||
const Functions = require('./models/functions');
|
const Functions = require('./models/functions');
|
||||||
const Categories = require('./models/categories');
|
const Categories = require('./models/categories');
|
||||||
const Users = require('./models/users');
|
const Users = require('./models/users');
|
||||||
const Favorites = require('./models/favorites');
|
const Favorites = require('./models/favorites');
|
||||||
const Comments = require('./models/comments');
|
const Comments = require('./models/comments');
|
||||||
const Quotes = require('./models/quotes');
|
const Quotes = require('./models/quotes');
|
||||||
|
const Tasks = require('./models/tasks');
|
||||||
|
|
||||||
// A function has a category
|
// A function has a category
|
||||||
Categories.hasOne(Functions, { constraints: true, onDelete: 'CASCADE'});
|
Categories.hasOne(Functions, { constraints: true, onDelete: 'CASCADE'});
|
||||||
@ -64,6 +66,10 @@ Comments.belongsTo(Functions, { constraints: false });
|
|||||||
Users.hasMany(Quotes);
|
Users.hasMany(Quotes);
|
||||||
Quotes.belongsTo(Users, { constraints: false });
|
Quotes.belongsTo(Users, { constraints: false });
|
||||||
|
|
||||||
|
// Users can have tasks
|
||||||
|
Users.hasMany(Tasks);
|
||||||
|
Tasks.belongsTo(Users, { constraints: false });
|
||||||
|
|
||||||
/* Server */
|
/* Server */
|
||||||
// sequelize.sync({ force: true })
|
// sequelize.sync({ force: true })
|
||||||
sequelize.sync()
|
sequelize.sync()
|
||||||
|
68
api/controllers/tasks.js
Normal file
68
api/controllers/tasks.js
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
20
api/models/tasks.js
Normal file
20
api/models/tasks.js
Normal file
@ -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
|
||||||
|
}
|
||||||
|
});
|
23
api/routes/tasks.js
Normal file
23
api/routes/tasks.js
Normal file
@ -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;
|
Reference in New Issue
Block a user