From db4e1c01b0ef7938efb6ae91f3200c606e67d239 Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Tue, 25 Nov 2025 16:14:16 +0300 Subject: [PATCH] Update logic on handlers and upd schema --- backend/app/api/v1/posts.py | 23 +++++++++++++---------- backend/app/schemas/posts.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/backend/app/api/v1/posts.py b/backend/app/api/v1/posts.py index c1921e2..81782c3 100644 --- a/backend/app/api/v1/posts.py +++ b/backend/app/api/v1/posts.py @@ -48,17 +48,12 @@ async def create_post( ) -> Post: try: post = PostCreate(title=title, content=content, images=images) - except Exception as e: - raise HTTPException(status_code=423, detail=str("Validation error")) - + except ValueError as e: + raise HTTPException(status_code=422, detail=str(e)) new_post = Post(title=post.title, content=post.content, images=post.images) - try: - db.add(new_post) - await db.commit() - await db.refresh(new_post) - except Exception as e: - raise HTTPException(status_code=400, detail=str(e)) - + db.add(new_post) + await db.commit() + await db.refresh(new_post) return new_post @@ -76,6 +71,14 @@ async def update_post( if post is None: raise HTTPException(status_code=404, detail="Post not found") + + if title is not None: + post.title = title # type: ignore + if content is not None: + post.content = content # type: ignore + if images is not None: + post.images = images # type: ignore + await db.commit() return post diff --git a/backend/app/schemas/posts.py b/backend/app/schemas/posts.py index ed80e9c..3cc0ee5 100644 --- a/backend/app/schemas/posts.py +++ b/backend/app/schemas/posts.py @@ -5,7 +5,7 @@ from typing import Optional class PostCreate(BaseModel): title: str = Field(min_length=3, max_length=150) content: str = Field(min_length=10) - images: Optional[str] = Field(default=None) + images: Optional[str] = Field(min_length=0) class Config: from_attributes = True