diff options
author | Rasmus Luha <rasmus.luha@gmail.com> | 2022-02-06 13:41:11 +0200 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@gmail.com> | 2022-02-06 13:41:11 +0200 |
commit | 6a6afdbe72c626b01245c9372c9d10be79789bb0 (patch) | |
tree | cfdb5aea7538296bad0105c813fce4cd33c19ef5 /app/routers/post.py | |
parent | 5e19a0569288de21365c61b0db78639880732dd0 (diff) |
restrucurring the stucture of the folderstruture --> structure
Diffstat (limited to 'app/routers/post.py')
-rw-r--r-- | app/routers/post.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/app/routers/post.py b/app/routers/post.py new file mode 100644 index 0000000..ce1c11d --- /dev/null +++ b/app/routers/post.py @@ -0,0 +1,102 @@ +from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter +from .. import models, schemas, oauth2 +from sqlalchemy import func +from sqlalchemy.orm import Session +from ..database import get_db +from typing import List, Optional + +router = APIRouter(prefix="/posts", tags=["Posts"]) + + +@router.get("/", response_model=List[schemas.PostOut]) # Siin List (from typing lib), sest võttame mitu posti, teistes vaid 1 +async 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] = ""): + + #cursor.execute(""" SELECT * FROM posts """) + #posts = cursor.fetchall() + + + #Lots of query parameter stuff here, kinda testing atm. + #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() + + return posts #FastAPI converts automaticaly into json types stuff + + +@router.get("/{id}", response_model=schemas.PostOut) #ID is called "path parameter" , could name it dingdong if wanted. Its just like a argument/variable +async def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)): #id: int > Et url ei oleks "afasdfasdfasf" <-- Siis viskab errori. Response imported from FastAPI + #cursor.execute(""" SELECT * FROM posts WHERE id = %s RETURNING *""", (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() + + + if not post: + raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, + detail=f"Post with id: {id} was not found") + return post + + +@router.post("/", status_code = status.HTTP_201_CREATED, response_model=schemas.Post) # Create puhul default kood 201. Miks mitte lihtsalt "201", miks status.blabblalba +async def create_posts(post: schemas.PostCreate,db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user) ): # Ekstraktib body read dictiks "pay.. + #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() + print(current_user.email) + + #new_post = models.Post(title=post.title, content=post.content, published=post.published) + new_post = models.Post(owner_id=current_user.id, **post.dict()) # Sama mis ülal, ilusamini + + + db.add(new_post) + db.commit() # not sure why have to add+commit, but hey + db.refresh(new_post) # sama mis SQL: "RESPONDING *" + return new_post + +@router.put("/{id}") +def update_post(id: int, post: schemas.PostCreate, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user) ): + + post_query = db.query(models.Post).filter(models.Post.id == id) + posters = post_query.first() + + if posters == None: + raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, detail = f"post with id: {id} was not found") + + + if posters.owner_id != current_user.id: + raise HTTPException(status_code = status.HTTP_403_FORBIDDEN, + detail=f"Not authorized duuud") + + post_query.update(post.dict(), synchronize_session=False) + + db.commit() + + return post_query.first() + + +@router.delete("/{id}", status_code = status.HTTP_204_NO_CONTENT) +async 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() + + if post == None: + raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, + detail=f"Post with id: {id} was not found") + + if post.owner_id != current_user.id: + raise HTTPException(status_code = status.HTTP_403_FORBIDDEN, + detail=f"Not authorized duuud") + + post_query.delete(synchronize_session=False) # Ei tea mis teeb, fastapi docs ütles et panna, vist ka default + db.commit() + + return Response(status_code = status.HTTP_204_NO_CONTENT) |