Skip to content

Commit d13ec65

Browse files
committed
refactoring: Move the "dynamic memory DB" to its own folder
1 parent 1ec2303 commit d13ec65

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fastify from "fastify";
22
import { StatusCodes } from "http-status-codes";
33

4-
import { diaryRouter } from "../routes/diary.route";
4+
import { diaryRouter } from "./routes/diary.route";
55

66
const app = fastify();
77

src/models/persistence/note.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const notes: any = [];
File renamed without changes.
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifyInstance } from "fastify";
22
import { StatusCodes } from "http-status-codes";
33

4-
const dynamicDbData: any = [];
4+
import { notes } from "./../models/persistence/note";
55

66
interface Diary {
77
id?: number;
@@ -13,14 +13,14 @@ interface Diary {
1313
// CRUD endpoints for diary app
1414
export async function diaryRouter(app: FastifyInstance) {
1515
app.get("/", async (request, reply) => {
16-
reply.status(StatusCodes.OK).send(dynamicDbData);
16+
reply.status(StatusCodes.OK).send(notes);
1717
});
1818

1919
app.get("/:id", async (request, reply) => {
2020
const { id } = request.params as { id: string };
2121
const idInteger = parseInt(id, 10); // Cast to integer decimal number
2222

23-
const diary = dynamicDbData.find((item: Diary) => item.id === idInteger);
23+
const diary = notes.find((item: Diary) => item.id === idInteger);
2424

2525
if (!diary) {
2626
console.info("Note entry not found");
@@ -40,13 +40,13 @@ export async function diaryRouter(app: FastifyInstance) {
4040
return;
4141
}
4242

43-
body.id = dynamicDbData.length + 1;
43+
body.id = notes.length + 1;
4444

4545
if (!body.createdAt) {
4646
body.createdAt = new Date();
4747
}
4848

49-
dynamicDbData.push(body);
49+
notes.push(body);
5050
reply.status(StatusCodes.CREATED).send(body);
5151
});
5252

@@ -55,27 +55,23 @@ export async function diaryRouter(app: FastifyInstance) {
5555
const { id } = request.params as { id: string };
5656
const idInteger = parseInt(id, 10); // Cast to integer decimal number
5757

58-
const index = dynamicDbData.findIndex(
59-
(item: Diary) => item.id === idInteger
60-
);
58+
const index = notes.findIndex((item: Diary) => item.id === idInteger);
6159
if (index === -1) {
6260
reply.status(StatusCodes.NOT_FOUND).send();
6361
} else {
64-
dynamicDbData[index] = { ...dynamicDbData[index], ...(body as Diary) };
65-
reply.status(StatusCodes.OK).send(dynamicDbData[index]);
62+
notes[index] = { ...notes[index], ...(body as Diary) };
63+
reply.status(StatusCodes.OK).send(notes[index]);
6664
}
6765
});
6866

6967
app.delete("/:id", async (request, reply) => {
7068
const { id } = request.params as { id: string };
71-
const index = dynamicDbData.findIndex(
72-
(item: Diary) => item.id === parseInt(id)
73-
);
69+
const index = notes.findIndex((item: Diary) => item.id === parseInt(id));
7470

7571
if (index === -1) {
7672
reply.status(StatusCodes.NOT_FOUND).send();
7773
} else {
78-
dynamicDbData.splice(index, 1);
74+
notes.splice(index, 1);
7975
reply.status(StatusCodes.OK).send({
8076
message: "Note entry deleted",
8177
});

0 commit comments

Comments
 (0)