Update schema and written handlers for post http
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -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(
|
router = APIRouter(
|
||||||
prefix="/posts",
|
prefix="/posts",
|
||||||
@@ -6,6 +12,7 @@ router = APIRouter(
|
|||||||
responses={404: {"description": "Not found"}},
|
responses={404: {"description": "Not found"}},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_posts():
|
async def get_posts():
|
||||||
pass
|
pass
|
||||||
@@ -17,8 +24,18 @@ async def get_post(id: int):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/")
|
@router.post("/")
|
||||||
async def create_post():
|
async def create_post(
|
||||||
pass
|
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}")
|
@router.put("/{id}")
|
||||||
@@ -29,5 +46,3 @@ async def update_post(id: int):
|
|||||||
@router.delete("/{id}")
|
@router.delete("/{id}")
|
||||||
async def delete_post(id: int):
|
async def delete_post(id: int):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ from typing import Optional
|
|||||||
class PostCreate(BaseModel):
|
class PostCreate(BaseModel):
|
||||||
title: str = Field(min_length=3, max_length=150)
|
title: str = Field(min_length=3, max_length=150)
|
||||||
content: str = Field(min_length=10)
|
content: str = Field(min_length=10)
|
||||||
images: Optional[str] = Field()
|
images: Optional[str] = Field(default=None)
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
class PostRead(BaseModel):
|
class PostRead(BaseModel):
|
||||||
@@ -18,13 +18,13 @@ class PostRead(BaseModel):
|
|||||||
images: str = Field()
|
images: str = Field()
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
class PostUpdate(BaseModel):
|
class PostUpdate(BaseModel):
|
||||||
title: str = Field(min_length=3, max_length=150)
|
title: str = Field(min_length=3, max_length=150)
|
||||||
content: str = Field(min_length=10)
|
content: str = Field(min_length=10)
|
||||||
images: Optional[str] = Field()
|
images: Optional[str] = Field(default=None)
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
from_attributes = True
|
||||||
|
|||||||
Reference in New Issue
Block a user