Skip to content
Snippets Groups Projects
Commit f33d6e20 authored by abouchtita's avatar abouchtita
Browse files

Ajout de la suppression d'une tâche (endpoint DELETE /api/tasks/:id)

parent a76df305
No related branches found
No related tags found
1 merge request!1Develop into main
Pipeline #47832 passed
...@@ -45,6 +45,16 @@ app.post('/api/tasks', (req, res) => { ...@@ -45,6 +45,16 @@ app.post('/api/tasks', (req, res) => {
} }
}); });
app.delete('/api/tasks/:id', (req, res) => {
const id = req.params.id;
const result = taskService.deleteTask(id);
if (result) {
res.status(204).send();
} else {
res.status(404).json({ error: 'Tâche non trouvée' });
}
});
// Fonction pour démarrer le serveur // Fonction pour démarrer le serveur
const startServer = () => { const startServer = () => {
const server = app.listen(port, () => { const server = app.listen(port, () => {
......
...@@ -28,9 +28,20 @@ function addTask(description) { ...@@ -28,9 +28,20 @@ function addTask(description) {
return newTask; return newTask;
} }
function deleteTask(id) {
const taskId = parseInt(id, 10);
const index = tasks.findIndex(task => task.id === taskId);
if (index === -1) {
return false;
}
tasks.splice(index, 1);
return true;
}
//export des fonctions //export des fonctions
module.exports = { module.exports = {
getAllTasks, getAllTasks,
getTaskById, getTaskById,
addTask addTask,
deleteTask
}; };
...@@ -65,3 +65,28 @@ describe('POST /api/tasks', () => { ...@@ -65,3 +65,28 @@ describe('POST /api/tasks', () => {
}); });
}); });
describe('DELETE /api/tasks/:id', () => {
test('should delete an existing task and return 204', async () => {
const addResponse = await request(app)
.post('/api/tasks')
.send({ description: "Task to delete via DELETE endpoint" });
expect(addResponse.statusCode).toBe(201);
const taskId = addResponse.body.id;
const deleteResponse = await request(app).delete(`/api/tasks/${taskId}`);
expect(deleteResponse.statusCode).toBe(204);
const getResponse = await request(app).get(`/api/tasks/${taskId}`);
expect(getResponse.statusCode).toBe(404);
});
test('should return 404 when trying to delete a non-existent task', async () => {
const response = await request(app).delete('/api/tasks/99999');
expect(response.statusCode).toBe(404);
expect(response.body).toEqual({ error: 'Tâche non trouvée' });
});
});
\ No newline at end of file
//test unitaire des fonctions de service //test unitaire des fonctions de service
// Import de la fonction à tester // Import de la fonction à tester
const { getAllTasks, getTaskById, addTask } = require('../../src/taskService'); const { getAllTasks, getTaskById, addTask, deleteTask } = require('../../src/taskService');
//Test de la fonction de récupération de toutes les tâches. //Test de la fonction de récupération de toutes les tâches.
test('should return all tasks', () => { test('should return all tasks', () => {
...@@ -33,3 +33,20 @@ test('should add a new task with provided description', () => { ...@@ -33,3 +33,20 @@ test('should add a new task with provided description', () => {
test('should throw an error if description is missing', () => { test('should throw an error if description is missing', () => {
expect(() => addTask()).toThrow("La description est requise"); expect(() => addTask()).toThrow("La description est requise");
}); });
test('should delete a task with a valid id', () => {
const initialLength = getAllTasks().length;
const newTask = addTask("Task to delete");
expect(getAllTasks().length).toBe(initialLength + 1);
const result = deleteTask(newTask.id);
expect(result).toBe(true);
expect(getAllTasks().length).toBe(initialLength);
});
test('should return false when deleting a non-existent task', () => {
const result = deleteTask(999);
expect(result).toBe(false);
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment