-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69e1bf0
commit df31f2f
Showing
11 changed files
with
325 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/amdb/application/command_handlers/delete_from_watchlist.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from amdb.application.common.gateways.movie_for_later import ( | ||
MovieForLaterGateway, | ||
) | ||
from amdb.application.common.unit_of_work import UnitOfWork | ||
from amdb.application.common.identity_provider import IdentityProvider | ||
from amdb.application.common.constants.exceptions import ( | ||
USER_IS_NOT_OWNER, | ||
MOVIE_NOT_IN_WATCHLIST, | ||
) | ||
from amdb.application.common.exception import ApplicationError | ||
from amdb.application.commands.delete_from_watchlist import ( | ||
DeleteFromWatchlistCommand, | ||
) | ||
|
||
|
||
class DeleteFromWatchlistHandler: | ||
def __init__( | ||
self, | ||
*, | ||
movie_for_later_gateway: MovieForLaterGateway, | ||
unit_of_work: UnitOfWork, | ||
identity_provider: IdentityProvider, | ||
) -> None: | ||
self._movie_for_later_gateway = movie_for_later_gateway | ||
self._unit_of_work = unit_of_work | ||
self._identity_provider = identity_provider | ||
|
||
def execute(self, command: DeleteFromWatchlistCommand) -> None: | ||
current_user_id = self._identity_provider.user_id() | ||
|
||
movie_for_later = self._movie_for_later_gateway.with_id( | ||
command.movie_for_later_id, | ||
) | ||
if not movie_for_later: | ||
raise ApplicationError(MOVIE_NOT_IN_WATCHLIST) | ||
|
||
if movie_for_later.user_id != current_user_id: | ||
raise ApplicationError(USER_IS_NOT_OWNER) | ||
|
||
self._movie_for_later_gateway.delete(movie_for_later) | ||
|
||
self._unit_of_work.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dataclasses import dataclass | ||
|
||
from amdb.domain.entities.movie_for_later import MovieForLaterId | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class DeleteFromWatchlistCommand: | ||
movie_for_later_id: MovieForLaterId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import Annotated, Optional | ||
|
||
from fastapi import Cookie | ||
from dishka.integrations.fastapi import FromDishka, inject | ||
|
||
from amdb.domain.entities.movie_for_later import MovieForLaterId | ||
from amdb.application.commands.delete_from_watchlist import ( | ||
DeleteFromWatchlistCommand, | ||
) | ||
from amdb.application.command_handlers.delete_from_watchlist import ( | ||
DeleteFromWatchlistHandler, | ||
) | ||
from amdb.application.common.gateways.permissions import PermissionsGateway | ||
from amdb.infrastructure.auth.session.session import SessionId | ||
from amdb.infrastructure.auth.session.session_gateway import SessionGateway | ||
from amdb.infrastructure.auth.session.identity_provider import ( | ||
SessionIdentityProvider, | ||
) | ||
from amdb.presentation.create_handler import CreateHandler | ||
from amdb.presentation.web_api.constants import SESSION_ID_COOKIE | ||
|
||
|
||
HandlerMaker = CreateHandler[DeleteFromWatchlistHandler] | ||
|
||
|
||
@inject | ||
async def delete_movie_from_watchlist( | ||
*, | ||
create_handler: Annotated[HandlerMaker, FromDishka()], | ||
session_gateway: Annotated[SessionGateway, FromDishka()], | ||
permissions_gateway: Annotated[PermissionsGateway, FromDishka()], | ||
session_id: Annotated[ | ||
Optional[str], | ||
Cookie(alias=SESSION_ID_COOKIE), | ||
] = None, | ||
movie_for_later_id: MovieForLaterId, | ||
) -> None: | ||
""" | ||
Deletes movie from watchlist. \n\n | ||
### Returns 400: | ||
* When movie not in watchlist | ||
* When user is not a watchlist owner | ||
""" | ||
identity_provider = SessionIdentityProvider( | ||
session_id=SessionId(session_id) if session_id else None, | ||
session_gateway=session_gateway, | ||
permissions_gateway=permissions_gateway, | ||
) | ||
|
||
handler = create_handler(identity_provider) | ||
command = DeleteFromWatchlistCommand( | ||
movie_for_later_id=movie_for_later_id, | ||
) | ||
|
||
handler.execute(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
from fastapi import APIRouter | ||
|
||
from .add_movie import add_movie_to_watchlist | ||
from .delete_movie import delete_movie_from_watchlist | ||
|
||
|
||
watchlists_router = APIRouter(tags=["watchlists"]) | ||
watchlists_router.add_api_route( | ||
path="/my/watchlist/movies", | ||
path="/my/movies-for-later", | ||
endpoint=add_movie_to_watchlist, | ||
methods=["POST"], | ||
) | ||
watchlists_router.add_api_route( | ||
path="/my/movies-for-later/{movie_for_later_id}", | ||
endpoint=delete_movie_from_watchlist, | ||
methods=["DELETE"], | ||
) |
Oops, something went wrong.