Commit 9ecdd57a authored by Hansi rathnayaka's avatar Hansi rathnayaka

API implementation

parent d9dd7112
from pymongo import MongoClient
conn = MongoClient("mongodb+srv://skin-disease:skindis123@cluster0.bv8ia.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
conn = MongoClient("mongodb+srv://skin-disease:skindis123@cluster0.bv8ia.mongodb.net/skinDiseaseDB?retryWrites=true&w=majority")
#mongodb+srv://skin-disease:skindis123@cluster0.bv8ia.mongodb.net/test
\ No newline at end of file
from fastapi import FastAPI
from routes.doctor import doctor
app = FastAPI()
app.include_router(doctor)
from pydantic import BaseModel
from typing import List
from typing import Optional
from models.isAvailable import IsAvailable
class Doctor(BaseModel):
id: str
name: str
phone: str
isAvailable: Optional[List[IsAvailable]] = []
\ No newline at end of file
from typing import List
from models.isAvailable import IsAvailable
from re import A
import re
from os import error
from bson.objectid import ObjectId
from schemas.doctor import doctorEntity, doctorsEntity
from schemas.availability import availableEntity, availablesEntity
from fastapi import APIRouter
from models.doctor import Doctor
from config.db import conn
doctor = APIRouter()
@doctor.get('/api/doctors')
async def find_all_doctors():
return doctorsEntity(conn.skinDiseaseDB.doctor.find())
@doctor.get('/api/doctor/{id}')
async def find_one_doctor(id):
try:
return doctorEntity(conn.skinDiseaseDB.doctor.find_one({"id":id}))
except error:
print(error)
return {"success": False, "msg": error}
@doctor.post('/api/doctor')
async def create_doctor(doctor: Doctor):
try:
conn.skinDiseaseDB.doctor.insert_one(dict(doctor))
return doctorEntity(conn.skinDiseaseDB.doctor.find_one({"id":doctor.id}))
#return {"success": True}
except error:
return {"success": False, "msg": error}
@doctor.put('/api/doctor/{id}')
async def update_doctor(id, doctor: Doctor):
try:
conn.skinDiseaseDB.doctor.find_one_and_update({"id":id},{"$set": dict(doctor)})
return doctorEntity(conn.skinDiseaseDB.doctor.find_one({"id": id}))
except error:
return {"success": False, "msg": error}
@doctor.delete('/api/doctor/{id}')
async def delete_doctor(id):
try:
result = doctorEntity(conn.skinDiseaseDB.doctor.find_one_and_delete({"id": id}))
return {"success": True}
except:
return {"success": False}
@doctor.put('/api/availability/{id}')
async def update_availability(id, availabilities: List[IsAvailable]):
try:
available_dic = dict()
for available in availabilities:
print(available)
#conn.skinDiseaseDB.doctor.find_one_and_update({"id":id},{"$set": {'isAvailable': isAvailable}})
return availabilities
except error:
return {"success": False, "msg": error}
def availableEntity(item) -> dict:
return {
"day": item["day"],
"availability": item["availability"]
}
def availablesEntity(entity) -> list:
print(entity)
return [availableEntity(item) for item in entity]
\ No newline at end of file
from pydantic import BaseModel
from typing import List
from models.isAvailable import IsAvailable
class Doctor(BaseModel):
name: str
phone: str
IsAvailable: List[IsAvailable]
\ No newline at end of file
def doctorEntity(item) -> dict:
return {
"id":str(item["id"]),
"name": item["name"],
"phone": item["phone"],
"isAvailable": item["isAvailable"]
}
def doctorsEntity(entity) -> list:
return [doctorEntity(item) for item in entity]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment