Coverage for app/ddd/presentation/endpoint/user/get_user.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
1from typing import Any
3from fastapi import Depends, Path, status
5# def __t_usecase(session: Session = Depends(get_session)) -> GetUsersUseCase:
6# user_repository = UserRepository(session)
7# return GetUsersUseCase(user_repository)
8# @router.get(
9# path="/users",
10# # response_model=GetUsersResponse,
11# )
12# def get_users(
13# usecase: GetUsersUseCase = Depends(__t_usecase),
14# ):
15# """Userを一覧する."""
16# dto : GetUserOutputDTO = usecase.execute()
17# return dto
18from sqlmodel import Session
20from app.core.depends.jwt_data_depends import jwt_data_depends
21from app.ddd.application.dto.user import GetUserInputDTO, GetUserOutputDTO
22from app.ddd.application.usecase.user import (
23 # GetUsersUseCase,
24 GetUserUseCase,
25)
26from app.ddd.domain import UserNotFoundError
27from app.ddd.infrastructure.database.db import get_session
28from app.ddd.infrastructure.uow import UserUnitOfWorkImpl
29from app.ddd.presentation.endpoint.user.router import router
30from app.ddd.presentation.schema.user import GetUserResponse
33# #######################
34def __usecase(session: Session = Depends(get_session)) -> GetUserUseCase:
35 return GetUserUseCase(uow=UserUnitOfWorkImpl(session))
37################ me
38@router.get(
39 path="/users/me",
40 response_model=GetUserResponse,
41)
42def get_me(
43 usecase: GetUserUseCase = Depends(__usecase),
44 jwt_data: dict[str, Any] = Depends(jwt_data_depends),
45) -> GetUserResponse:
46 """自身を取得する."""
47 input_dto: GetUserInputDTO = GetUserInputDTO(user_id=jwt_data["sub"])
48 # input_dto: GetUserInputDTO = GetUserInputDTO(user_id="guest")
49 dto: GetUserOutputDTO = usecase.execute(input_dto)
50 return GetUserResponse.model_validate(dto)
52################ id
53@router.get(
54 path="/users/{userId}",
55 response_model=GetUserResponse,
56 responses={
57 status.HTTP_404_NOT_FOUND: UserNotFoundError(user_id="dammy").response(),
58 },
59)
60def get_user(
61 user_id: str = Path(..., alias="userId"),
62 usecase: GetUserUseCase = Depends(__usecase),
63) -> GetUserResponse:
64 """Userを取得する."""
65 input_dto: GetUserInputDTO = GetUserInputDTO(user_id=user_id)
66 dto: GetUserOutputDTO = usecase.execute(input_dto)
67 return GetUserResponse.model_validate(dto)