improve local db utils

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-02-27 23:11:51 +00:00
parent aa548d4fa8
commit 537420624e

22
db.js
View File

@ -23,7 +23,7 @@ function init () {
* @returns {boolean} whether the user is approved to allocate requested resources
*/
function requestResources (user, resources) {
Object.keys(resources).forEach((element) => {
Object.keys(db[user]).forEach((element) => {
if (db[user][element] < resources[element]) {
return false;
}
@ -38,14 +38,18 @@ function requestResources (user, resources) {
* @returns {boolean} true if resources were successfully allocated, false otherwise
*/
function allocateResources (user, resources) {
Object.keys(resources).forEach((element) => {
db[user][element] -= resource[element];
let newdb = {};
Object.assign(newdb, db);
Object.keys(db[user]).forEach((element) => {
newdb[user][element] -= resource[element];
});
try {
fs.writeFileSync(filename, db);
fs.writeFileSync(filename, newdb);
Object.assign(db, newdb);
return true;
}
catch {
fs.writeFileSync(filename, db)
return false;
}
}
@ -57,14 +61,18 @@ function allocateResources (user, resources) {
* @returns {boolean} true if resources were successfully deallocated, false otherwise
*/
function releaseResources (user, resources) {
Object.keys(resources).forEach((element) => {
db[user][element] += resource[element];
let newdb = {};
Object.assign(newdb, db);
Object.keys(db[user]).forEach((element) => {
newdb[user][element] += resource[element];
});
try {
fs.writeFileSync(filename, db);
fs.writeFileSync(filename, newdb);
Object.assign(db, newdb);
return true;
}
catch {
fs.writeFileSync(filename, db)
return false;
}
}