Spaces:
Runtime error
Runtime error
Download app/routers/vote.py from abdullah090809/FastAPI-Project: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/abdullah090809/FastAPI-Project/resolve/main/app/routers/vote.py
- Command line
-
hf download hf://spaces/abdullah090809/FastAPI-Project/app/routers/vote.py
-
curl -L -o vote.py https://huggingface.co/spaces/abdullah090809/FastAPI-Project/resolve/main/app/routers/vote.py
1.55 kB
| from fastapi import APIRouter, HTTPException, Depends, status, Response | |
| from sqlalchemy.orm import Session | |
| from app.database import get_db | |
| from app.models import User, Votes, Post | |
| from app.oauth2 import Get_Current_User | |
| from app.schemas import VoteBase | |
| router = APIRouter( | |
| prefix="/vote", | |
| tags=["Votes"] | |
| ) | |
| def vote(vote: VoteBase, db: Session = Depends(get_db), current_user: User = Depends(Get_Current_User)): | |
| post = db.query(Post).filter(Post.id==vote.post_id).first() | |
| if not post: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f"Post with {vote.post_id} does not Exit") | |
| else: | |
| vote_query=db.query(Votes).filter(Votes.post_id==vote.post_id,Votes.user_id==current_user.id) | |
| found_vote=vote_query.first() | |
| if vote.dir==1: | |
| if found_vote: | |
| raise HTTPException(status_code=status.HTTP_409_CONFLICT,detail=f"User with id {current_user.id} has already voted on this Post") | |
| new_vote=Votes(post_id=vote.post_id,user_id=current_user.id) | |
| db.add(new_vote) | |
| db.commit() | |
| return{ | |
| "message": "Successfully Voted" | |
| } | |
| else: | |
| if not found_vote: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail="Vote does not Exit") | |
| vote_query.delete(synchronize_session=False) | |
| db.commit() | |
| return{ | |
| "message": "Vote Successfully Deleted" | |
| } | |