diff --git a/parts/3/followAlong/index.js b/parts/3/followAlong/index.js
index 7df9407..e168b9e 100644
--- a/parts/3/followAlong/index.js
+++ b/parts/3/followAlong/index.js
@@ -1,6 +1,8 @@
const express = require("express");
const app = express();
+app.use(express.json());
+
let notes = [
{ id: "1", content: "HTML is easy", important: true },
{ id: "2", content: "Browser can execute only JavaScript", important: false },
@@ -11,6 +13,12 @@ let notes = [
},
];
+const generateId = () => {
+ const maxId =
+ notes.length > 0 ? Math.max(...notes.map((n) => Number(n.id))) : 0;
+ return String(maxId + 1);
+};
+
app.get("/", (request, response) => {
response.send("
Hello World!
");
});
@@ -19,6 +27,26 @@ app.get("/api/notes", (request, response) => {
response.json(notes);
});
+app.post("/api/notes", (request, response) => {
+ const body = request.body;
+
+ if (!body.content) {
+ return response.status(400).json({
+ error: "content missing",
+ });
+ }
+
+ const note = {
+ content: body.content,
+ important: body.important || false,
+ id: generateId(),
+ };
+
+ notes = notes.concat(note);
+
+ response.json(note);
+});
+
app.get("/api/notes/:id", (request, response) => {
const id = request.params.id;
const note = notes.find((note) => note.id === id);
diff --git a/parts/3/followAlong/requests/create_note.rest b/parts/3/followAlong/requests/create_note.rest
new file mode 100644
index 0000000..5efc6dd
--- /dev/null
+++ b/parts/3/followAlong/requests/create_note.rest
@@ -0,0 +1,7 @@
+POST http://localhost:3001/api/notes
+Content-Type: application/json
+
+{
+ "content": "i'm a tttttnoty note",
+ "important": false
+}
\ No newline at end of file
diff --git a/parts/3/followAlong/requests/get_all_notes.rest b/parts/3/followAlong/requests/get_all_notes.rest
new file mode 100644
index 0000000..b20ee4b
--- /dev/null
+++ b/parts/3/followAlong/requests/get_all_notes.rest
@@ -0,0 +1 @@
+GET http://localhost:3001/api/notes
\ No newline at end of file