|
1 | 1 | from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter |
2 | 2 | from sqlalchemy.orm import Session |
3 | 3 | from typing import List, Optional |
| 4 | +from fastapi.responses import JSONResponse |
4 | 5 |
|
5 | 6 | from sqlalchemy import func |
6 | 7 | from .. import models, schemas, oauth2 |
|
14 | 15 | tags=['Chats & Feedback'] |
15 | 16 | ) |
16 | 17 |
|
17 | | -@router.post('/chat_messages/{id}', response_model=schemas.ChatList) |
| 18 | +@router.post('/chat_messages/{id}', response_model=schemas.JSONChatList) |
18 | 19 | def chat_messages(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)): |
19 | 20 | chats = db.query(models.Chat).filter(models.Chat.consultation_id == id).all() |
20 | 21 | if not chats: |
21 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
22 | | - detail=f"Chat with id {id} not found") |
| 22 | + error_response = { |
| 23 | + "status": "error", |
| 24 | + "id": -1, |
| 25 | + "data": f"Chat with id {id} not found" |
| 26 | + } |
| 27 | + return JSONResponse(content=error_response, status_code=404) |
23 | 28 |
|
24 | | - return schemas.ChatList(consultation_id=id, chats=chats) |
| 29 | + response_obj = schemas.ChatList(consultation_id=id, chats=chats) |
| 30 | + return schemas.JSONChatList(status="success", id=current_user.id, data=response_obj) |
25 | 31 |
|
26 | 32 |
|
27 | | -@router.post('/create_chat/{id}', response_model=schemas.ChatOut) |
| 33 | +@router.post('/create_chat/{id}', response_model=schemas.JSONChatOut) |
28 | 34 | def create_message(id: int, message: schemas.Chat, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)): |
29 | 35 | consultation = db.query(models.Consultation).filter(models.Consultation.id == id).first() |
30 | 36 | if not consultation: |
31 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
32 | | - detail=f"Consultation with id {id} not found") |
| 37 | + error_response = { |
| 38 | + "status": "error", |
| 39 | + "id": -1, |
| 40 | + "data": f"Consultation with id {id} not found" |
| 41 | + } |
| 42 | + return JSONResponse(content=error_response, status_code=404) |
33 | 43 |
|
34 | 44 | new_message = models.Chat(sender_id=current_user.id, consultation_id=id, **message.dict()) |
35 | 45 | db.add(new_message) |
36 | 46 | db.commit() |
37 | 47 | db.refresh(new_message) |
38 | 48 |
|
39 | | - return new_message |
| 49 | + return schemas.JSONChatOut(status="success", id=current_user.id, data=new_message) |
40 | 50 |
|
41 | 51 |
|
42 | | -@router.post('/user_feedback/{id}', response_model=schemas.FeedbackResponse) |
| 52 | +@router.post('/user_feedback/{id}', response_model=schemas.JSONFeedbackResponse) |
43 | 53 | def get_feedback(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)): |
44 | 54 | doctor = db.query(models.Doctor).filter(models.Doctor.doctor_id == id).first() |
45 | 55 | if not doctor: |
46 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
47 | | - detail=f"There is Currently no Feedback from Doctors for user, id: {id}") |
| 56 | + error_response = { |
| 57 | + "status": "error", |
| 58 | + "id": -1, |
| 59 | + "data": f"There is Currently no Feedback from Doctors for user, id: {id}" |
| 60 | + } |
| 61 | + return JSONResponse(content=error_response, status_code=404) |
48 | 62 |
|
49 | 63 | reviews = db.query(models.Feedback).filter(models.Feedback.receiver_id == id).all() |
50 | 64 | if not reviews: |
51 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
52 | | - detail=f"There is Currently no Feedback with id: {id}") |
| 65 | + error_response = { |
| 66 | + "status": "error", |
| 67 | + "id": -1, |
| 68 | + "data": f"There is Currently no Feedback with id: {id}" |
| 69 | + } |
| 70 | + return JSONResponse(content=error_response, status_code=404) |
53 | 71 |
|
54 | 72 | list_feedback = [] |
55 | 73 | for single_feedback in reviews: |
56 | 74 | feedback_sender = db.query(models.Patient).filter(models.Patient.patient_id == single_feedback.sender_id).first() |
57 | 75 | if not feedback_sender: |
58 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
59 | | - detail=f"Feedback Sender with id {single_feedback.sender_id} not found") |
| 76 | + error_response = { |
| 77 | + "status": "error", |
| 78 | + "id": -1, |
| 79 | + "data": f"Feedback Sender with id {single_feedback.sender_id} not found" |
| 80 | + } |
| 81 | + return JSONResponse(content=error_response, status_code=404) |
60 | 82 |
|
61 | 83 | list_feedback.append(schemas.FeedbackOut(created_at=single_feedback.created_at, feedback=single_feedback.feedback, sender=feedback_sender)) |
62 | | - |
63 | | - return schemas.FeedbackResponse(doctor_id=id, FeedBacks=list_feedback) |
| 84 | + |
| 85 | + response_obj = schemas.FeedbackResponse(doctor_id=id, FeedBacks=list_feedback) |
| 86 | + return schemas.JSONFeedbackResponse(status="success", id=current_user.id, data=response_obj) |
64 | 87 |
|
65 | 88 |
|
66 | | -@router.post('/post_feedback/{id}', response_model=schemas.FeedbackOutput) |
| 89 | +@router.post('/post_feedback/{id}', response_model=schemas.JSONFeedbackOutput) |
67 | 90 | def post_feedback(id: int, message: schemas.FeedbackCreate, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)): |
68 | 91 | doctor = db.query(models.Doctor).filter(models.Doctor.doctor_id == id).first() |
69 | 92 | if not doctor: |
70 | | - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
71 | | - detail=f"Doctor with id {id} not found") |
| 93 | + error_response = { |
| 94 | + "status": "error", |
| 95 | + "id": -1, |
| 96 | + "data": f"Doctor with id {id} not found" |
| 97 | + } |
| 98 | + return JSONResponse(content=error_response, status_code=404) |
72 | 99 |
|
73 | 100 | new_feedback = models.Feedback(sender_id=current_user.id, receiver_id=id, **message.dict()) |
74 | 101 |
|
75 | 102 | db.add(new_feedback) |
76 | 103 | db.commit() |
77 | 104 | db.refresh(new_feedback) |
78 | 105 |
|
79 | | - return new_feedback |
| 106 | + return schemas.JSONFeedbackOutput(status="success", id=current_user.id, data=new_feedback) |
80 | 107 |
|
81 | 108 |
|
82 | 109 | @router.post('/whatsapp') |
83 | 110 | def whatsapp(): |
84 | | - return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION } |
| 111 | + return { "status":"pending", "id": -1, "data": MESSAGE_UNDER_CONSTRUCTION } |
85 | 112 |
|
86 | 113 |
|
87 | 114 | @router.post('/meeting') |
88 | 115 | def meeting(): |
89 | | - return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION } |
| 116 | + return { "status":"pending", "id": -1, "data": MESSAGE_UNDER_CONSTRUCTION } |
90 | 117 |
|
0 commit comments