Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ function readStoreJSON() {
}
readStoreJSON();

function writeIntoStoreJson(store) {
fs.writeFile(
"./db/store.json",
JSON.stringify(store, null, 4),
"utf8",
function cb(err) {
if (err) {
res.status(500).send("Could not save data");
}
}
);
}
// Helper functions

/**
Expand Down Expand Up @@ -216,16 +228,7 @@ router.post("/:entityType", (req, res) => {
newEntity["id"] = id;
entities.push(newEntity);

fs.writeFile(
"./db/store.json",
JSON.stringify(store, null, 4),
"utf8",
function cb(err) {
if (err) {
res.status(500).send("Could not save data");
}
}
);
writeIntoStoreJson(store);
res.send(newEntity);
});
// Update an existing entity rowby id
Expand All @@ -250,15 +253,7 @@ router.put("/:entityType/:id", (req, res) => {
obj.id === mergedEntity.id ? mergedEntity : obj
);

fs.writeFile(
"./db/store.json",
JSON.stringify(store, null, 4),
"utf8",
function cb(err) {
if (err) {
}
}
);
writeIntoStoreJson(store)
res.send(mergedEntity);
});

Expand All @@ -268,17 +263,7 @@ router.delete("/:entityType/:id", (req, res) => {
let id = req.params.id;
if (removeEntityById(store, entityType, id) != -1) {
res.send("Deleted Successfully");

fs.writeFile(
"./db/store.json",
JSON.stringify(store, null, 4),
"utf8",
function cb(err) {
if (err) {
res.status(500).send("Could not save data");
}
}
);
writeIntoStoreJson(store)
} else {
respondWith404(res);
}
Expand Down