From c8ccce931a6091d40af2d8a4906945de6f9fe9b4 Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Sun, 23 Nov 2025 21:51:06 +0300 Subject: [PATCH] Update schema and written handlers for post http --- .../v1/__pycache__/__init__.cpython-313.pyc | Bin 157 -> 0 bytes .../api/v1/__pycache__/posts.cpython-313.pyc | Bin 1306 -> 0 bytes backend/app/api/v1/posts.py | 25 ++++++++++++++---- backend/app/schemas/posts.py | 10 +++---- 4 files changed, 25 insertions(+), 10 deletions(-) delete mode 100644 backend/app/api/v1/__pycache__/__init__.cpython-313.pyc delete mode 100644 backend/app/api/v1/__pycache__/posts.cpython-313.pyc diff --git a/backend/app/api/v1/__pycache__/__init__.cpython-313.pyc b/backend/app/api/v1/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 00ba8a6df27fe6cd365c815485cbb2d8c5acea44..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 157 zcmey&%ge<81V0WcWP<3&AOZ#$p^VQgK*m&tbOudEzm*I{OhDdekkl<3{fzwFRQ(j= zT!U=G6#b&qf_xZ9uc%Z%DKR-aH7`X!v7i8mGxf_1_2c6+^D;}~gxA3H0TD#-3VMJUE~9mP6%mD~7A6`=5WR+Lm?W1~rmQP#5ia6^lNd|D zAr>)xM}=Jo>0wsgyL{lMo5c{uN(7d&lKzsVdgaNO>ex#|Yx zmfP#vRh8==qb=tp*Mdgd=O&}R=XQPSb8I!^B`IL+zz#DYDyR%IYNMNK3-968J;cOq z2pjQb>#5tJmVLcb+o;zloX*4%z*R~p-2ivip*H6k_^j>kZO@qg>Y9?B4 z3QhyW5!y!IHRE@vf$>(+@K(Zl?bvU);@Hj6sKJBKxka)xcx91$m=!?x1+2^_Kyo8W z%Vxk7S8P&&$Gw;^WdEO z&-^ysq*46~z+F1Uzgo@mWUJu^FiNI^i|b7OLastnDvB8 z{Wz(mlx=;OL5lIN*e?$5?p!%mFg8OKDfyw$hDPMOcYvqm)vh0+QOry%W}&|@AO|=< gCMd+5f_zNyvGPxXQol5Kae!wfEEY2-8RcL31Gew-)c^nh diff --git a/backend/app/api/v1/posts.py b/backend/app/api/v1/posts.py index cdc6b30..f042797 100644 --- a/backend/app/api/v1/posts.py +++ b/backend/app/api/v1/posts.py @@ -1,4 +1,10 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends, HTTPException, Body +from sqlalchemy.ext.asyncio import AsyncSession +from pydantic import BaseModel +from app.schemas.posts import PostCreate, PostRead, PostRead +from app.db_engine import get_async_session +from app.models.posts import Post + router = APIRouter( prefix="/posts", @@ -6,6 +12,7 @@ router = APIRouter( responses={404: {"description": "Not found"}}, ) + @router.get("/") async def get_posts(): pass @@ -17,8 +24,18 @@ async def get_post(id: int): @router.post("/") -async def create_post(): - pass +async def create_post( + post: PostCreate = Body(...), db: AsyncSession = Depends(get_async_session) +): + 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 @router.put("/{id}") @@ -29,5 +46,3 @@ async def update_post(id: int): @router.delete("/{id}") async def delete_post(id: int): pass - - diff --git a/backend/app/schemas/posts.py b/backend/app/schemas/posts.py index 154db42..ed80e9c 100644 --- a/backend/app/schemas/posts.py +++ b/backend/app/schemas/posts.py @@ -5,10 +5,10 @@ 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() + images: Optional[str] = Field(default=None) class Config: - orm_mode = True + from_attributes = True class PostRead(BaseModel): @@ -18,13 +18,13 @@ class PostRead(BaseModel): images: str = Field() class Config: - orm_mode = True + from_attributes = True class PostUpdate(BaseModel): title: str = Field(min_length=3, max_length=150) content: str = Field(min_length=10) - images: Optional[str] = Field() + images: Optional[str] = Field(default=None) class Config: - orm_mode = True + from_attributes = True