2021-02-05 02:18:25 +03:00
|
|
|
"""Endpoints module."""
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Response, status
|
|
|
|
from dependency_injector.wiring import inject, Provide
|
|
|
|
|
|
|
|
from .containers import Container
|
|
|
|
from .services import UserService
|
|
|
|
from .repositories import NotFoundError
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@router.get("/users")
|
2021-02-05 02:18:25 +03:00
|
|
|
@inject
|
|
|
|
def get_list(
|
|
|
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
|
|
|
):
|
|
|
|
return user_service.get_users()
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@router.get("/users/{user_id}")
|
2021-02-05 02:18:25 +03:00
|
|
|
@inject
|
|
|
|
def get_by_id(
|
|
|
|
user_id: int,
|
|
|
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
return user_service.get_user_by_id(user_id)
|
|
|
|
except NotFoundError:
|
|
|
|
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@router.post("/users", status_code=status.HTTP_201_CREATED)
|
2021-02-05 02:18:25 +03:00
|
|
|
@inject
|
|
|
|
def add(
|
|
|
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
|
|
|
):
|
|
|
|
return user_service.create_user()
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@router.delete("/users/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
|
2021-02-05 02:18:25 +03:00
|
|
|
@inject
|
|
|
|
def remove(
|
|
|
|
user_id: int,
|
|
|
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
user_service.delete_user_by_id(user_id)
|
|
|
|
except NotFoundError:
|
|
|
|
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
else:
|
|
|
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
|
|
|
2021-10-01 02:05:54 +03:00
|
|
|
@router.get("/status")
|
2021-02-05 02:18:25 +03:00
|
|
|
def get_status():
|
2021-10-01 02:05:54 +03:00
|
|
|
return {"status": "OK"}
|