Skip to content

Commit d2273aa

Browse files
feat: add uuid package and update todo ID generation
1 parent cab79fb commit d2273aa

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

api/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Fastify from "fastify";
22
import cors from "@fastify/cors";
3+
import { v4 as uuidv4 } from "uuid";
34

45
const fastify = Fastify({
56
logger: false,
@@ -16,7 +17,7 @@ fastify.get("/todos", (request, reply) => {
1617
// Get a single todo by ID
1718
fastify.get("/todos/:id", (request, reply) => {
1819
const { id } = request.params;
19-
const todo = todos.find((todo) => todo.id === parseInt(id));
20+
const todo = todos.find((todo) => todo.id === id);
2021
if (todo) {
2122
reply.send(todo);
2223
} else {
@@ -27,7 +28,7 @@ fastify.get("/todos/:id", (request, reply) => {
2728
fastify.post("/todos", (request, reply) => {
2829
const { title } = request.body;
2930
const newTodo = {
30-
id: todos.length + 1,
31+
id: uuidv4(),
3132
title,
3233
completed: false,
3334
};
@@ -39,7 +40,7 @@ fastify.post("/todos", (request, reply) => {
3940
fastify.put("/todos/:id", (request, reply) => {
4041
const { id } = request.params;
4142
const { title, completed } = request.body;
42-
const todo = todos.find((todo) => todo.id === parseInt(id));
43+
const todo = todos.find((todo) => todo.id === id);
4344
if (todo) {
4445
todo.title = title !== undefined ? title : todo.title;
4546
todo.completed = completed !== undefined ? completed : todo.completed;
@@ -52,7 +53,7 @@ fastify.put("/todos/:id", (request, reply) => {
5253
// Delete a todo
5354
fastify.delete("/todos/:id", (request, reply) => {
5455
const { id } = request.params;
55-
todos = todos.filter((todo) => todo.id !== parseInt(id));
56+
todos = todos.filter((todo) => todo.id !== id);
5657
reply.status(204).send();
5758
});
5859

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"clsx": "2.1.1",
1818
"fastify": "4.27.0",
1919
"react": "18.2.0",
20-
"react-dom": "18.2.0"
20+
"react-dom": "18.2.0",
21+
"uuid": "13.0.0"
2122
},
2223
"devDependencies": {
2324
"@playwright/test": "1.44.1",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)