diff --git a/src/index.js b/src/index.js index d2ff43150c5839fd8c7db0557f21d4b80e32760e..94fa182a3cec7e9e2c69df3f64d1f6ce0a7b9717 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 19298d624d6c53bb8e1d205cdd6e6378a1003c27..d9eaefc2c9e6a236f6354495e2bfc79941f70631 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 dd7eb43b2f2f05edf1bc5293a48e70ad3dfa41f4..79bdebee3c0f1ec93b53a8f1842ad369defdbd4c 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 9c0157aa80c50d41129d0df3b3aab499c4e55da2..a5409b434cae156f7af2a0ea9f30a6f84733501e 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