Skip to content

Commit

Permalink
updated db code
Browse files Browse the repository at this point in the history
  • Loading branch information
jzakotnik committed Jul 31, 2021
1 parent 4aa3943 commit 2b90824
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
9 changes: 8 additions & 1 deletion nextjs/mwe/pages/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ export default function Admin(props) {
event.preventDefault();
const adminID = uuidv4();
const readerID = uuidv4();
const defaultLunchProfile = {
mon: true,
tue: true,
wed: true,
thu: true,
fri: true,
};
setAuthurl({ admin: adminID, reader: readerID });

fetch("http://localhost:3000/api/insertData", {
method: "POST",
body: JSON.stringify(authurl),
body: JSON.stringify({ authurl, defaultLunchProfile }),
headers: {
"Content-Type": "application/json",
},
Expand Down
19 changes: 16 additions & 3 deletions nextjs/mwe/pages/api/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ var level = require("level-rocksdb");

export default function handler(req, res) {
var db = level("./lunchdb");
console.log(req.body);
db.get("juresUUID", function (err, value) {
//console.log(req.body);
/*db.get("juresUUID", function (err, value) {
if (err) return console.log("Ooops!", err); // likely the key was not found
console.log(value);
});
});*/
db.createReadStream()
.on("data", function (data) {
console.log(data.key, "=", data.value);
})
.on("error", function (err) {
console.log("Oh my!", err);
})
.on("close", function () {
console.log("Stream closed");
})
.on("end", function () {
console.log("Stream ended");
});
db.close();
res.end().status(200);
}
13 changes: 12 additions & 1 deletion nextjs/mwe/pages/api/insertData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ var level = require("level-rocksdb");

export default function handler(req, res) {
var db = level("./lunchdb");
console.log("Inserting data into DB");
console.log(req.body);
db.put("juresUUID", JSON.stringify(req.body), function (err) {
const authurl = req.body.authurl;
const lunchprofile = req.body.lunchprofile;

db.put(
authurl.admin,
{ lunchprofile, reader: authurl.reader, admin: true },
function (err) {
if (err) return console.log("Ooops!", err); // some kind of I/O error
}
);
db.put(authurl.reader, { lunchprofile, admin: false }, function (err) {
if (err) return console.log("Ooops!", err); // some kind of I/O error
});
db.close();
Expand Down

0 comments on commit 2b90824

Please sign in to comment.