Skip to content

Commit

Permalink
Merge pull request #13 from TebogoYungMercykay/feature_01
Browse files Browse the repository at this point in the history
The Python FastAPI Implementation Files
  • Loading branch information
TebogoYungMercykay authored Dec 22, 2023
2 parents 8947c7c + 414e445 commit bfab454
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 92 deletions.
36 changes: 0 additions & 36 deletions app/calculations.py

This file was deleted.

14 changes: 0 additions & 14 deletions app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,3 @@ def get_db():
yield db
finally:
db.close()


# while True:

# try:
# conn = psycopg2.connect(host='localhost', database='fastapi', user='postgres',
# password='password123', cursor_factory=RealDictCursor)
# cursor = conn.cursor()
# print("Database connection was succesfull!")
# break
# except Exception as error:
# print("Connecting to database failed")
# print("Error: ", error)
# time.sleep(2)
5 changes: 4 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from . import models
from .database import engine
from .routers import post, user, auth, vote
from .routers import post, user, auth, vote, consultations, disease_prediction, chats
from .config import settings

models.Base.metadata.create_all(bind=engine)
Expand All @@ -24,6 +24,9 @@
app.include_router(user.router)
app.include_router(auth.router)
app.include_router(vote.router)
app.include_router(consultations.router)
app.include_router(disease_prediction.router)
app.include_router(chats.router)


@app.get("/")
Expand Down
7 changes: 4 additions & 3 deletions app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def login(user_credentials: OAuth2PasswordRequestForm = Depends(), db: Session =
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail=f"Invalid Credentials")

# create a token
# return token

access_token = oauth2.create_access_token(data={"user_id": user.id})

return {"access_token": access_token, "token_type": "bearer"}

@router.post('/logout')
def logout():
return { "status":"pending", "message": "Functionality Under Construction." }
40 changes: 40 additions & 0 deletions app/routers/chats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter
from sqlalchemy.orm import Session
from typing import List, Optional

from sqlalchemy import func
from .. import models, schemas, oauth2
from ..database import get_db


MESSAGE_UNDER_CONSTRUCTION = "Functionality Under Construction."

router = APIRouter(
prefix="/chats",
tags=['Chats']
)

@router.get('/')
def chat_messages():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.get('/get_feedback')
def get_feedback():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/post_feedback')
def post_feedback():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/whatsapp')
def whatsapp():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/meeting')
def meeting():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }

44 changes: 44 additions & 0 deletions app/routers/consultations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter
from sqlalchemy.orm import Session
from typing import List, Optional

from sqlalchemy import func
from .. import models, schemas, oauth2
from ..database import get_db

MESSAGE_UNDER_CONSTRUCTION = "Functionality Under Construction."

router = APIRouter(
prefix="/consultations",
tags=['Consultations']
)

@router.get('/')
def consult_a_doctor():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/make_consultation')
def make_consultation():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.get('/consultation_history')
def consultation_history():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.get('/consultation_view')
def consultation_view():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/close_consultation')
def close_consultation():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/rate_review')
def rate_review():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }

24 changes: 24 additions & 0 deletions app/routers/disease_prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter
from sqlalchemy.orm import Session
from typing import List, Optional

from sqlalchemy import func
from .. import models, schemas, oauth2
from ..database import get_db

MESSAGE_UNDER_CONSTRUCTION = "Functionality Under Construction."

router = APIRouter(
prefix="/disease_prediction",
tags=['Disease Prediction Model']
)

@router.get('/')
def checkdisease():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }


@router.post('/checkdisease')
def checkdisease():
return { "status":"pending", "message": MESSAGE_UNDER_CONSTRUCTION }

42 changes: 6 additions & 36 deletions app/routers/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,12 @@


router = APIRouter(
prefix="/posts", # prefix for all routes
prefix="/posts",
tags=['Posts']
)


