1
1
from typing import Annotated
2
2
3
- from fastapi import Request , Depends , HTTPException
3
+ from fastapi import Request , Depends
4
4
from sqlalchemy .ext .asyncio import AsyncSession
5
5
import fastapi
6
6
10
10
from app .core .db .database import async_get_db
11
11
from app .crud .crud_posts import crud_posts
12
12
from 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
14
14
from app .core .utils .cache import cache
15
15
from app .api .paginated import PaginatedListResponse , paginated_response , compute_offset
16
16
@@ -26,10 +26,10 @@ async def write_post(
26
26
):
27
27
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
28
28
if db_user is None :
29
- raise HTTPException ( status_code = 404 , detail = "User not found" )
29
+ raise NotFoundException ( "User not found" )
30
30
31
31
if current_user ["id" ] != db_user ["id" ]:
32
- raise privileges_exception
32
+ raise ForbiddenException ()
33
33
34
34
post_internal_dict = post .model_dump ()
35
35
post_internal_dict ["created_by_user_id" ] = db_user ["id" ]
@@ -53,7 +53,7 @@ async def read_posts(
53
53
):
54
54
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
55
55
if not db_user :
56
- raise HTTPException ( status_code = 404 , detail = "User not found" )
56
+ raise NotFoundException ( "User not found" )
57
57
58
58
posts_data = await crud_posts .get_multi (
59
59
db = db ,
@@ -81,11 +81,11 @@ async def read_post(
81
81
):
82
82
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
83
83
if db_user is None :
84
- raise HTTPException ( status_code = 404 , detail = "User not found" )
84
+ raise NotFoundException ( "User not found" )
85
85
86
86
db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , created_by_user_id = db_user ["id" ], is_deleted = False )
87
87
if db_post is None :
88
- raise HTTPException ( status_code = 404 , detail = "Post not found" )
88
+ raise NotFoundException ( "Post not found" )
89
89
90
90
return db_post
91
91
@@ -106,14 +106,14 @@ async def patch_post(
106
106
):
107
107
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
108
108
if db_user is None :
109
- raise HTTPException ( status_code = 404 , detail = "User not found" )
109
+ raise NotFoundException ( "User not found" )
110
110
111
111
if current_user ["id" ] != db_user ["id" ]:
112
- raise privileges_exception
112
+ raise ForbiddenException ()
113
113
114
114
db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
115
115
if db_post is None :
116
- raise HTTPException ( status_code = 404 , detail = "Post not found" )
116
+ raise NotFoundException ( "Post not found" )
117
117
118
118
await crud_posts .update (db = db , object = values , id = id )
119
119
return {"message" : "Post updated" }
@@ -134,14 +134,14 @@ async def erase_post(
134
134
):
135
135
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
136
136
if db_user is None :
137
- raise HTTPException ( status_code = 404 , detail = "User not found" )
137
+ raise NotFoundException ( "User not found" )
138
138
139
139
if current_user ["id" ] != db_user ["id" ]:
140
- raise privileges_exception
140
+ raise ForbiddenException ()
141
141
142
142
db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
143
143
if db_post is None :
144
- raise HTTPException ( status_code = 404 , detail = "Post not found" )
144
+ raise NotFoundException ( "Post not found" )
145
145
146
146
await crud_posts .delete (db = db , db_row = db_post , id = id )
147
147
@@ -162,11 +162,11 @@ async def erase_db_post(
162
162
):
163
163
db_user = await crud_users .get (db = db , schema_to_select = UserRead , username = username , is_deleted = False )
164
164
if db_user is None :
165
- raise HTTPException ( status_code = 404 , detail = "User not found" )
165
+ raise NotFoundException ( "User not found" )
166
166
167
167
db_post = await crud_posts .get (db = db , schema_to_select = PostRead , id = id , is_deleted = False )
168
168
if db_post is None :
169
- raise HTTPException ( status_code = 404 , detail = "Post not found" )
169
+ raise NotFoundException ( "Post not found" )
170
170
171
171
await crud_posts .db_delete (db = db , id = id )
172
172
return {"message" : "Post deleted from the database" }
0 commit comments