2020-08-03 12:04:07 +02:00
|
|
|
const errorHandling = require('../assets/utils/errorHandling')
|
|
|
|
const { serverError, requiredFields } = require('../assets/config/errors')
|
|
|
|
const Tasks = require('../models/tasks')
|
2020-04-23 15:44:39 +02:00
|
|
|
|
|
|
|
exports.getTasks = async (req, res, next) => {
|
2020-08-03 12:04:07 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.postTask = async (req, res, next) => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const { task } = req.body
|
|
|
|
try {
|
|
|
|
if (!task) {
|
|
|
|
return errorHandling(next, requiredFields)
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
const taskResult = await Tasks.create({ task, userId: req.userId })
|
|
|
|
return res.status(201).json(taskResult)
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
return errorHandling(next, serverError)
|
|
|
|
}
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.putTask = async (req, res, next) => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const { id } = req.params
|
|
|
|
const { isCompleted } = req.body
|
|
|
|
try {
|
|
|
|
if (typeof isCompleted !== 'boolean') {
|
2020-08-03 14:14:45 +02:00
|
|
|
return errorHandling(next, {
|
|
|
|
message: 'isCompleted doit être un booléen.',
|
|
|
|
statusCode: 400
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
2020-04-23 15:44:39 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const taskResult = await Tasks.findOne({
|
|
|
|
where: { id, userId: req.userId }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
if (!taskResult) {
|
2020-08-03 14:14:45 +02:00
|
|
|
return errorHandling(next, {
|
|
|
|
message: 'La "tâche à faire" n\'existe pas.',
|
|
|
|
statusCode: 404
|
|
|
|
})
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
taskResult.isCompleted = isCompleted
|
|
|
|
const taskSaved = await taskResult.save()
|
|
|
|
return res.status(200).json(taskSaved)
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
return errorHandling(next, serverError)
|
|
|
|
}
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.deleteTask = async (req, res, next) => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const { id } = req.params
|
|
|
|
try {
|
2020-08-03 14:14:45 +02:00
|
|
|
const taskResult = await Tasks.findOne({
|
|
|
|
where: { id, userId: req.userId }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
if (!taskResult) {
|
2020-08-03 14:14:45 +02:00
|
|
|
return errorHandling(next, {
|
|
|
|
message: 'La "tâche à faire" n\'existe pas.',
|
|
|
|
statusCode: 404
|
|
|
|
})
|
2020-04-23 15:44:39 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
await taskResult.destroy()
|
2020-08-03 14:14:45 +02:00
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.json({ message: 'La "tâche à faire" a bien été supprimée!' })
|
2020-08-03 12:04:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
return errorHandling(next, serverError)
|
|
|
|
}
|
|
|
|
}
|