Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Polytech Todo Api Abouchtita
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BOUCHTITA Achraf
Polytech Todo Api Abouchtita
Commits
f33d6e20
Commit
f33d6e20
authored
4 months ago
by
abouchtita
Browse files
Options
Downloads
Patches
Plain Diff
Ajout de la suppression d'une tâche (endpoint DELETE /api/tasks/:id)
parent
a76df305
No related branches found
No related tags found
1 merge request
!1
Develop into main
Pipeline
#47832
passed
4 months ago
Stage: lint
Stage: test
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/index.js
+10
-0
10 additions, 0 deletions
src/index.js
src/taskService.js
+12
-1
12 additions, 1 deletion
src/taskService.js
tests/integration/app.test.js
+25
-0
25 additions, 0 deletions
tests/integration/app.test.js
tests/unit/todoService.test.js
+18
-1
18 additions, 1 deletion
tests/unit/todoService.test.js
with
65 additions
and
2 deletions
src/index.js
+
10
−
0
View file @
f33d6e20
...
@@ -45,6 +45,16 @@ app.post('/api/tasks', (req, res) => {
...
@@ -45,6 +45,16 @@ app.post('/api/tasks', (req, res) => {
}
}
});
});
app
.
delete
(
'
/api/tasks/:id
'
,
(
req
,
res
)
=>
{
const
id
=
req
.
params
.
id
;
const
result
=
taskService
.
deleteTask
(
id
);
if
(
result
)
{
res
.
status
(
204
).
send
();
}
else
{
res
.
status
(
404
).
json
({
error
:
'
Tâche non trouvée
'
});
}
});
// 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
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
src/taskService.js
+
12
−
1
View file @
f33d6e20
...
@@ -28,9 +28,20 @@ function addTask(description) {
...
@@ -28,9 +28,20 @@ function addTask(description) {
return
newTask
;
return
newTask
;
}
}
function
deleteTask
(
id
)
{
const
taskId
=
parseInt
(
id
,
10
);
const
index
=
tasks
.
findIndex
(
task
=>
task
.
id
===
taskId
);
if
(
index
===
-
1
)
{
return
false
;
}
tasks
.
splice
(
index
,
1
);
return
true
;
}
//export des fonctions
//export des fonctions
module
.
exports
=
{
module
.
exports
=
{
getAllTasks
,
getAllTasks
,
getTaskById
,
getTaskById
,
addTask
addTask
,
deleteTask
};
};
This diff is collapsed.
Click to expand it.
tests/integration/app.test.js
+
25
−
0
View file @
f33d6e20
...
@@ -65,3 +65,28 @@ describe('POST /api/tasks', () => {
...
@@ -65,3 +65,28 @@ describe('POST /api/tasks', () => {
});
});
});
});
describe
(
'
DELETE /api/tasks/:id
'
,
()
=>
{
test
(
'
should delete an existing task and return 204
'
,
async
()
=>
{
const
addResponse
=
await
request
(
app
)
.
post
(
'
/api/tasks
'
)
.
send
({
description
:
"
Task to delete via DELETE endpoint
"
});
expect
(
addResponse
.
statusCode
).
toBe
(
201
);
const
taskId
=
addResponse
.
body
.
id
;
const
deleteResponse
=
await
request
(
app
).
delete
(
`/api/tasks/
${
taskId
}
`
);
expect
(
deleteResponse
.
statusCode
).
toBe
(
204
);
const
getResponse
=
await
request
(
app
).
get
(
`/api/tasks/
${
taskId
}
`
);
expect
(
getResponse
.
statusCode
).
toBe
(
404
);
});
test
(
'
should return 404 when trying to delete a non-existent task
'
,
async
()
=>
{
const
response
=
await
request
(
app
).
delete
(
'
/api/tasks/99999
'
);
expect
(
response
.
statusCode
).
toBe
(
404
);
expect
(
response
.
body
).
toEqual
({
error
:
'
Tâche non trouvée
'
});
});
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/unit/todoService.test.js
+
18
−
1
View file @
f33d6e20
//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
,
addTask
}
=
require
(
'
../../src/taskService
'
);
const
{
getAllTasks
,
getTaskById
,
addTask
,
deleteTask
}
=
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
'
,
()
=>
{
...
@@ -33,3 +33,20 @@ test('should add a new task with provided description', () => {
...
@@ -33,3 +33,20 @@ test('should add a new task with provided description', () => {
test
(
'
should throw an error if description is missing
'
,
()
=>
{
test
(
'
should throw an error if description is missing
'
,
()
=>
{
expect
(()
=>
addTask
()).
toThrow
(
"
La description est requise
"
);
expect
(()
=>
addTask
()).
toThrow
(
"
La description est requise
"
);
});
});
test
(
'
should delete a task with a valid id
'
,
()
=>
{
const
initialLength
=
getAllTasks
().
length
;
const
newTask
=
addTask
(
"
Task to delete
"
);
expect
(
getAllTasks
().
length
).
toBe
(
initialLength
+
1
);
const
result
=
deleteTask
(
newTask
.
id
);
expect
(
result
).
toBe
(
true
);
expect
(
getAllTasks
().
length
).
toBe
(
initialLength
);
});
test
(
'
should return false when deleting a non-existent task
'
,
()
=>
{
const
result
=
deleteTask
(
999
);
expect
(
result
).
toBe
(
false
);
});
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment