|
| 1 | +from typing import List |
| 2 | +from app import oauth2 |
| 3 | +from .. import schemas, models |
| 4 | +from sqlalchemy.orm import Session |
| 5 | +from fastapi import Depends, HTTPException, status, APIRouter, Response |
| 6 | +from ..database import get_db |
| 7 | + |
| 8 | +router = APIRouter() |
| 9 | + |
| 10 | + |
| 11 | +@router.get("/") |
| 12 | +def get_posts(db: Session = Depends(get_db), limit: int = 10, page: int = 1, search: str = ''): |
| 13 | + skip = (page - 1) * limit |
| 14 | + |
| 15 | + posts = db.query(models.Post).group_by(models.Post.id).filter( |
| 16 | + models.Post.title.contains(search)).limit(limit).offset(skip).all() |
| 17 | + return {'status': 'success', 'results': len(posts), 'data': posts} |
| 18 | + |
| 19 | + |
| 20 | +@router.post('/', status_code=status.HTTP_201_CREATED, response_model=schemas.PostResponse) |
| 21 | +def create_post(post: schemas.CreatePostSchema, db: Session = Depends(get_db), user_id: str = Depends(oauth2.require_user)): |
| 22 | + new_post = models.Post(**post.dict(), user_id=user_id) |
| 23 | + db.add(new_post) |
| 24 | + db.commit() |
| 25 | + db.refresh(new_post) |
| 26 | + return new_post |
| 27 | + |
| 28 | + |
| 29 | +@router.put('/{id}', response_model=schemas.PostResponse) |
| 30 | +def update_post(id: str, post: schemas.CreatePostSchema, db: Session = Depends(get_db), user_id: str = Depends(oauth2.require_user)): |
| 31 | + post_query = db.query(models.Post).filter(models.Post.id == id) |
| 32 | + updated_post = post_query.first() |
| 33 | + |
| 34 | + if not updated_post: |
| 35 | + raise HTTPException(status_code=status.HTTP_200_OK, |
| 36 | + detail=f'No post with this id: {id} found') |
| 37 | + if updated_post.owner_id != user_id: |
| 38 | + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, |
| 39 | + detail='You are not allowed to perform this action') |
| 40 | + post_query.update(post.dict(), synchronize_session=False) |
| 41 | + db.commit() |
| 42 | + return updated_post |
| 43 | + |
| 44 | + |
| 45 | +@router.get('/{id}', response_model=schemas.PostResponse) |
| 46 | +def get_post(id: str, db: Session = Depends(get_db), user_id: str = Depends(oauth2.require_user)): |
| 47 | + post = db.query(models.Post).filter(models.Post.id == id).first() |
| 48 | + if not post: |
| 49 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 50 | + detail=f"No post with this id: {id} found") |
| 51 | + return post |
| 52 | + |
| 53 | + |
| 54 | +@router.delete('/{id}') |
| 55 | +def delete_post(id: int, db: Session = Depends(get_db), user_id: str = Depends(oauth2.require_user)): |
| 56 | + post_query = db.query(models.Post).filter(models.Post.id == id) |
| 57 | + post = post_query.first() |
| 58 | + if not post: |
| 59 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 60 | + detail=f'No post with this id: {id} found') |
| 61 | + |
| 62 | + if post.owner_id != user_id: |
| 63 | + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, |
| 64 | + detail='You are not allowed to perform this action') |
| 65 | + post_query.delete(synchronize_session=False) |
| 66 | + db.commit() |
| 67 | + return Response(status_code=status.HTTP_204_NO_CONTENT) |