summaryrefslogtreecommitdiff
path: root/app/routers/vote.py
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-02-06 13:41:11 +0200
committerRasmus Luha <rasmus.luha@gmail.com>2022-02-06 13:41:11 +0200
commit6a6afdbe72c626b01245c9372c9d10be79789bb0 (patch)
treecfdb5aea7538296bad0105c813fce4cd33c19ef5 /app/routers/vote.py
parent5e19a0569288de21365c61b0db78639880732dd0 (diff)
restrucurring the stucture of the folderstruture --> structure
Diffstat (limited to 'app/routers/vote.py')
-rw-r--r--app/routers/vote.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/routers/vote.py b/app/routers/vote.py
new file mode 100644
index 0000000..af17b88
--- /dev/null
+++ b/app/routers/vote.py
@@ -0,0 +1,45 @@
+from fastapi import APIRouter, Depends, status, HTTPException, Response
+from sqlalchemy.orm import Session
+from .. import schemas, database, models, oauth2
+
+router = APIRouter(
+ prefix="/vote",
+ tags=["vote"]
+)
+
+
+@router.post("/", status_code=status.HTTP_201_CREATED)
+def vote(vote: schemas.Vote, db: Session = Depends(database.get_db)
+ , current_user: int = Depends(oauth2.get_current_user) ):
+
+ post = db.query(models.Post).filter(models.Post.id == vote.post_id).first() # Cheki seda, et ei proovi likeda posti mida ei eksisteeri
+ if not post:
+ raise HTTPException(status_code=status.HTTP404_NOT_FOUND,
+ detail=f"The post with id {vote.id} that you are trying to like doesn't exsits")
+
+
+ vote_query = db.query(models.Vote).filter(models.Vote.post_id == vote.post_id,
+ models.Vote.user_id == current_user.id)
+ found_vote=vote_query.first() # kui oled juba posti likenud, siis filter leiab, kui ei ole, siis ei leia
+
+ if (vote.dir == 1):
+ if found_vote:
+ raise HTTPException(status_code=status.HTTP_409_CONFLICT,
+ detail=f"user {current_user.email} has alreadey liked the post {vote.post_id}")
+ new_vote = models.Vote(post_id = vote.post_id, user_id = current_user.id)
+ db.add(new_vote)
+ db.commit()
+ return {"Message":"Successfully liked Post"}
+ else:
+ if not found_vote:
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
+ detail=f"user {current_user.email} has not liked the post {vote.post_id}")
+ vote_query.delete(synchronize_session=False)
+ db.commit()
+
+ return {"Message":"Succesfulle Unliked the Post"}
+
+
+
+
+