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