# @router.get("/", response_model=List[schemas.Post])
@router.get("/", response_model=List[schemas.PostOut])
def get_posts(db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user), limit: int = 10, skip: int = 0, search: Optional[str] = ""):
# results = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
# models.Vote, models.Vote.post_id == models.Post.id, isouter=True).group_by(models.Post.id)

# cursor.execute("""SELECT * FROM posts """)
# posts = cursor.fetchall()

# posts = db.execute(
# 'select posts.*, COUNT(votes.post_id) as votes from posts LEFT JOIN votes ON posts.id=votes.post_id group by posts.id')
# results = []
# for post in posts:
# results.append(dict(post))
# print(results)
# posts = db.query(models.Post).filter(
# models.Post.title.contains(search)).limit(limit).offset(skip).all()

posts = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
models.Vote, models.Vote.post_id == models.Post.id, isouter=True).group_by(models.Post.id).filter(models.Post.title.contains(search)).limit(limit).offset(skip).all()
Expand All @@ -38,11 +22,6 @@ def get_posts(db: Session = Depends(get_db), current_user: int = Depends(oauth2.

@router.post("/", status_code=status.HTTP_201_CREATED, response_model=schemas.Post)
def create_posts(post: schemas.PostCreate, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)):
# cursor.execute("""INSERT INTO posts (title, content, published) VALUES (%s, %s, %s) RETURNING * """,
# (post.title, post.content, post.published))
# new_post = cursor.fetchone()

# conn.commit()

new_post = models.Post(owner_id=current_user.id, **post.dict())
db.add(new_post)
Expand All @@ -54,9 +33,6 @@ def create_posts(post: schemas.PostCreate, db: Session = Depends(get_db), curren

@router.get("/{id}", response_model=schemas.PostOut)
def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)):
# cursor.execute("""SELECT * from posts WHERE id = %s """, (str(id),))
# post = cursor.fetchone()
# post = db.query(models.Post).filter(models.Post.id == id).first()

post = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
models.Vote, models.Vote.post_id == models.Post.id, isouter=True).group_by(models.Post.id).filter(models.Post.id == id).first()
Expand All @@ -66,16 +42,11 @@ def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends
detail=f"post with id: {id} was not found")

return post
# return { 'status': "success", "data": post }


@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)):

# cursor.execute(
# """DELETE FROM posts WHERE id = %s returning *""", (str(id),))
# deleted_post = cursor.fetchone()
# conn.commit()
post_query = db.query(models.Post).filter(models.Post.id == id)

post = post_query.first()
Expand All @@ -97,12 +68,6 @@ def delete_post(id: int, db: Session = Depends(get_db), current_user: int = Depe
@router.put("/{id}", response_model=schemas.Post)
def update_post(id: int, updated_post: schemas.PostCreate, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)):

# cursor.execute("""UPDATE posts SET title = %s, content = %s, published = %s WHERE id = %s RETURNING *""",
# (post.title, post.content, post.published, str(id)))

# updated_post = cursor.fetchone()
# conn.commit()

post_query = db.query(models.Post).filter(models.Post.id == id)

post = post_query.first()
Expand All @@ -120,3 +85,8 @@ def update_post(id: int, updated_post: schemas.PostCreate, db: Session = Depends
db.commit()

return post_query.first()


@router.get('/create_reply')
def create_reply():
return { "status":"pending", "message": "Functionality Under Construction." }
8 changes: 6 additions & 2 deletions app/routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

router = APIRouter(
prefix="/users",
tags=['Users']
tags=['Accounts']
)

@router.post("/", status_code=status.HTTP_201_CREATED, response_model=schemas.UserOut)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):

# hash the password - user.password
hashed_password = utils.hash(user.password)
user.password = hashed_password

Expand All @@ -31,3 +30,8 @@ def get_user(id: int, db: Session = Depends(get_db), ):
detail=f"User with id: {id} does not exist")

return user


@router.put('/savedata')
def savedata():
return { "status":"pending", "message": "Functionality Under Construction." }

0 comments on commit bfab454

Please sign in to comment.