Commit 420efe85 authored by Omar Elkadi's avatar Omar Elkadi
Browse files

impelement MongoDb

parent 8ac2cc11
......@@ -48,22 +48,46 @@ export class patientRoutes {
// parse JSON to Objects
var patientStudent = JSON.parse(body.patientStudent);
var sickNote = JSON.parse(body.SickNote);
var message = JSON.parse(body.Message);
const file = req.file;
if (!file) {
console.log("not file error");
}
const path = "./uploads/reports/" + patientStudent.MatrikelNumber + ".json";
var sicknessReport = {
var sickReport = {
patientStudent,
sickNote,
message,
sickNoteFilePath : path,
};
writeFile(path, JSON.stringify(sicknessReport), function (err) {
}
const file = req.file;
if (!file || sickReport == null) {
console.log("the data is invalid");
return res.status(400).send("Bad Request");
}
// define mongoDb instance
var MongoClient = require('mongodb').MongoClient
// connect to the server
MongoClient.connect('mongodb://localhost:27017',async function (err, client) {
if (err) throw err
// cereat the DB instance
var db = client.db('admin')
// create the collection if not exist
try{
await db.createCollection( "Students",{} )
console.log("Collection Student is created")
}
catch(error){
console.log("Collection already exists, so it didnot be created")
}
// define a collection and insert a new record
try {
await db.collection('Students').insertOne(sickReport)
console.log("a record added succsully to the collection Students")
} catch (error) {
console.log(error)
}
})
// save the recived Object as Json file with the mat number as a name
writeFile(path, JSON.stringify(sickReport), function (err) {
if (err) return console.log(err);
else return console.log("file generated in path: " + path);
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment