Skip to content

Commit

Permalink
Merge pull request #19 from ghtali/develop
Browse files Browse the repository at this point in the history
Bug fix, update endpoints, delete request for lab test.
  • Loading branch information
ghtali authored Oct 7, 2023
2 parents 089c263 + 483cf01 commit 531619e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 76 deletions.
70 changes: 16 additions & 54 deletions api/appointments.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,25 @@
from fastapi import APIRouter, Depends, HTTPException, Response, status
from sqlalchemy.orm import Session

from ..db.database import SessionLocal
from ..db.models import Appointment
from ..db.repositories.appointments_repository import AppointmentsRepository
from ..services.appointments_service import AppointmentsService

router = APIRouter(prefix="/appointments", tags=["appointments"])


def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()


@router.post("/", response_model=Appointment, status_code=status.HTTP_201_CREATED)
async def create_appointment(appointment: Appointment, db: Session = Depends(get_db)):
appointment_service = AppointmentsService(AppointmentsRepository(db))
created_appointment = appointment_service.create_appointment(appointment)

return created_appointment


@router.get("/{appointment_id}", response_model=Appointment)
async def get_appointment(appointment_id: int, db: Session = Depends(get_db)):
appointment_service = AppointmentsService(AppointmentsRepository(db))
appointment = appointment_service.get_appointment(appointment_id)

if not appointment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Appointment not found")

return appointment
from flask import Blueprint, jsonify, request

from ..services.appointments_service import AppointmentsService

@router.put("/{appointment_id}", response_model=Appointment)
async def update_appointment(appointment_id: int, appointment: Appointment, db: Session = Depends(get_db)):
appointment_service = AppointmentsService(AppointmentsRepository(db))
updated_appointment = appointment_service.update_appointment(
appointment_id, appointment)
appointments_api_bp = Blueprint('appointments_api', __name__)
appointments_service = AppointmentsService()

if not updated_appointment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Appointment not found")
# Define Flask routes based on the original FastAPI routes...

return updated_appointment
# TODO: Define the necessary routes here


@router.delete("/{appointment_id}")
async def delete_appointment(appointment_id: int, db: Session = Depends(get_db)):
appointment_service = AppointmentsService(AppointmentsRepository(db))
appointment_deleted = appointment_service.delete_appointment(
appointment_id)
@appointments_api_bp.route('/appointments', methods=['GET'])
def get_all_appointments():
appointments = appointments_service.get_all_appointments()
return jsonify(appointments)

if not appointment_deleted:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Appointment not found")
@appointments_api_bp.route('/appointments/<int:appointment_id>', methods=['GET'])
def get_appointment(appointment_id):
appointment = appointments_service.get_appointment_by_id(appointment_id)
if appointment:
return jsonify(appointment)
return jsonify({"error": "Appointment not found"}), 404

return Response(status_code=status.HTTP_204_NO_CONTENT)
9 changes: 5 additions & 4 deletions api/lab_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

@lab_tests_api_bp.route('/lab_tests', methods=['POST'])
def create_lab_test():
# TODO: Implement create_lab_test endpoint
pass
data = request.json
lab_test = lab_tests_service.add_lab_test(data)
return jsonify(lab_test), 201


@lab_tests_api_bp.route('/lab_tests', methods=['GET'])
def get_all_lab_tests():
# TODO: Implement get_all_lab_tests endpoint
pass
lab_tests = lab_tests_service.get_all_lab_tests()
return jsonify(lab_tests)


@lab_tests_api_bp.route('/lab_tests/<int:lab_test_id>', methods=['GET'])
Expand Down
58 changes: 40 additions & 18 deletions models/lab_tests.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
"""
This file contains the LabTest model class.
"""
from flask import Blueprint, jsonify, request

from .base import Base, db
from ..services.lab_tests_service import LabTestsService


class LabTest(Base):
"""
The LabTest model class.
"""
__tablename__ = 'lab_tests'
lab_tests_api_bp = Blueprint('lab_tests_api', __name__)
lab_tests_service = LabTestsService()

# Fields
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text, nullable=False)
price = db.Column(db.Float, nullable=False)

def __repr__(self):
"""
Returns a string representation of the lab test object.
"""
return f"<LabTest {self.id}>"
@lab_tests_api_bp.route('/lab_tests', methods=['POST'])
def create_lab_test():
data = request.json
lab_test = lab_tests_service.add_lab_test(data)
return jsonify(lab_test), 201


@lab_tests_api_bp.route('/lab_tests', methods=['GET'])
def get_all_lab_tests():
lab_tests = lab_tests_service.get_all_lab_tests()
return jsonify(lab_tests)


@lab_tests_api_bp.route('/lab_tests/<int:lab_test_id>', methods=['GET'])
def get_lab_test(lab_test_id):
lab_test = lab_tests_service.get_lab_test_by_id(lab_test_id)
if lab_test:
return jsonify(lab_test)
return jsonify({'error': 'Lab Test not found'}), 404


@lab_tests_api_bp.route('/lab_tests/<int:lab_test_id>', methods=['PUT'])
def update_lab_test(lab_test_id):
data = request.json
updated_lab_test = lab_tests_service.update_lab_test(lab_test_id, data)
if updated_lab_test:
return jsonify(updated_lab_test)
return jsonify({'error': 'Lab Test not found or update failed'}), 404


@lab_tests_api_bp.route('/lab_tests/<int:lab_test_id>', methods=['DELETE'])
def delete_lab_test(lab_test_id):
result = lab_tests_service.delete_lab_test(lab_test_id)
if result:
return jsonify({'message': 'Lab Test deleted successfully'}), 200
return jsonify({'error': 'Lab Test not found or deletion failed'}), 404

0 comments on commit 531619e

Please sign in to comment.