-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathposts.py
More file actions
50 lines (37 loc) · 1.5 KB
/
posts.py
File metadata and controls
50 lines (37 loc) · 1.5 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
42
43
44
45
46
47
48
49
50
from typing import List
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from database.connection import get_db
from schemas.models import DeletePostResponse, Post, UpdatePost
from utils.post_crud import (
post_create,
post_delete,
post_get_one,
post_update,
posts_get_all,
)
router = APIRouter(tags=["posts"])
@router.post("/create", status_code=status.HTTP_201_CREATED, response_model=Post)
def create_post(post: Post, db: Session = Depends(get_db)):
return post_create(db=db, post=post)
@router.get("/list/all", status_code=status.HTTP_200_OK, response_model=List[Post])
def get_all_posts(db: Session = Depends(get_db)):
return posts_get_all(db=db)
@router.get("/get/{id}", status_code=status.HTTP_200_OK, response_model=Post)
def get_one_post(id: UUID, db: Session = Depends(get_db)):
return post_get_one(db=db, id=id)
@router.delete(
"/delete/{id}", status_code=status.HTTP_200_OK, response_model=DeletePostResponse
)
def delete_post(id: UUID, db: Session = Depends(get_db)):
delete_status = post_delete(db=db, id=id)
if delete_status.detail == "Doesnt Exist":
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Post Not Found"
)
else:
return delete_status
@router.patch("/update", status_code=status.HTTP_200_OK, response_model=Post)
def update_post(post: UpdatePost, db: Session = Depends(get_db)):
return post_update(db=db, post=post)