Add new handler
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Body
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
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
|
||||
@@ -14,19 +14,33 @@ router = APIRouter(
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def get_posts():
|
||||
pass
|
||||
|
||||
|
||||
@router.get("/{id}")
|
||||
async def get_post(id: int):
|
||||
pass
|
||||
|
||||
|
||||
@router.post("/")
|
||||
async def create_post(
|
||||
post: PostCreate = Body(...), db: AsyncSession = Depends(get_async_session)
|
||||
async def get_posts(
|
||||
page: int = Query(1, ge=1), db: AsyncSession = Depends(get_async_session)
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=PostRead)
|
||||
async def get_post(id: int, db: AsyncSession = Depends(get_async_session)):
|
||||
post = await db.get(Post, id)
|
||||
if post is None:
|
||||
raise HTTPException(status_code=404, detail="Post not found")
|
||||
else:
|
||||
return post
|
||||
|
||||
|
||||
@router.post("/", response_model=PostRead)
|
||||
async def create_post(
|
||||
title: str,
|
||||
content: str,
|
||||
images: Optional[str] = None,
|
||||
db: AsyncSession = Depends(get_async_session),
|
||||
) -> Post:
|
||||
try:
|
||||
post = PostCreate(title=title, content=content, images=images)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=423, detail=str("Validation error"))
|
||||
|
||||
new_post = Post(title=post.title, content=post.content, images=post.images)
|
||||
try:
|
||||
db.add(new_post)
|
||||
|
||||
Reference in New Issue
Block a user