|
| 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) |
0 commit comments