|
| 1 | +from typing import List, Annotated |
| 2 | + |
| 3 | +from fastapi import Depends, HTTPException |
| 4 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | +import fastapi |
| 6 | + |
| 7 | +from app.schemas.post import PostCreate, PostUpdate, PostRead, PostCreateInternal |
| 8 | +from app.schemas.user import UserRead |
| 9 | +from app.api.dependencies import get_current_user, get_current_superuser |
| 10 | +from app.core.database import async_get_db |
| 11 | +from app.crud.crud_posts import crud_posts |
| 12 | +from app.crud.crud_users import crud_users |
| 13 | +from app.api.exceptions import privileges_exception |
| 14 | + |
| 15 | +router = fastapi.APIRouter(tags=["posts"]) |
| 16 | + |
| 17 | +@router.post("/{username}/post", response_model=PostRead, status_code=201) |
| 18 | +async def write_post( |
| 19 | + username: str, |
| 20 | + post: PostCreate, |
| 21 | + current_user: Annotated[UserRead, Depends(get_current_user)], |
| 22 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 23 | +): |
| 24 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 25 | + if db_user is None: |
| 26 | + raise HTTPException(status_code=404, detail="User not found") |
| 27 | + |
| 28 | + if current_user.id != db_user.id: |
| 29 | + raise privileges_exception |
| 30 | + |
| 31 | + post_internal_dict = post.model_dump() |
| 32 | + post_internal_dict["created_by_user_id"] = db_user.id |
| 33 | + post_internal = PostCreateInternal(**post_internal_dict) |
| 34 | + return await crud_posts.create(db=db, object=post_internal) |
| 35 | + |
| 36 | + |
| 37 | +@router.get("/{username}/posts", response_model=List[PostRead]) |
| 38 | +async def read_posts( |
| 39 | + username: str, |
| 40 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 41 | +): |
| 42 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 43 | + if db_user is None: |
| 44 | + raise HTTPException(status_code=404, detail="User not found") |
| 45 | + |
| 46 | + posts = await crud_posts.get_multi(db=db, created_by_user_id=db_user.id, is_deleted=False) |
| 47 | + return posts |
| 48 | + |
| 49 | + |
| 50 | +@router.get("/{username}/post/{id}", response_model=PostRead) |
| 51 | +async def read_post( |
| 52 | + username: str, |
| 53 | + id: int, |
| 54 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 55 | +): |
| 56 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 57 | + if db_user is None: |
| 58 | + raise HTTPException(status_code=404, detail="User not found") |
| 59 | + |
| 60 | + db_post = await crud_posts.get(db=db, id=id, created_by_user_id=db_user.id, is_deleted=False) |
| 61 | + if db_post is None: |
| 62 | + raise HTTPException(status_code=404, detail="Post not found") |
| 63 | + |
| 64 | + return db_post |
| 65 | + |
| 66 | + |
| 67 | +@router.patch("/{username}/post/{id}", response_model=PostRead) |
| 68 | +async def patch_post( |
| 69 | + username: str, |
| 70 | + id: int, |
| 71 | + values: PostUpdate, |
| 72 | + current_user: Annotated[UserRead, Depends(get_current_user)], |
| 73 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 74 | +): |
| 75 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 76 | + if db_user is None: |
| 77 | + raise HTTPException(status_code=404, detail="User not found") |
| 78 | + |
| 79 | + if current_user.id != db_user.id: |
| 80 | + raise privileges_exception |
| 81 | + |
| 82 | + db_post = await crud_posts.get(db=db, id=id, is_deleted=False) |
| 83 | + if db_post is None: |
| 84 | + raise HTTPException(status_code=404, detail="Post not found") |
| 85 | + |
| 86 | + return await crud_posts.update(db=db, object=values, db_object=db_post, id=id) |
| 87 | + |
| 88 | + |
| 89 | +@router.delete("/{username}/post/{id}") |
| 90 | +async def erase_post( |
| 91 | + username: str, |
| 92 | + id: int, |
| 93 | + current_user: Annotated[UserRead, Depends(get_current_user)], |
| 94 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 95 | +): |
| 96 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 97 | + if db_user is None: |
| 98 | + raise HTTPException(status_code=404, detail="User not found") |
| 99 | + |
| 100 | + if current_user.id != db_user.id: |
| 101 | + raise privileges_exception |
| 102 | + |
| 103 | + db_post = await crud_posts.get(db=db, id=id, is_deleted=False) |
| 104 | + if db_post is None: |
| 105 | + raise HTTPException(status_code=404, detail="Post not found") |
| 106 | + |
| 107 | + deleted_post = await crud_posts.delete(db=db, db_object=db_post, id=id) |
| 108 | + if deleted_post.is_deleted == True: |
| 109 | + message = {"message": "Post deleted"} |
| 110 | + else: |
| 111 | + message = {"message": "Something went wrong"} |
| 112 | + |
| 113 | + return message |
| 114 | + |
| 115 | + |
| 116 | +@router.delete("/{username}/db_post/{id}") |
| 117 | +async def erase_db_post( |
| 118 | + username: str, |
| 119 | + id: int, |
| 120 | + current_superuser: Annotated[UserRead, Depends(get_current_superuser)], |
| 121 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 122 | +): |
| 123 | + db_user = await crud_users.get(db=db, username=username, is_deleted=False) |
| 124 | + if db_user is None: |
| 125 | + raise HTTPException(status_code=404, detail="User not found") |
| 126 | + |
| 127 | + db_post = await crud_posts.get(db=db, id=id, is_deleted=False) |
| 128 | + if db_post is None: |
| 129 | + raise HTTPException(status_code=404, detail="Post not found") |
| 130 | + |
| 131 | + await crud_posts.db_delete(db=db, db_object=db_post, id=id) |
| 132 | + deleted_post = await crud_posts.get(db=db, id=id) |
| 133 | + |
| 134 | + if deleted_post is None: |
| 135 | + message = {"message": "Post deleted"} |
| 136 | + else: |
| 137 | + message = {"message": "Something went wrong"} |
| 138 | + |
| 139 | + return message |
0 commit comments