diff --git a/server/src/routes/patientRoutes.ts b/server/src/routes/patientRoutes.ts index 894ee0e1e562ec7b17331324f3bba9e7c6d0ecff..74f4fdde02bc18d000f6435b0ed4b7e96dc07354 100644 --- a/server/src/routes/patientRoutes.ts +++ b/server/src/routes/patientRoutes.ts @@ -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); });