Update logic on handlers and upd schema
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user