@@ -163,15 +163,54 @@ async def read_blog(
163163 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
164164 detail = "Database error occurred." ,
165165 )
166- except ValueError as e :
167- logger .error (f"Invalid blog post ID '{ id } ': { e } " )
166+ except Exception as e :
167+ logger .error (f"Unexpected error occurred: { e } " )
168+ raise HTTPException (
169+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
170+ detail = "Unexpected error occurred." ,
171+ )
172+
173+
174+ @blog .delete (
175+ "/{id}" ,
176+ status_code = status .HTTP_204_NO_CONTENT ,
177+ )
178+ async def delete_blog (
179+ id : int ,
180+ db : Session = Depends (get_db ),
181+ ) -> None :
182+ try :
183+ blog_to_delete = (
184+ db .query (Blog )
185+ .filter (
186+ Blog .id == id ,
187+ not_ (Blog .is_deleted ),
188+ )
189+ .first ()
190+ )
191+ if not blog_to_delete :
192+ logger .warning (f"Blog post with ID '{ id } ' not found." )
193+ raise HTTPException (
194+ status_code = status .HTTP_404_NOT_FOUND ,
195+ detail = "Blog post with given id not found." ,
196+ )
197+
198+ blog_to_delete .is_deleted = True
199+ db .commit ()
200+ logger .info (f"Blog post '{ blog_to_delete .title } ' deleted successfully." )
201+
202+ except SQLAlchemyError as e :
203+ logger .error (f"Database error occurred: { e } " )
204+ db .rollback ()
168205 raise HTTPException (
169- status_code = status .HTTP_400_BAD_REQUEST ,
170- detail = "Invalid blog post ID ." ,
206+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
207+ detail = "Database error occurred ." ,
171208 )
209+
172210 except Exception as e :
173211 logger .error (f"Unexpected error occurred: { e } " )
212+ db .rollback ()
174213 raise HTTPException (
175214 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
176- detail = "Unexpected error occurred ." ,
215+ detail = "Internal server error ." ,
177216 )
0 commit comments