-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathposts.py
More file actions
41 lines (28 loc) · 1.33 KB
/
posts.py
File metadata and controls
41 lines (28 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import APIRouter, Depends
from services import posts as posts_service
import schemas, models
from sqlalchemy.orm import Session
router = APIRouter(prefix="/posts")
@router.post("/", tags=["posts"])
async def create_post(post: schemas.Post, db: Session = Depends(models.get_db)):
return posts_service.create_post(post=post, db=db)
@router.get("/{post_id}", tags=["posts"])
async def get_post_by_id(post_id: str, db: Session = Depends(models.get_db)):
return posts_service.get_post_by_id(post_id=post_id, db=db)
@router.get("/", tags=["posts"])
async def get_posts_by_title(title: str = None, db: Session = Depends(models.get_db)):
if title:
return posts_service.get_all_posts(db=db)
else:
return posts_service.get_posts_by_title(title=title, db=db)
@router.put("/{post_id}", tags=["posts"])
async def update_post_by_id(
post_id: str, post: schemas.Post, db: Session = Depends(models.get_db)
):
return posts_service.update_post(post_id=post_id, db=db, post=post)
@router.delete("/{post_id}", tags=["posts"])
async def delete_post_by_id(post_id: str, db: Session = Depends(models.get_db)):
return posts_service.delete_post(post_id=post_id, db=db)
@router.delete("/", tags=["posts"])
async def delete_all_posts(db: Session = Depends(models.get_db)):
return posts_service.delete_all_posts(db=db)