From 46fa00a83a34ae4f524bb9c4450f16180b410cb8 Mon Sep 17 00:00:00 2001
From: abouchtita <abouchtita@devops-tp.local>
Date: Fri, 14 Mar 2025 17:15:50 +0100
Subject: [PATCH] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20d'une=20t=C3=A2che=20p?=
 =?UTF-8?q?ar=20ID=20&=20test?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 FETCH_HEAD                     |  0
 src/index.js                   | 11 +++++++++++
 src/taskService.js             |  8 +++++++-
 tests/integration/app.test.js  | 19 +++++++++++++++++++
 tests/unit/todoService.test.js | 10 +++++++++-
 5 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 FETCH_HEAD

diff --git a/FETCH_HEAD b/FETCH_HEAD
new file mode 100644
index 0000000..e69de29
diff --git a/src/index.js b/src/index.js
index 441d6f9..d2ff431 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 aad0b48..19298d6 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 04c94c8..dd7eb43 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 503dafd..9c0157a 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
-- 
GitLab