Skip to content

Commit 5fd4b62

Browse files
committed
added medical record api.
added medical record api.
1 parent a0d6524 commit 5fd4b62

File tree

3 files changed

+133
-4
lines changed

3 files changed

+133
-4
lines changed

app/models/medicalrecordDbModel.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from mongoengine import Document, FloatField, StringField, ReferenceField, DateTimeField
2+
from mongoengine.errors import ValidationError
3+
from datetime import datetime
4+
5+
6+
# Custom modules
7+
from app.models.patientDbModel import Patient
8+
9+
# "MedicalRecord" model with validators
10+
class MedicalRecord(Document):
11+
bloodSugarLevel = FloatField(required=True, min_value=0, max_value=500)
12+
height = FloatField(required=True, min_value=0, max_value=300)
13+
weight = FloatField(required=True, min_value=0, max_value=1000)
14+
allergies = StringField()
15+
currentMedications = StringField()
16+
medicalHistory = StringField()
17+
systolicPressure = FloatField(required=True, min_value=0, max_value=300)
18+
diastolicPressure = FloatField(required=True, min_value=0, max_value=300)
19+
patientId = ReferenceField(Patient, required=True)
20+
dateCreated = DateTimeField(required=True, default=datetime.utcnow)
21+
dateModified = DateTimeField(required=True, default=datetime.utcnow)
22+
23+
# Custom validation method for the "MedicalRecord" model
24+
def clean(self):
25+
# Check if systolic pressure is greater than diastolic pressure
26+
if self.systolicPressure < self.diastolicPressure:
27+
raise ValidationError("Systolic pressure cannot be less than diastolic pressure")

app/routes/medicalRecordCRUD.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Standard library imports
2+
import datetime
3+
import json
4+
5+
# Third-party imports
6+
from flask import Blueprint, request
7+
from mongoengine import DoesNotExist
8+
9+
# Custom modules
10+
from app.models.medicalrecordDbModel import medicalrecord
11+
from service.errorHandler import error_handler
12+
from service.pydanticDecorator import pydantic_validation
13+
from service.response import response
14+
15+
16+
# * Define Blueprint for API Routes
17+
medicalrecord_module = Blueprint("medicalrecord_module", __name__)
18+
19+
20+
# * Define API Route for Create medicalrecord API
21+
@medicalrecord_module.route("/", methods=["POST"], endpoint="create-medicalrecord")
22+
@error_handler
23+
def create_medicalrecord_main():
24+
# * Get Data from Frontend
25+
data = json.loads(request.data)
26+
27+
# * Save Data in Mongodb
28+
medicalrecord = medicalrecord(**data).save()
29+
30+
body = {
31+
"data": json.loads(medicalrecord.to_json()),
32+
"msg": "Medical record created successfully.",
33+
}
34+
return response(201, body)
35+
36+
37+
# * Define API for update medicalrecord details
38+
@medicalrecord_module.route("/<id>", methods=["PUT"], endpoint="update-medicalrecord")
39+
@error_handler
40+
def update_medicalrecord_by_id(id):
41+
# get the medicalrecord instance with the given id
42+
medicalrecords = medicalrecord.objects(id=id)
43+
44+
# Check if the medicalrecord is None or not
45+
if medicalrecords.first() == None:
46+
return response(404, {"message": "Medical record not found."})
47+
48+
# get the update data from the request body
49+
data = request.get_json()
50+
51+
# update the medicalrecord instance with the new data
52+
medicalrecords.update(**data)
53+
54+
# update the modified_date field to the current date and time
55+
medicalrecords.update(set__modified_date=datetime.datetime.now)
56+
57+
body = {
58+
"data": json.loads(medicalrecords.first().to_json()),
59+
"message": "Medical record updated successfully.",
60+
}
61+
return response(200, body)
62+
63+
64+
# * Define API, which read id and delete medical record
65+
@medicalrecord_module.route("/<id>", methods=["DELETE"], endpoint="delete-medicalrecord")
66+
@error_handler
67+
def delete_medicalrecord_by_id(id):
68+
try:
69+
medicalrecord.objects.get(id=id).delete()
70+
body = {"message": "Medical record deleted successfully."}
71+
return response(204, body)
72+
except DoesNotExist:
73+
body = {"message": "Medical record not found."}
74+
return response(404, body)
75+
76+
77+
# * Define API, which reads all medical records from the database
78+
@medicalrecord_module.route("/", methods=["GET"], endpoint="get-all-medicalrecords")
79+
@error_handler
80+
def get_all_medicalrecords():
81+
medicalrecords = medicalrecord.objects()
82+
body = {
83+
"msg": "Successfully get all Medical record details.",
84+
"data": json.loads(medicalrecords.to_json()),
85+
}
86+
return response(200, body)
87+
88+
89+
# * Define API, which takes document id and returns value of that document
90+
@medicalrecord_module.route("/<id>", methods=["GET"], endpoint="get-single-medicalrecord")
91+
@error_handler
92+
def get_medicalrecord_by_id(id):
93+
try:
94+
medicalrecord = medicalrecord.objects.get(id=id)
95+
body = {
96+
"msg": "Successfully get single medical record details.",
97+
"data": json.loads(medicalrecord.to_json()),
98+
}
99+
return response(200, body)
100+
except DoesNotExist:
101+
body = {"message": "Medical record not found"}
102+
return response(404, body)

app/routes/patientCRUD.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def create_patient_main():
3636
return response(201, body)
3737

3838

39-
# * Design API for update patient details
39+
# * Define API for update patient details
4040
@patient_module.route("/<id>", methods=["PUT"], endpoint="update-patient")
4141
@pydantic_validation(PatientModel)
4242
@error_handler
@@ -64,7 +64,7 @@ def update_patient_by_id(id):
6464
return response(200, body)
6565

6666

67-
# * Desing API, which read id and delete patient
67+
# * Define API, which read id and delete patient
6868
@patient_module.route("/<id>", methods=["DELETE"], endpoint="delete-patient")
6969
@error_handler
7070
def delete_patient_by_id(id):
@@ -77,7 +77,7 @@ def delete_patient_by_id(id):
7777
return response(404, body)
7878

7979

80-
# * Desing API, which reads all patients from the database
80+
# * Define API, which reads all patients from the database
8181
@patient_module.route("/", methods=["GET"], endpoint="get-all-patients")
8282
@error_handler
8383
def get_all_patients():
@@ -89,7 +89,7 @@ def get_all_patients():
8989
return response(200, body)
9090

9191

92-
# * Design API, which takes document id and returns value of that document
92+
# * Define API, which takes document id and returns value of that document
9393
@patient_module.route("/<id>", methods=["GET"], endpoint="get-single-patient")
9494
@error_handler
9595
def get_patient_by_id(id):

0 commit comments

Comments
 (0)