11from typing import Annotated
22
3- from fastapi import Request , Depends , HTTPException
3+ from fastapi import Request , Depends
44from sqlalchemy .ext .asyncio import AsyncSession
55import fastapi
66
1010from app .core .db .database import async_get_db
1111from app .crud .crud_posts import crud_posts
1212from app .crud .crud_users import crud_users
13- from app .api .exceptions import privileges_exception
13+ from app .core .exceptions . http_exceptions import NotFoundException , ForbiddenException
1414from app .core .utils .cache import cache
1515from app .api .paginated import PaginatedListResponse , paginated_response , compute_offset
1616
@@ -26,10 +26,10 @@ async def write_post(
2626):
2727 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
2828 if db_user is None :
29- raise HTTPException ( status_code = 404 , detail = "User not found" )
29+ raise NotFoundException ( "User not found" )
3030
3131 if current_user ["id" ] != db_user ["id" ]:
32- raise privileges_exception
32+ raise ForbiddenException ()
3333
3434 post_internal_dict = post .model_dump ()
3535 post_internal_dict ["created_by_user_id" ] = db_user ["id" ]
@@ -53,7 +53,7 @@ async def read_posts(
5353):
5454 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
5555 if not db_user :
56- raise HTTPException ( status_code = 404 , detail = "User not found" )
56+ raise NotFoundException ( "User not found" )
5757
5858 posts_data = await crud_posts .get_multi (
5959 db = db ,
@@ -81,11 +81,11 @@ async def read_post(
8181):
8282 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
8383 if db_user is None :
84- raise HTTPException ( status_code = 404 , detail = "User not found" )
84+ raise NotFoundException ( "User not found" )
8585
8686 db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , created_by_user_id = db_user ["id" ], is_deleted = False )
8787 if db_post is None :
88- raise HTTPException ( status_code = 404 , detail = "Post not found" )
88+ raise NotFoundException ( "Post not found" )
8989
9090 return db_post
9191
@@ -106,14 +106,14 @@ async def patch_post(
106106):
107107 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
108108 if db_user is None :
109- raise HTTPException ( status_code = 404 , detail = "User not found" )
109+ raise NotFoundException ( "User not found" )
110110
111111 if current_user ["id" ] != db_user ["id" ]:
112- raise privileges_exception
112+ raise ForbiddenException ()
113113
114114 db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
115115 if db_post is None :
116- raise HTTPException ( status_code = 404 , detail = "Post not found" )
116+ raise NotFoundException ( "Post not found" )
117117
118118 await crud_posts .update (db = db , object = values , id = id )
119119 return {"message" : "Post updated" }
@@ -134,14 +134,14 @@ async def erase_post(
134134):
135135 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
136136 if db_user is None :
137- raise HTTPException ( status_code = 404 , detail = "User not found" )
137+ raise NotFoundException ( "User not found" )
138138
139139 if current_user ["id" ] != db_user ["id" ]:
140- raise privileges_exception
140+ raise ForbiddenException ()
141141
142142 db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
143143 if db_post is None :
144- raise HTTPException ( status_code = 404 , detail = "Post not found" )
144+ raise NotFoundException ( "Post not found" )
145145
146146 await crud_posts .delete (db = db , db_row = db_post , id = id )
147147
@@ -162,11 +162,11 @@ async def erase_db_post(
162162):
163163 db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
164164 if db_user is None :
165- raise HTTPException ( status_code = 404 , detail = "User not found" )
165+ raise NotFoundException ( "User not found" )
166166
167167 db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
168168 if db_post is None :
169- raise HTTPException ( status_code = 404 , detail = "Post not found" )
169+ raise NotFoundException ( "Post not found" )
170170
171171 await crud_posts .db_delete (db = db , id = id )
172172 return {"message" : "Post deleted from the database" }
0 commit comments