File size: 1,554 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, List

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from utils import deps
from cruds import crud_${snake_case_name}
from schemas import ${PascalCaseName}, ${PascalCaseName}Update, ${PascalCaseName}Create

router = APIRouter()


@router.get("/${snake_case_name}", response_model=List[${PascalCaseName}])
async def get_${snake_case_name}(
    db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100
) -> Any:
    ${snake_case_name} = crud_${snake_case_name}.get_multi(db, skip=skip, limit=limit)
    return ${snake_case_name}


@router.post("/${snake_case_name}", response_model=${PascalCaseName})
async def create_${snake_case_name}(
    db: Session = Depends(deps.get_db), *, obj_in: ${PascalCaseName}Create
) -> Any:
    ${snake_case_name} = crud_${snake_case_name}.create(db, obj_in=obj_in)
    return ${snake_case_name}


@router.get("/${snake_case_name}/{id}", response_model=${PascalCaseName})
async def get_specific_${snake_case_name}(db: Session = Depends(deps.get_db), *, id: int) -> Any:
    ${snake_case_name} = crud_${snake_case_name}.get(db, id)
    return ${snake_case_name}


@router.put("/${snake_case_name}/{id}", response_model=${PascalCaseName})
async def update_${snake_case_name}(
    db: Session = Depends(deps.get_db), *, id: int, obj_in: ${PascalCaseName}Update
) -> Any:
    ${snake_case_name} = crud_${snake_case_name}.get(db, id)
    ${snake_case_name} = crud_${snake_case_name}.update(db, db_obj=${snake_case_name}, obj_in=obj_in)
    return ${snake_case_name}