From 420efe853c8b98c29affef6bcd6a7c620d628431 Mon Sep 17 00:00:00 2001
From: Omar Elkadi <omar.elkadi@stud.th-deg.de>
Date: Fri, 25 Jun 2021 01:46:12 +0200
Subject: [PATCH] impelement MongoDb

---
 server/src/routes/patientRoutes.ts | 46 +++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 11 deletions(-)

diff --git a/server/src/routes/patientRoutes.ts b/server/src/routes/patientRoutes.ts
index 894ee0e..74f4fdd 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);
     });
-- 
GitLab