UC /users v1.0
This commit is contained in:
@@ -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'));
|
||||
|
||||
Reference in New Issue
Block a user