1+ """API routes and handlers for blog-related operations."""
2+
13import logging
24
35from fastapi import APIRouter , Depends , HTTPException , Query , status
@@ -28,8 +30,7 @@ async def create_blog(
2830 blog : BlogCreate ,
2931 db : Session = Depends (get_db ),
3032) -> BlogResponse :
31- """
32- Create a new blog post.
33+ """Create a new blog post.
3334
3435 Args:
3536 blog (BlogCreate): The blog post data to create.
@@ -45,23 +46,23 @@ async def create_blog(
4546 try :
4647 return BlogService .create_blog (db , blog )
4748 except ValueError as e :
48- logger .warning (str (e ))
49+ logger .warning ("Value error occurred: %s" , str (e ))
4950 raise HTTPException (
5051 status_code = status .HTTP_409_CONFLICT ,
5152 detail = str (e ),
52- )
53+ ) from e
5354 except SQLAlchemyError as e :
54- logger .error ( f "Database error occurred: { e } " )
55+ logger .exception ( "Database error occurred" , exc_info = e )
5556 raise HTTPException (
5657 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
5758 detail = "Database error occurred." ,
58- )
59+ ) from e
5960 except Exception as e :
60- logger .error ( f "Internal server error: { e } " )
61+ logger .exception ( "Internal server error" , exc_info = e )
6162 raise HTTPException (
6263 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
6364 detail = "Internal server error." ,
64- )
65+ ) from e
6566
6667
6768@blog .get (
@@ -71,11 +72,10 @@ async def create_blog(
7172)
7273async def list_blog (
7374 page : int = Query (1 , ge = 1 ),
74- page_size : int = Query (10 , ge = 1 , le = 100 ),
75+ page_size : int = Query (10 , ge = 1 , le = 25 ),
7576 db : Session = Depends (get_db ),
7677) -> BlogListResponse :
77- """
78- Retrieve a list of blog posts with pagination.
78+ """Retrieve a list of blog posts with pagination.
7979
8080 Args:
8181 page (int): The page number for pagination.
@@ -91,17 +91,17 @@ async def list_blog(
9191 try :
9292 return BlogService .list_blog (db , page , page_size )
9393 except SQLAlchemyError as e :
94- logger .error ( f "Database error occurred: { e } " )
94+ logger .exception ( "Database error occurred" , exc_info = e )
9595 raise HTTPException (
9696 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
9797 detail = "Database error occurred." ,
98- )
98+ ) from e
9999 except Exception as e :
100- logger .error ( f "Internal server error: { e } " )
100+ logger .exception ( "Internal server error" , exc_info = e )
101101 raise HTTPException (
102102 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
103103 detail = "Internal server error." ,
104- )
104+ ) from e
105105
106106
107107@blog .get (
@@ -113,8 +113,7 @@ async def read_blog(
113113 id : int ,
114114 db : Session = Depends (get_db ),
115115) -> BlogResponse :
116- """
117- Retrieve a blog post by ID.
116+ """Retrieve a blog post by ID.
118117
119118 Args:
120119 id (int): The ID of the blog post to retrieve.
@@ -124,28 +123,29 @@ async def read_blog(
124123 BlogResponse: The blog post response.
125124
126125 Raises:
127- HTTPException: If the blog post is not found or a database error occurs.
126+ HTTPException: If the blog post is not found
127+ or a database error occurs.
128128 """
129129 try :
130130 return BlogService .read_blog (db , id )
131131 except ValueError as e :
132- logger .warning (str (e ))
132+ logger .warning ("Value error occurred: %s" , str (e ))
133133 raise HTTPException (
134134 status_code = status .HTTP_404_NOT_FOUND ,
135135 detail = str (e ),
136- )
136+ ) from e
137137 except SQLAlchemyError as e :
138- logger .error ( f "Database error occurred: { e } " )
138+ logger .exception ( "Database error occurred" , exc_info = e )
139139 raise HTTPException (
140140 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
141141 detail = "Database error occurred." ,
142- )
142+ ) from e
143143 except Exception as e :
144- logger .error ( f "Internal server error: { e } " )
144+ logger .exception ( "Internal server error" , exc_info = e )
145145 raise HTTPException (
146146 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
147147 detail = "Internal server error." ,
148- )
148+ ) from e
149149
150150
151151@blog .patch (
@@ -158,8 +158,7 @@ async def update_blog(
158158 blog_update : BlogUpdate ,
159159 db : Session = Depends (get_db ),
160160) -> BlogResponse :
161- """
162- Update an existing blog post by ID.
161+ """Update an existing blog post by ID.
163162
164163 Args:
165164 id (int): The ID of the blog post to update.
@@ -176,31 +175,31 @@ async def update_blog(
176175 try :
177176 return BlogService .update_blog (db , id , blog_update )
178177 except RequestValidationError as e :
179- logger .warning (f "Validation error: { e } " )
178+ logger .warning ("Validation error: %s" , str ( e ) )
180179 raise HTTPException (
181180 status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
182181 detail = e .errors (),
183- )
182+ ) from e
184183 except ValueError as e :
185- logger .warning (str (e ))
184+ logger .warning ("Value error occurred: %s" , str (e ))
186185 raise HTTPException (
187186 status_code = status .HTTP_404_NOT_FOUND
188187 if "not found" in str (e ).lower ()
189188 else status .HTTP_409_CONFLICT ,
190189 detail = str (e ),
191- )
190+ ) from e
192191 except SQLAlchemyError as e :
193- logger .error ( f "Database error occurred: { e } " )
192+ logger .exception ( "Database error occurred" , exc_info = e )
194193 raise HTTPException (
195194 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
196195 detail = "Database error occurred." ,
197- )
196+ ) from e
198197 except Exception as e :
199- logger .error ( f "Internal server error: { e } " )
198+ logger .exception ( "Internal server error" , exc_info = e )
200199 raise HTTPException (
201200 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
202201 detail = "Internal server error." ,
203- )
202+ ) from e
204203
205204
206205@blog .delete (
@@ -211,33 +210,33 @@ async def delete_blog(
211210 id : int ,
212211 db : Session = Depends (get_db ),
213212) -> None :
214- """
215- Delete a blog post by ID (soft delete).
213+ """Delete a blog post by ID (soft delete).
216214
217215 Args:
218216 id (int): The ID of the blog post to delete.
219217 db (Session): The database session dependency.
220218
221219 Raises:
222- HTTPException: If the blog post is not found or a database error occurs.
220+ HTTPException: If the blog post is not found
221+ or a database error occurs.
223222 """
224223 try :
225224 BlogService .delete_blog (db , id )
226225 except ValueError as e :
227- logger .warning (str (e ))
226+ logger .warning ("Value error occurred: %s" , str (e ))
228227 raise HTTPException (
229228 status_code = status .HTTP_404_NOT_FOUND ,
230229 detail = str (e ),
231- )
230+ ) from e
232231 except SQLAlchemyError as e :
233- logger .error ( f "Database error occurred: { e } " )
232+ logger .exception ( "Database error occurred" , exc_info = e )
234233 raise HTTPException (
235234 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
236235 detail = "Database error occurred." ,
237- )
236+ ) from e
238237 except Exception as e :
239- logger .error ( f "Internal server error: { e } " )
238+ logger .exception ( "Internal server error" , exc_info = e )
240239 raise HTTPException (
241240 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
242241 detail = "Internal server error." ,
243- )
242+ ) from e
0 commit comments