66from app .models import Post , Vote
77from app .schemas import PostCreateUpdate , PostSchema , PostOut
88from sqlalchemy import func
9+ from app .utils import sanitize_input
910
1011router = APIRouter (prefix = "/posts" , tags = ["Post" ])
1112
@@ -37,6 +38,8 @@ async def create_post(
3738 db : SessionDep ,
3839 current_user = Depends (oauth2 .get_current_user ),
3940):
41+ post_data .title = sanitize_input (post_data .title )
42+ post_data .content = sanitize_input (post_data .content )
4043 new_post = Post (owner_id = current_user .id , ** post_data .model_dump ())
4144 db .add (new_post )
4245 db .commit ()
@@ -58,7 +61,7 @@ async def get_post(
5861 if not post :
5962 raise HTTPException (
6063 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" ,
6265 )
6366 return post
6467
@@ -75,14 +78,17 @@ async def update_post(
7578 if not existing_post :
7679 raise HTTPException (
7780 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" ,
7982 )
8083 if existing_post .owner_id != current_user .id :
8184 raise HTTPException (
8285 status_code = status .HTTP_401_UNAUTHORIZED ,
8386 detail = "Not authorised to perform requsted action" ,
8487 )
85-
88+ if post .title :
89+ post .title = sanitize_input (post .title )
90+ if post .content :
91+ post .content = sanitize_input (post .content )
8692 post_data = post .model_dump (exclude_unset = True )
8793 existing_post .sqlmodel_update (post_data )
8894 db .add (existing_post )
@@ -100,7 +106,7 @@ async def delete_post(
100106 if not deleted_post :
101107 raise HTTPException (
102108 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" ,
104110 )
105111 if deleted_post .owner_id != current_user .id :
106112 raise HTTPException (
@@ -110,4 +116,4 @@ async def delete_post(
110116
111117 db .delete (deleted_post )
112118 db .commit ()
113- return deleted_post
119+ return
0 commit comments