6
6
from app .models import Post , Vote
7
7
from app .schemas import PostCreateUpdate , PostSchema , PostOut
8
8
from sqlalchemy import func
9
+ from app .utils import sanitize_input
9
10
10
11
router = APIRouter (prefix = "/posts" , tags = ["Post" ])
11
12
@@ -37,6 +38,8 @@ async def create_post(
37
38
db : SessionDep ,
38
39
current_user = Depends (oauth2 .get_current_user ),
39
40
):
41
+ post_data .title = sanitize_input (post_data .title )
42
+ post_data .content = sanitize_input (post_data .content )
40
43
new_post = Post (owner_id = current_user .id , ** post_data .model_dump ())
41
44
db .add (new_post )
42
45
db .commit ()
@@ -58,7 +61,7 @@ async def get_post(
58
61
if not post :
59
62
raise HTTPException (
60
63
status_code = status .HTTP_404_NOT_FOUND ,
61
- detail = f"post with id: { id } doesn't exist" ,
64
+ detail = f"post with id: { id } was not exist" ,
62
65
)
63
66
return post
64
67
@@ -75,14 +78,17 @@ async def update_post(
75
78
if not existing_post :
76
79
raise HTTPException (
77
80
status_code = status .HTTP_404_NOT_FOUND ,
78
- detail = f"post with id: { id } doesn't found" ,
81
+ detail = f"post with id: { id } was not found" ,
79
82
)
80
83
if existing_post .owner_id != current_user .id :
81
84
raise HTTPException (
82
85
status_code = status .HTTP_401_UNAUTHORIZED ,
83
86
detail = "Not authorised to perform requsted action" ,
84
87
)
85
-
88
+ if post .title :
89
+ post .title = sanitize_input (post .title )
90
+ if post .content :
91
+ post .content = sanitize_input (post .content )
86
92
post_data = post .model_dump (exclude_unset = True )
87
93
existing_post .sqlmodel_update (post_data )
88
94
db .add (existing_post )
@@ -100,7 +106,7 @@ async def delete_post(
100
106
if not deleted_post :
101
107
raise HTTPException (
102
108
status_code = status .HTTP_404_NOT_FOUND ,
103
- detail = f"post with id: { id } doesn't found" ,
109
+ detail = f"post with id: { id } was not found" ,
104
110
)
105
111
if deleted_post .owner_id != current_user .id :
106
112
raise HTTPException (
@@ -110,4 +116,4 @@ async def delete_post(
110
116
111
117
db .delete (deleted_post )
112
118
db .commit ()
113
- return deleted_post
119
+ return
0 commit comments