-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathcatalog.py
More file actions
110 lines (94 loc) · 3.27 KB
/
catalog.py
File metadata and controls
110 lines (94 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import Annotated
from fastapi import APIRouter, Depends
from api.dependencies import Application, User, get_application, get_authenticated_user
from api.models.catalog import ListingIndexModel, ListingReadModel, ListingWriteModel
from modules.catalog.application.command import (
CreateListingDraftCommand,
DeleteListingDraftCommand,
PublishListingDraftCommand,
)
from modules.catalog.application.query.get_all_listings import GetAllListings
from modules.catalog.application.query.get_listing_details import GetListingDetails
from seedwork.domain.value_objects import GenericUUID, Money
"""
Inspired by https://developer.ebay.com/api-docs/sell/inventory/resources/offer/methods/createOffer
"""
router = APIRouter()
@router.get("/catalog", tags=["catalog"], response_model=ListingIndexModel)
async def get_all_listings(app: Annotated[Application, Depends(get_application)]):
"""
Shows all published listings in the catalog
"""
query = GetAllListings()
result = await app.execute_async(query)
return dict(data=result)
@router.get("/catalog/{listing_id}", tags=["catalog"], response_model=ListingReadModel)
async def get_listing_details(
listing_id, app: Annotated[Application, Depends(get_application)]
):
"""
Shows listing details
"""
query = GetListingDetails(listing_id=listing_id)
query_result = await app.execute_async(query)
return dict(data=query_result.payload)
@router.post(
"/catalog", tags=["catalog"], status_code=201, response_model=ListingReadModel
)
async def create_listing(
request_body: ListingWriteModel,
app: Annotated[Application, Depends(get_application)],
current_user: Annotated[User, Depends(get_authenticated_user)],
):
"""
Creates a new listing
"""
command = CreateListingDraftCommand(
listing_id=GenericUUID.next_id(),
title=request_body.title,
description=request_body.description,
ask_price=Money(request_body.ask_price_amount, request_body.ask_price_currency),
seller_id=current_user.id,
)
app.execute(command)
query = GetListingDetails(listing_id=command.listing_id)
query_result = app.execute_query(query)
return dict(query_result.payload)
@router.delete(
"/catalog/{listing_id}", tags=["catalog"], status_code=204, response_model=None
)
async def delete_listing(
listing_id,
app: Annotated[Application, Depends(get_application)],
current_user: Annotated[User, Depends(get_authenticated_user)],
):
"""
Deletes a listing
"""
command = DeleteListingDraftCommand(
listing_id=listing_id,
seller_id=current_user.id,
)
await app.execute_async(command)
@router.post(
"/catalog/{listing_id}/publish",
tags=["catalog"],
status_code=200,
response_model=ListingReadModel,
)
async def publish_listing(
listing_id: GenericUUID,
app: Annotated[Application, Depends(get_application)],
current_user: Annotated[User, Depends(get_authenticated_user)],
):
"""
Publishes a listing
"""
command = PublishListingDraftCommand(
listing_id=listing_id,
seller_id=current_user.id,
)
await app.execute_async(command)
query = GetListingDetails(listing_id=listing_id)
response = await app.execute_async(query)
return response