|
1 | 1 | from modules.catalog.application.commands import ( |
2 | 2 | CreateListingDraftCommand, |
| 3 | + UpdateListingDraftCommand, |
3 | 4 | ) |
4 | 5 | from modules.catalog.domain.entities import Listing |
5 | | -from seedwork.application.commands import Command |
| 6 | +from modules.catalog.domain.repositories import ListingRepository |
6 | 7 | from seedwork.application.command_handlers import CommandResult |
7 | 8 | from seedwork.application.decorators import command_handler |
8 | 9 |
|
9 | 10 |
|
10 | 11 | @command_handler |
11 | 12 | def create_listing_draft( |
12 | | - command: CreateListingDraftCommand, repository |
| 13 | + command: CreateListingDraftCommand, repository: ListingRepository |
13 | 14 | ) -> CommandResult: |
14 | 15 | listing = Listing(**command.dict()) |
15 | 16 | try: |
16 | 17 | repository.insert(listing) |
17 | 18 | except: |
18 | | - return CommandResult.error("Failed to add item") |
| 19 | + return CommandResult.error("Failed to create listing") |
19 | 20 |
|
20 | 21 | 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() |
0 commit comments