-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
AuthorRouter.py
76 lines (61 loc) · 1.73 KB
/
AuthorRouter.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from typing import List, Optional
from fastapi import APIRouter, Depends, status
from schemas.pydantic.AuthorSchema import (
AuthorPostRequestSchema,
AuthorSchema,
)
from schemas.pydantic.BookSchema import BookSchema
from services.AuthorService import AuthorService
AuthorRouter = APIRouter(
prefix="/v1/authors", tags=["author"]
)
@AuthorRouter.get("/", response_model=List[AuthorSchema])
def index(
name: Optional[str] = None,
pageSize: Optional[int] = 100,
startIndex: Optional[int] = 0,
authorService: AuthorService = Depends(),
):
return [
author.normalize()
for author in authorService.list(
name, pageSize, startIndex
)
]
@AuthorRouter.get("/{id}", response_model=AuthorSchema)
def get(id: int, authorService: AuthorService = Depends()):
return authorService.get(id).normalize()
@AuthorRouter.post(
"/",
response_model=AuthorSchema,
status_code=status.HTTP_201_CREATED,
)
def create(
author: AuthorPostRequestSchema,
authorService: AuthorService = Depends(),
):
return authorService.create(author).normalize()
@AuthorRouter.patch("/{id}", response_model=AuthorSchema)
def update(
id: int,
author: AuthorPostRequestSchema,
authorService: AuthorService = Depends(),
):
return authorService.update(id, author).normalize()
@AuthorRouter.delete(
"/{id}", status_code=status.HTTP_204_NO_CONTENT
)
def delete(
id: int, authorService: AuthorService = Depends()
):
return authorService.delete(id)
@AuthorRouter.get(
"/{id}/books/", response_model=List[BookSchema]
)
def get_books(
id: int, authorService: AuthorService = Depends()
):
return [
book.normalize()
for book in authorService.get_books(id)
]