From 5c5cc39acb1c4dd3f4f762b30fde40376b2ff4ea Mon Sep 17 00:00:00 2001 From: abouchtita <abouchtita@devops-tp.local> Date: Fri, 14 Mar 2025 17:33:53 +0100 Subject: [PATCH] Ajout de l'endpoint POST /api/tasks et de la fonction addTask --- src/index.js | 13 +++++++++++++ src/taskService.js | 17 ++++++++++++++++- tests/integration/app.test.js | 22 ++++++++++++++++++++++ tests/unit/todoService.test.js | 19 +++++++++++++++++-- 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index d2ff431..94fa182 100644 --- a/src/index.js +++ b/src/index.js @@ -32,6 +32,19 @@ app.get('/api/tasks/:id', (req, res) => { } }); +app.post('/api/tasks', (req, res) => { + const { description } = req.body; + if (!description) { + return res.status(400).json({ error: 'La description est requise' }); + } + try { + const newTask = taskService.addTask(description); + res.status(201).json(newTask); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + // Fonction pour démarrer le serveur const startServer = () => { const server = app.listen(port, () => { diff --git a/src/taskService.js b/src/taskService.js index 19298d6..d9eaefc 100644 --- a/src/taskService.js +++ b/src/taskService.js @@ -14,8 +14,23 @@ function getAllTasks() { return tasks; } +function addTask(description) { + if (!description) { + throw new Error("La description est requise"); + } + const newId = tasks.reduce((max, task) => task.id > max ? task.id : max, 0) + 1; + const newTask = { + id: newId, + description: description, + done: false + }; + tasks.push(newTask); + return newTask; +} + //export des fonctions module.exports = { getAllTasks, - getTaskById + getTaskById, + addTask }; diff --git a/tests/integration/app.test.js b/tests/integration/app.test.js index dd7eb43..79bdebe 100644 --- a/tests/integration/app.test.js +++ b/tests/integration/app.test.js @@ -42,4 +42,26 @@ describe('GET /api/tasks/:id', () => { expect(res.body).toEqual({ error: 'Tâche non trouvée' }); }); +}); + +describe('POST /api/tasks', () => { + + test('should create a new task and return it', async () => { + const response = await request(app) + .post('/api/tasks') + .send({ description: "Nouvelle tâche" }); + expect(response.statusCode).toBe(201); + expect(response.body).toHaveProperty('id'); + expect(response.body.description).toBe("Nouvelle tâche"); + expect(response.body.done).toBe(false); + }); + + test('should return 400 if description is missing', async () => { + const response = await request(app) + .post('/api/tasks') + .send({}); + expect(response.statusCode).toBe(400); + expect(response.body).toHaveProperty('error', 'La description est requise'); + }); + }); \ No newline at end of file diff --git a/tests/unit/todoService.test.js b/tests/unit/todoService.test.js index 9c0157a..a5409b4 100644 --- a/tests/unit/todoService.test.js +++ b/tests/unit/todoService.test.js @@ -1,7 +1,7 @@ //test unitaire des fonctions de service // Import de la fonction à tester -const { getAllTasks , getTaskById} = require('../../src/taskService'); +const { getAllTasks, getTaskById, addTask } = require('../../src/taskService'); //Test de la fonction de récupération de toutes les tâches. test('should return all tasks', () => { @@ -17,4 +17,19 @@ test('should return the correct task for a given id', () => { test('should return undefined for a non-existent task id', () => { const task = getTaskById(999); - expect(task).toBeUndefined();}); \ No newline at end of file + expect(task).toBeUndefined(); +}); + +test('should add a new task with provided description', () => { + const initialLength = getAllTasks().length; + const description = "Nouvelle tâche"; + const newTask = addTask(description); + expect(newTask).toHaveProperty('id'); + expect(newTask.description).toBe(description); + expect(newTask.done).toBe(false); + expect(getAllTasks().length).toBe(initialLength + 1); +}); + +test('should throw an error if description is missing', () => { + expect(() => addTask()).toThrow("La description est requise"); +}); \ No newline at end of file -- GitLab