Update logic on handlers and upd schema

This commit is contained in:
d3m0k1d
2025-11-25 16:14:16 +03:00
parent 432e8e9c95
commit db4e1c01b0
2 changed files with 14 additions and 11 deletions

View File

@@ -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))
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

View File

@@ -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