Skip to content

Commit ea1971f

Browse files
committed
update listing draft command
1 parent 8b06aa8 commit ea1971f

6 files changed

Lines changed: 94 additions & 23 deletions

File tree

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
from modules.catalog.application.commands import (
22
CreateListingDraftCommand,
3+
UpdateListingDraftCommand,
34
)
45
from modules.catalog.domain.entities import Listing
5-
from seedwork.application.commands import Command
6+
from modules.catalog.domain.repositories import ListingRepository
67
from seedwork.application.command_handlers import CommandResult
78
from seedwork.application.decorators import command_handler
89

910

1011
@command_handler
1112
def create_listing_draft(
12-
command: CreateListingDraftCommand, repository
13+
command: CreateListingDraftCommand, repository: ListingRepository
1314
) -> CommandResult:
1415
listing = Listing(**command.dict())
1516
try:
1617
repository.insert(listing)
1718
except:
18-
return CommandResult.error("Failed to add item")
19+
return CommandResult.error("Failed to create listing")
1920

2021
return CommandResult.ok(id=listing.id)
22+
23+
24+
@command_handler
25+
def update_listing_draft(
26+
command: UpdateListingDraftCommand, repository: ListingRepository
27+
) -> CommandResult:
28+
listing = repository.get_by_id(command.listing_id)
29+
listing.change_main_attributes(
30+
title=command.title, description=command.description, price=command.price
31+
)
32+
try:
33+
repository.update(listing)
34+
except:
35+
return CommandResult.error("Failed to update listing")
36+
37+
return CommandResult.ok()
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from datetime import datetime
2+
3+
from seedwork.application.command_handlers import CommandResult
14
from seedwork.application.commands import Command
25
from seedwork.domain.value_objects import Currency, UUID
36

@@ -9,18 +12,9 @@ class CreateListingDraftCommand(Command):
912
seller_id: UUID
1013

1114

12-
# class UpdateAuctionItemCommand(Command):
13-
# title: str
14-
# description: str
15-
16-
17-
# class DeleteAuctionItemCommand(Command):
18-
# pass
19-
20-
21-
# class PublishAuctionItemCommand(Command):
22-
# pass
23-
24-
25-
# class UnpublishAuctionItemCommand(Command):
26-
# pass
15+
class UpdateListingDraftCommand(Command):
16+
listing_id: UUID
17+
title: str
18+
description: str
19+
price: Currency
20+
modify_user_id: UUID
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
class ListingRepository:
2-
pass
1+
from seedwork.domain.repositories import GenericRepository
2+
3+
4+
class ListingRepository(GenericRepository):
5+
"""An interface for Listing repository"""

src/modules/catalog/tests/application/test_command_handlers.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import pytest
2-
from modules.catalog.application.commands import CreateListingDraftCommand
3-
from modules.catalog.application.command_handlers import create_listing_draft
2+
from modules.catalog.application.commands import (
3+
CreateListingDraftCommand,
4+
UpdateListingDraftCommand,
5+
)
6+
from modules.catalog.application.command_handlers import (
7+
create_listing_draft,
8+
update_listing_draft,
9+
)
10+
from modules.catalog.domain.entities import Listing
411
from seedwork.infrastructure.repository import InMemoryRepository
512
from seedwork.domain.value_objects import UUID
613

@@ -18,3 +25,27 @@ def test_create_listing_draft():
1825
# assert
1926
assert repository.get_by_id(result.id).title == "foo"
2027
assert result.has_errors() is False
28+
29+
30+
def test_update_listing_draft():
31+
# arrange
32+
repository = InMemoryRepository()
33+
listing = Listing(
34+
title="Tiny dragon",
35+
description="Tiny dragon for sale",
36+
price=1,
37+
seller_id=UUID.v4(),
38+
)
39+
repository.insert(listing)
40+
41+
command = UpdateListingDraftCommand(
42+
listing_id=listing.id,
43+
title="Tiny golden dragon",
44+
description=listing.description,
45+
price=listing.price,
46+
modify_user_id=listing.seller_id,
47+
)
48+
49+
# act
50+
result = update_listing_draft(command, repository)
51+
assert result.is_ok()

src/seedwork/application/command_handlers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ def has_errors(self):
1818
def add_error(self, error):
1919
self.__errors.append(error)
2020

21+
def is_ok(self):
22+
return not self.has_errors()
23+
2124
@classmethod
2225
def ok(cls, **kwargs):
2326
return CommandResult(**kwargs)
2427

2528
@classmethod
26-
def error(cls, errors):
29+
def errors(cls, errors):
30+
result = CommandResult()
31+
for error in errors:
32+
result.add_error(error)
33+
return result
34+
35+
@classmethod
36+
def errors(cls, errors):
2737
result = CommandResult()
2838
for error in errors:
2939
result.add_error(error)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .entities import Entity
2+
from .value_objects import UUID
3+
4+
5+
class GenericRepository:
6+
def get_by_id(self, id: UUID) -> Entity:
7+
pass
8+
9+
def insert(self, entity: Entity):
10+
pass
11+
12+
def update(self, entity: Entity):
13+
pass
14+
15+
def delete(self, entity_id: UUID):
16+
pass

0 commit comments

Comments
 (0)