|
| 1 | +from typing import Annotated |
| 2 | + |
| 3 | +from fastapi import APIRouter, Depends, status |
| 4 | +from fastapi_pagination import Page |
| 5 | + |
| 6 | +from futuramaapi.routers.exceptions import NotFoundResponse |
| 7 | +from futuramaapi.routers.rest.users.dependencies import _oauth2_scheme |
| 8 | +from futuramaapi.routers.services.favorites.create_favorite_character import CreateFavoriteCharacterService |
| 9 | +from futuramaapi.routers.services.favorites.delete_favorite_character import DeleteFavoriteCharacterService |
| 10 | +from futuramaapi.routers.services.favorites.list_favorite_characters import ( |
| 11 | + ListFavoriteCharacters, |
| 12 | + ListFavoriteCharactersResponse, |
| 13 | +) |
| 14 | + |
| 15 | +router = APIRouter( |
| 16 | + prefix="/favorites/characters", |
| 17 | + tags=[ |
| 18 | + "Favorite Characters", |
| 19 | + ], |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@router.post( |
| 24 | + "/{character_id}", |
| 25 | + status_code=status.HTTP_204_NO_CONTENT, |
| 26 | + responses={ |
| 27 | + status.HTTP_404_NOT_FOUND: { |
| 28 | + "model": NotFoundResponse, |
| 29 | + }, |
| 30 | + status.HTTP_409_CONFLICT: { |
| 31 | + "example": "Character is already in favorites", |
| 32 | + }, |
| 33 | + }, |
| 34 | + name="create_favorite_character", |
| 35 | +) |
| 36 | +async def create_favorite_character( |
| 37 | + character_id: int, |
| 38 | + token: Annotated[str, Depends(_oauth2_scheme)], |
| 39 | +) -> None: |
| 40 | + """ |
| 41 | + Add character to favorites. |
| 42 | +
|
| 43 | + Adds the specified character to the authenticated user's favorites list. |
| 44 | +
|
| 45 | + If the character is already present in favorites, the request will fail |
| 46 | + with a conflict error. |
| 47 | +
|
| 48 | + This endpoint requires authentication. |
| 49 | + """ |
| 50 | + service: CreateFavoriteCharacterService = CreateFavoriteCharacterService( |
| 51 | + token=token, |
| 52 | + character_id=character_id, |
| 53 | + ) |
| 54 | + await service() |
| 55 | + |
| 56 | + |
| 57 | +@router.get( |
| 58 | + "", |
| 59 | + status_code=status.HTTP_200_OK, |
| 60 | + response_model=Page[ListFavoriteCharactersResponse], |
| 61 | + name="favorite_characters", |
| 62 | +) |
| 63 | +async def get_favorite_characters( |
| 64 | + token: Annotated[str, Depends(_oauth2_scheme)], |
| 65 | +) -> Page[ListFavoriteCharactersResponse]: |
| 66 | + """ |
| 67 | + Retrieve favorite characters. |
| 68 | +
|
| 69 | + Returns a paginated list of characters added to the current user's favorites. |
| 70 | +
|
| 71 | + You can use standard pagination parameters to control the size and order of the response. |
| 72 | + This endpoint requires authentication and returns only the favorites of the authorized user. |
| 73 | +
|
| 74 | + Check the query parameters section for available pagination options. |
| 75 | + """ |
| 76 | + service: ListFavoriteCharacters = ListFavoriteCharacters(token=token) |
| 77 | + return await service() |
| 78 | + |
| 79 | + |
| 80 | +@router.delete( |
| 81 | + "/{character_id}", |
| 82 | + status_code=status.HTTP_204_NO_CONTENT, |
| 83 | + responses={ |
| 84 | + status.HTTP_404_NOT_FOUND: { |
| 85 | + "model": NotFoundResponse, |
| 86 | + }, |
| 87 | + status.HTTP_409_CONFLICT: { |
| 88 | + "example": "Character is already in favorites", |
| 89 | + }, |
| 90 | + }, |
| 91 | + name="create_favorite_character", |
| 92 | +) |
| 93 | +async def delete_favorite_character( |
| 94 | + character_id: int, |
| 95 | + token: Annotated[str, Depends(_oauth2_scheme)], |
| 96 | +) -> None: |
| 97 | + """ |
| 98 | + Remove character from favorites. |
| 99 | +
|
| 100 | + Removes the specified character from the authenticated user's favorites list. |
| 101 | +
|
| 102 | + If the character does not exist or is not present in the user's favorites, |
| 103 | + the request will fail with a not found error. |
| 104 | +
|
| 105 | + This endpoint requires authentication and performs a destructive operation |
| 106 | + without returning a response body. |
| 107 | + """ |
| 108 | + service: DeleteFavoriteCharacterService = DeleteFavoriteCharacterService( |
| 109 | + token=token, |
| 110 | + character_id=character_id, |
| 111 | + ) |
| 112 | + await service() |
0 commit comments