forked from yaso_meth/mih-project
Add otification api with get and inster api
This commit is contained in:
@@ -7,6 +7,7 @@ import routers.patients_files as patients_files
|
||||
import routers.patients_notes as patients_notes
|
||||
import routers.patients_queue as patients_queue
|
||||
import routers.users as users
|
||||
import routers.notifications as notifications
|
||||
import routers.fileStorage as fileStorage
|
||||
import routers.medicine as medicine
|
||||
import routers.business_user as business_user
|
||||
@@ -83,6 +84,7 @@ app.include_router(fileStorage.router)
|
||||
app.include_router(medicine.router)
|
||||
app.include_router(business_user.router)
|
||||
app.include_router(business.router)
|
||||
app.include_router(notifications.router)
|
||||
|
||||
# Check if server is up
|
||||
@app.get("/", tags=["Server Check"])
|
||||
|
||||
154
backend/routers/notifications.py
Normal file
154
backend/routers/notifications.py
Normal file
@@ -0,0 +1,154 @@
|
||||
import mysql.connector
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime, timedelta
|
||||
#from ..database import dbConnection
|
||||
import database
|
||||
#SuperToken Auth from front end
|
||||
from supertokens_python.recipe.session.framework.fastapi import verify_session
|
||||
from supertokens_python.recipe.session import SessionContainer
|
||||
from fastapi import Depends
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class notificationsGetNuberedRequest(BaseModel):
|
||||
notification_count: str
|
||||
app_id: str
|
||||
|
||||
class notificationInsertRequest(BaseModel):
|
||||
app_id: str
|
||||
notification_type: str
|
||||
notification_message: str
|
||||
action_path: str
|
||||
|
||||
# class patientUpdateRequest(BaseModel):
|
||||
# id_no: str
|
||||
# app_id: str
|
||||
# last_name: str
|
||||
# email: str
|
||||
# cell_no: str
|
||||
# medical_aid: str
|
||||
# medical_aid_main_member: str
|
||||
# medical_aid_no: str
|
||||
# medical_aid_code: str
|
||||
# medical_aid_name: str
|
||||
# medical_aid_scheme: str
|
||||
# address: str
|
||||
# app_id: str
|
||||
|
||||
# class patientDeleteRequest(BaseModel):
|
||||
# app_id: str
|
||||
|
||||
|
||||
# Get Notifications By app ID
|
||||
@router.get("/notifications/{app_id}", tags=["Notifications"])
|
||||
async def read_notifications_By_app_ID(app_id: str, amount: int): # , session: SessionContainer = Depends(verify_session())
|
||||
db = database.dbConnection.dbAppDataConnect()
|
||||
cursor = db.cursor()
|
||||
#query = "SELECT * FROM patients"
|
||||
query = "Select * from notifications "
|
||||
query += "where app_id = '%s' " % app_id
|
||||
query += "order by insert_date desc "
|
||||
query += "limit %s" % amount
|
||||
# return {"query": query}
|
||||
cursor.execute(query)
|
||||
items = [
|
||||
{
|
||||
"idnotifications": item[0],
|
||||
"app_id": item[1],
|
||||
"notification_message": item[2],
|
||||
"notification_read": item[3],
|
||||
"action_path": item[4],
|
||||
"insert_date": item[5],
|
||||
"notification_type": item[6],
|
||||
}
|
||||
for item in cursor.fetchall()
|
||||
]
|
||||
cursor.close()
|
||||
db.close()
|
||||
return items
|
||||
|
||||
|
||||
# Insert Patient into table
|
||||
@router.post("/notifications/insert/", tags=["Notifications"], status_code=201)
|
||||
async def insert_Patient(itemRequest : notificationInsertRequest, session: SessionContainer = Depends(verify_session())): #, session: SessionContainer = Depends(verify_session())
|
||||
db = database.dbConnection.dbAppDataConnect()
|
||||
now = datetime.now() + timedelta(hours=2)
|
||||
notificationDateTime = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(notificationDateTime)
|
||||
readType = "No"
|
||||
cursor = db.cursor()
|
||||
query = "insert into notifications "
|
||||
query += "(app_id, notification_message, notification_read, action_path, insert_date, notification_type) "
|
||||
query += "values (%s, %s, %s, %s, %s, %s)"
|
||||
patientData = (
|
||||
itemRequest.app_id,
|
||||
itemRequest.notification_message,
|
||||
readType,
|
||||
itemRequest.action_path,
|
||||
notificationDateTime,
|
||||
itemRequest.notification_type
|
||||
)
|
||||
try:
|
||||
cursor.execute(query, patientData)
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise HTTPException(status_code=404, detail="Failed to Create Record")
|
||||
# return {"message": error}
|
||||
db.commit()
|
||||
cursor.close()
|
||||
db.close()
|
||||
return {"message": "Successfully Created Record"}
|
||||
|
||||
# # Update Patient on table
|
||||
# @router.put("/patients/update/", tags=["Patients"])
|
||||
# async def Update_Patient(itemRequest : patientUpdateRequest, session: SessionContainer = Depends(verify_session())):
|
||||
# db = database.dbConnection.dbPatientManagerConnect()
|
||||
# cursor = db.cursor()
|
||||
# query = "update patients "
|
||||
# query += "set id_no=%s, first_name=%s, last_name=%s, email=%s, cell_no=%s, medical_aid=%s, "
|
||||
# query += "medical_aid_main_member=%s, medical_aid_no=%s, medical_aid_code=%s, medical_aid_name=%s, "
|
||||
# query += "medical_aid_scheme=%s, address=%s, app_id=%s "
|
||||
# query += "where app_id=%s"
|
||||
# patientData = (itemRequest.id_no,
|
||||
# itemRequest.first_name,
|
||||
# itemRequest.last_name,
|
||||
# itemRequest.email,
|
||||
# itemRequest.cell_no,
|
||||
# itemRequest.medical_aid,
|
||||
# itemRequest.medical_aid_main_member,
|
||||
# itemRequest.medical_aid_no,
|
||||
# itemRequest.medical_aid_code,
|
||||
# itemRequest.medical_aid_name,
|
||||
# itemRequest.medical_aid_scheme,
|
||||
# itemRequest.address,
|
||||
# itemRequest.app_id,
|
||||
# itemRequest.app_id,)
|
||||
# try:
|
||||
# cursor.execute(query, patientData)
|
||||
# except Exception as error:
|
||||
# raise HTTPException(status_code=404, detail="Failed to Update Record")
|
||||
# #return {"query": query, "message": error}
|
||||
# db.commit()
|
||||
# cursor.close()
|
||||
# db.close()
|
||||
# return {"message": "Successfully Updated Record"}
|
||||
|
||||
# # delete Patient on table
|
||||
# @router.delete("/patients/delete/", tags=["Patients"])
|
||||
# async def Delete_Patient(itemRequest : patientDeleteRequest, session: SessionContainer = Depends(verify_session())):
|
||||
# db = database.dbConnection.dbPatientManagerConnect()
|
||||
# cursor = db.cursor()
|
||||
# query = "delete from patients "
|
||||
# query += "where app_id=%s"
|
||||
# patientData = (itemRequest.app_id,
|
||||
# )
|
||||
# try:
|
||||
# cursor.execute(query, patientData)
|
||||
# except Exception as error:
|
||||
# raise HTTPException(status_code=404, detail="Failed to delete Record")
|
||||
# #return {"query": query, "message": error}
|
||||
# db.commit()
|
||||
# cursor.close()
|
||||
# db.close()
|
||||
# return {"message": "Successfully delete Record"}
|
||||
Reference in New Issue
Block a user