-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.py
26 lines (21 loc) · 917 Bytes
/
post.py
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
from fastapi import APIRouter, Depends, HTTPException
from ..services import PostService, UserService
from ..models import Post
from .authentication import registered_user
api = APIRouter(prefix="/api/post")
@api.post("", response_model=Post, tags=['Post'])
def create(post: Post, post_svc: PostService = Depends(), usr_svc: UserService = Depends()):
try:
user_entity = usr_svc.findUser(post.user)
except:
raise HTTPException(status_code=422, detail=str("User Not Registered"))
return post_svc.create(post,user_entity)
@api.get("", response_model=list[Post], tags=['Post'])
def getAll(post_svc: PostService = Depends()):
return post_svc.getAll()
@api.delete("/{id}", tags=['Post'])
def delete(id: int, post_svc: PostService = Depends()) -> bool:
try:
return post_svc.delete(id=id)
except:
raise HTTPException(status_code=422, detail=str("Post not found"))