diff --git a/FETCH_HEAD b/FETCH_HEAD new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/index.js b/src/index.js index 441d6f95ffa4de0bbfe069eb952dd50f3cfdc48f..d2ff43150c5839fd8c7db0557f21d4b80e32760e 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,17 @@ app.get('/api/tasks', (req, res) => { res.json(tasks); }); +// +app.get('/api/tasks/:id', (req, res) => { + const id = req.params.id; + const task = taskService.getTaskById(id); + if (task) { + res.json(task); + } else { + res.status(404).json({ error: 'Tâche non trouvée' }); + } +}); + // Fonction pour démarrer le serveur const startServer = () => { const server = app.listen(port, () => { diff --git a/src/taskService.js b/src/taskService.js index aad0b48d53cc0142b908a33ebbd47eff1da5432c..19298d624d6c53bb8e1d205cdd6e6378a1003c27 100644 --- a/src/taskService.js +++ b/src/taskService.js @@ -4,6 +4,11 @@ const tasks = require('../tasks.json'); +function getTaskById(id) { + const taskId = parseInt(id, 10); + return tasks.find(task => task.id === taskId); +} + //fonction de récupération de toutes les tâches function getAllTasks() { return tasks; @@ -11,5 +16,6 @@ function getAllTasks() { //export des fonctions module.exports = { - getAllTasks + getAllTasks, + getTaskById }; diff --git a/tests/integration/app.test.js b/tests/integration/app.test.js index 04c94c89bbff985d5716ca22aaa24ac2509911ef..dd7eb43b2f2f05edf1bc5293a48e70ad3dfa41f4 100644 --- a/tests/integration/app.test.js +++ b/tests/integration/app.test.js @@ -24,3 +24,22 @@ describe('Endpoints de base', () => { }); }); + + +describe('GET /api/tasks/:id', () => { + + // Test pour un ID existant (par exemple, ID = 1) + test('devrait retourner la tâche correspondante pour un ID existant', async () => { + const res = await request(app).get('/api/tasks/1'); + expect(res.statusCode).toEqual(200); + expect(res.body).toEqual({ id: 1, description: 'Apprendre Git', done: false }); + }); + + // Test pour un ID inexistant (par exemple, ID = 999) + test('devrait retourner 404 pour un ID inexistant', async () => { + const res = await request(app).get('/api/tasks/999'); + expect(res.statusCode).toEqual(404); + expect(res.body).toEqual({ error: 'Tâche non trouvée' }); + }); + +}); \ No newline at end of file diff --git a/tests/unit/todoService.test.js b/tests/unit/todoService.test.js index 503dafd405abeef2020c5acb8fc75754dbd36ad4..9c0157aa80c50d41129d0df3b3aab499c4e55da2 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 } = require('../../src/taskService'); +const { getAllTasks , getTaskById} = require('../../src/taskService'); //Test de la fonction de récupération de toutes les tâches. test('should return all tasks', () => { @@ -10,3 +10,11 @@ test('should return all tasks', () => { expect(tasks.length).toBeGreaterThan(0); }); +test('should return the correct task for a given id', () => { + const task = getTaskById(1); + expect(task).toEqual({ id: 1, description: 'Apprendre Git', done: false }); +}); + +test('should return undefined for a non-existent task id', () => { + const task = getTaskById(999); + expect(task).toBeUndefined();}); \ No newline at end of file