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

Ajout de l'endpoint POST /api/tasks et de la fonction addTask

parent ab758194
Branches
No related tags found
1 merge request!1Develop into main
Pipeline #47826 failed
...@@ -32,6 +32,19 @@ app.get('/api/tasks/:id', (req, res) => { ...@@ -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 // Fonction pour démarrer le serveur
const startServer = () => { const startServer = () => {
const server = app.listen(port, () => { const server = app.listen(port, () => {
......
...@@ -14,8 +14,23 @@ function getAllTasks() { ...@@ -14,8 +14,23 @@ function getAllTasks() {
return tasks; 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 //export des fonctions
module.exports = { module.exports = {
getAllTasks, getAllTasks,
getTaskById getTaskById,
addTask
}; };
...@@ -43,3 +43,25 @@ describe('GET /api/tasks/:id', () => { ...@@ -43,3 +43,25 @@ describe('GET /api/tasks/:id', () => {
}); });
}); });
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
//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} = require('../../src/taskService'); const { getAllTasks, getTaskById, addTask } = 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', () => {
...@@ -17,4 +17,19 @@ test('should return the correct task for a given id', () => { ...@@ -17,4 +17,19 @@ test('should return the correct task for a given id', () => {
test('should return undefined for a non-existent task id', () => { test('should return undefined for a non-existent task id', () => {
const task = getTaskById(999); const task = getTaskById(999);
expect(task).toBeUndefined();}); expect(task).toBeUndefined();
\ No newline at end of file });
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment