From f65772d6e4d458c532d1be75e0a38a0af321f45a Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 27 Feb 2023 23:11:51 +0000 Subject: [PATCH] improve local db utils Signed-off-by: Arthur Lu --- db.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/db.js b/db.js index b474c53..0731fa0 100644 --- a/db.js +++ b/db.js @@ -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; } }