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