UC /users v1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-04-03 08:02:26 +03:00
parent 5e5bab933b
commit 4f8719e9c8
16 changed files with 295 additions and 31 deletions
+42
View File
@@ -31,6 +31,48 @@ class JSONDatabase {
console.error('Error writing file:', error);
}
}
isKeyValue(key, value) {
const data = this.readFile();
if (!data) {
return false;
}
for (const item of data) {
if (item[key] === value) {
return true;
}
}
return false;
}
findIndexByKeyValueInArray(key, value) {
const data = this.readFile();
if (!data || !Array.isArray(data)) {
return -1;
}
for (let i = 0; i < data.length; i++) {
if (data[i][key] === value) {
return i;
}
}
return -1;
}
findByKeyValue(key, value) {
const data = this.readFile();
if (!data || !Array.isArray(data)) {
return null; // Database read failed or data is not an array
}
for (const item of data) {
if (item[key] === value) {
return item; // Return the object containing the specified key-value pair
}
}
return null; // No matching key-value pair found
}
}
const usersDB = new JSONDatabase(path.join(__dirname, 'users.json'));