Skip to content

Commit 80e1fa8

Browse files
Merge pull request #238 from HackYourFuture-CPH/234-put-and-delete-node
backend/node: PUT and DELETE exercises
2 parents 3160e5d + ba3da02 commit 80e1fa8

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
11
# PUT endpoint
22

3-
TODO
3+
## `PUT /api/snippets/:id`
4+
5+
Let's start with a simplified version of the `PUT /api/snippets/:id` route. First we add the PUT route to `api/snippets.js`:
6+
7+
```js
8+
// ...
9+
10+
// PUT /api/snippets/:id
11+
router.put("/:id", async (request, response) => {
12+
// TODO
13+
});
14+
15+
// ...
16+
```
17+
18+
To be able to update a row in the `snippets` table, we need to have an existing snippet. Create a snippet first and note what the snippet ID is.
19+
20+
The PUT request we want to make will look something like this:
21+
22+
```text
23+
PUT /api/snippets/1
24+
{
25+
"title": "Updated snippet title",
26+
"contents": "#hello world",
27+
"is_private": true
28+
}
29+
```
30+
31+
### Exercise: Implement the PUT endpoint
32+
33+
When we now make a request like:
34+
35+
```text
36+
PUT /api/snippets/1
37+
{
38+
"title": "Updated snippet title",
39+
"contents": "#hello world",
40+
"is_private": true
41+
}
42+
```
43+
44+
you should update the existing row in the `snippets` table with the data from the request body, where the snippet ID matches the `:id` parameter in the URL.
45+
46+
When updating a snippet we also need to specify a `user_id`. For now, you can just pass in the `user_id` in the request body (alongside the other snippet data).
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
11
# DELETE endpoint
22

3-
TODO
3+
## `DELETE /api/snippets/:id`
4+
5+
Let's start with a simplified version of the `DELETE /api/snippets/:id` route. First we add the DELETE route to `api/snippets.js`:
6+
7+
```js
8+
// ...
9+
10+
// DELETE /api/snippets/:id
11+
router.delete("/:id", async (request, response) => {
12+
// TODO
13+
});
14+
15+
// ...
16+
```
17+
18+
To be able to delete a row from the `snippets` table, we need to have an existing snippet. Create a snippet first and note what the snippet ID is.
19+
20+
The DELETE request we want to make will look something like this:
21+
22+
```text
23+
DELETE /api/snippets/1
24+
```
25+
26+
### Exercise: Implement the DELETE endpoint
27+
28+
When we now make a request like:
29+
30+
```text
31+
DELETE /api/snippets/1
32+
```
33+
34+
you should delete the row from the `snippets` table where the snippet ID matches the `:id` parameter in the URL.

0 commit comments

Comments
 (0)