-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create mutation for manga branch
- Loading branch information
1 parent
14b7a71
commit 76909dd
Showing
24 changed files
with
416 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import strawberry | ||
|
||
from app.adapters.graphql.types import LanguageGQL | ||
from app.core.domain.branches.dto import MangaBranchCreateDTO | ||
|
||
|
||
@strawberry.input(name="MangaBranchCreateInput") | ||
class MangaBranchCreateInput: | ||
name: str | ||
language: LanguageGQL | ||
manga_id: strawberry.ID | ||
|
||
def to_dto(self) -> MangaBranchCreateDTO: | ||
return MangaBranchCreateDTO( | ||
name=self.name, | ||
language=self.language, | ||
manga_id=self.manga_id, # type: ignore[arg-type] | ||
) |
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,7 +1,52 @@ | ||
from typing import Annotated | ||
|
||
import strawberry | ||
from aioinject import Inject | ||
from aioinject.ext.strawberry import inject | ||
from result import Err | ||
|
||
from app.adapters.graphql.apps.branches.input import MangaBranchCreateInput | ||
from app.adapters.graphql.apps.branches.payload import ( | ||
MangaBranchCreatePayloadGQL, | ||
) | ||
from app.adapters.graphql.apps.branches.types import MangaBranchGQL | ||
from app.adapters.graphql.context import Info | ||
from app.adapters.graphql.errors import RelationshipNotFoundErrorGQL | ||
from app.adapters.graphql.extensions import AuthExtension | ||
from app.adapters.graphql.validation import validate_callable | ||
from app.core.domain.branches.commands import MangaBranchCreateCommand | ||
from app.core.errors import RelationshipNotFoundError | ||
|
||
|
||
@strawberry.type | ||
class MangaBranchMutation: | ||
async def create(self) -> None: | ||
pass | ||
class MangaBranchMutationGQL: | ||
@strawberry.mutation(extensions=[AuthExtension]) # type: ignore[misc] | ||
@inject | ||
async def create( | ||
self, | ||
input: MangaBranchCreateInput, | ||
command: Annotated[MangaBranchCreateCommand, Inject], | ||
info: Info, | ||
) -> MangaBranchCreatePayloadGQL: | ||
dto = validate_callable(input.to_dto) | ||
if isinstance(dto, Err): | ||
return MangaBranchCreatePayloadGQL(error=dto.err_value) | ||
|
||
result = await command.execute( | ||
dto=dto.ok_value, | ||
user=await info.context.user, | ||
) | ||
|
||
if isinstance(result, Err): | ||
match result.err_value: | ||
case RelationshipNotFoundError(): # pragma: no branch | ||
return MangaBranchCreatePayloadGQL( | ||
error=RelationshipNotFoundErrorGQL.from_err( | ||
result.err_value, | ||
), | ||
) | ||
|
||
return MangaBranchCreatePayloadGQL( | ||
branch=MangaBranchGQL.from_dto(result.ok_value), | ||
error=None, | ||
) |
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,18 @@ | ||
from typing import Annotated | ||
|
||
import strawberry | ||
|
||
from app.adapters.graphql.apps.branches.types import MangaBranchGQL | ||
from app.adapters.graphql.errors import RelationshipNotFoundErrorGQL | ||
from app.adapters.graphql.validation import ValidationErrorsGQL | ||
|
||
MangaBranchCreateError = Annotated[ | ||
ValidationErrorsGQL | RelationshipNotFoundErrorGQL, | ||
strawberry.union(name="MangaBranchCreateError"), | ||
] | ||
|
||
|
||
@strawberry.type(name="MangaBranchCreatePayload") | ||
class MangaBranchCreatePayloadGQL: | ||
branch: MangaBranchGQL | None = None | ||
error: MangaBranchCreateError | None |
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,10 @@ | ||
from aioinject import Scoped | ||
|
||
from app.core.domain.branches.commands import MangaBranchCreateCommand | ||
from app.core.domain.branches.services import MangaBranchService | ||
from lib.types import Providers | ||
|
||
providers: Providers = [ | ||
Scoped(MangaBranchService), | ||
Scoped(MangaBranchCreateCommand), | ||
] |
Empty file.
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,33 @@ | ||
from result import Err, Ok, Result | ||
|
||
from app.core.domain.branches.dto import MangaBranchCreateDTO | ||
from app.core.domain.branches.services import MangaBranchService | ||
from app.core.domain.manga.repositories import MangaRepository | ||
from app.core.errors import RelationshipNotFoundError | ||
from app.db.models import MangaBranch, User | ||
|
||
|
||
class MangaBranchCreateCommand: | ||
def __init__( | ||
self, | ||
manga_repository: MangaRepository, | ||
service: MangaBranchService, | ||
) -> None: | ||
self._manga_repository = manga_repository | ||
self._service = service | ||
|
||
async def execute( | ||
self, | ||
dto: MangaBranchCreateDTO, | ||
user: User, # noqa: ARG002 | ||
) -> Result[MangaBranch, RelationshipNotFoundError]: | ||
manga = await self._manga_repository.get(id=dto.manga_id) | ||
if manga is None: | ||
return Err( | ||
RelationshipNotFoundError( | ||
entity_name="Manga", | ||
entity_id=str(dto.manga_id), | ||
), | ||
) | ||
|
||
return Ok(await self._service.create(dto=dto, manga=manga)) |
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,13 @@ | ||
from uuid import UUID | ||
|
||
from pydantic import Field | ||
|
||
from app.core.domain.const import GENERIC_NAME_LENGTH | ||
from lib.dto import BaseDTO | ||
from lib.types import Language | ||
|
||
|
||
class MangaBranchCreateDTO(BaseDTO): | ||
name: str = Field(max_length=GENERIC_NAME_LENGTH) | ||
manga_id: UUID | ||
language: Language |
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,22 @@ | ||
from app.core.domain.branches.dto import MangaBranchCreateDTO | ||
from app.db.models import Manga, MangaBranch | ||
from lib.db import DBContext | ||
|
||
|
||
class MangaBranchService: | ||
def __init__(self, db_context: DBContext) -> None: | ||
self._db_context = db_context | ||
|
||
async def create( | ||
self, | ||
dto: MangaBranchCreateDTO, | ||
manga: Manga, | ||
) -> MangaBranch: | ||
branch = MangaBranch( | ||
name=dto.name, | ||
language=dto.language, | ||
manga=manga, | ||
) | ||
self._db_context.add(branch) | ||
await self._db_context.flush() | ||
return branch |
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
Empty file.
Empty file.
Oops, something went wrong.