Skip to content

Commit

Permalink
Bugs fixed, error messages added.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailcankaratas committed Jul 1, 2022
1 parent 529c37b commit b4da572
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions helpers/dataFilter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const log = require("../utils/log");
const getAllData = require("./getAllData")

function dataFilter(arrayName, condition) {
const dataAll = getAllData();
if (dataAll[arrayName] == undefined) {
log(`${arrayName} array not found.`, "Error", "error");
return undefined
}
return dataAll[arrayName].filter(condition)
}

Expand Down
5 changes: 5 additions & 0 deletions helpers/dataFind.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const log = require("../utils/log");
const getAllData = require("./getAllData")

function dataFind(arrayName, condition) {
const dataAll = getAllData();
if (dataAll[arrayName] == undefined) {
log(`${arrayName} array not found.`, "Error", "error");
return undefined
}
return dataAll[arrayName].find(condition)
}

Expand Down
5 changes: 5 additions & 0 deletions helpers/removeData.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const getAllData = require("./getAllData");
const writeDataJson = require("./writeDataJson");
const log = require("../utils/log");

function removeData(arrayName, dataId) {
var dataAll = getAllData();
if (dataAll[arrayName] == undefined) {
log(`${arrayName} array not found.`, "Error", "error");
return undefined
}
const index = dataAll[arrayName].findIndex(x => x.id == dataId);
dataAll[arrayName].splice(index, 1);
writeDataJson(JSON.stringify(dataAll));
Expand Down
20 changes: 19 additions & 1 deletion helpers/updateData.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
const getAllData = require("./getAllData");
const writeDataJson = require("./writeDataJson");
const log = require("../utils/log");

function updateData(arrayName, data) {
var dataAll = getAllData();

if (dataAll[arrayName] == undefined) {
log(`${arrayName} array not found.`, "Error", "error");
return undefined
}

if (!data.id) {
log(`There is no id data!`, "Error", "error");
return undefined
}

const index = dataAll[arrayName].findIndex(i => i.id === data.id);

if (index == -1) {
log(`No data matching id found!`, "Error", "error");
return undefined
}

Object.entries(dataAll[arrayName][index]).forEach(entry => {
const [key, value] = entry;
if (key != "id") {
dataAll[arrayName][index][key] = data[key];
}
});
writeDataJson(JSON.stringify(dataAll));
writeDataJson(JSON.stringify(dataAll, null, 4));
}

module.exports = updateData;
1 change: 1 addition & 0 deletions operations/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function add(arrayName, data) {
newData[arrayName].push(data);
}
writeDataJson(JSON.stringify(newData, null, 4));
return data;
}

module.exports = add;
12 changes: 12 additions & 0 deletions operations/data/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"products": [
{
"id": 1,
"categoryId": 2,
"productName": "Chai",
"quantityPerUnit": "48 - 6 oz jars",
"unitPrice": "30",
"unitsInStock": 53
}
]
}
5 changes: 5 additions & 0 deletions operations/getById.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const getAll = require("./getAll");
const log = require('../utils/log');

function getById(arrayName, dataId) {
var dataAll = getAll();
if (dataAll[arrayName] == undefined) {
log(`${arrayName} array not found.`, "Error", "error");
return undefined
}
return dataAll[arrayName].find(x => x.id == dataId)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dbcopycat",
"version": "0.4.1",
"version": "0.5.1",
"description": "A JSON Database that saves your Json data in a file and makes it easy for you to perform CRUD operations.",
"main": "local.js",
"license": "ISC",
Expand Down

0 comments on commit b4da572

Please sign in to comment.