11from http import HTTPStatus
22
33from aws_lambda_powertools .utilities .data_classes import APIGatewayProxyEvent
4- from domain .core .cpm_product import CpmProduct , CpmProductIncomingParams
4+ from domain .core .cpm_product import CpmProduct
55from domain .core .cpm_system_id import ProductId
66from domain .core .product_team .v3 import ProductTeam
7+ from domain .repository .cpm_product_repository .v3 import CpmProductRepository
78from domain .repository .product_team_repository .v2 import ProductTeamRepository
9+ from domain .request_models .v1 import (
10+ CreateCpmProductIncomingParams ,
11+ ProductTeamPathParams ,
12+ )
813from domain .response .validation_errors import (
914 InboundValidationError ,
1015 mark_json_decode_errors_as_inbound ,
16+ mark_validation_errors_as_inbound ,
1117)
12- from event .aws .client import dynamodb_client
1318from event .step_chain import StepChain
1419from pydantic import ValidationError
1520
1621
22+ @mark_validation_errors_as_inbound
23+ def parse_path_params (data , cache ) -> ProductTeamPathParams :
24+ event = APIGatewayProxyEvent (data [StepChain .INIT ])
25+ return ProductTeamPathParams (** event .path_parameters )
26+
27+
1728@mark_json_decode_errors_as_inbound
1829def parse_event_body (data , cache ) -> dict :
1930 event = APIGatewayProxyEvent (data [StepChain .INIT ])
2031 return event .json_body if event .body else {}
2132
2233
23- def parse_incoming_cpm_product (data , cache ) -> CpmProductIncomingParams :
34+ def parse_incoming_cpm_product (data , cache ) -> CreateCpmProductIncomingParams :
2435 json_body = data [parse_event_body ]
2536 try :
26- incoming_product = CpmProductIncomingParams (** json_body )
37+ incoming_product = CreateCpmProductIncomingParams (** json_body )
2738 return incoming_product
2839 except ValidationError as exc :
2940 raise InboundValidationError (errors = exc .raw_errors , model = exc .model )
3041
3142
3243def read_product_team (data , cache ) -> ProductTeam :
33- incoming_product = data [parse_incoming_cpm_product ]
34- event = APIGatewayProxyEvent (data [StepChain .INIT ])
35- product_team_id = event .path_parameters ["product_team_id" ]
44+ path_params : ProductTeamPathParams = data [parse_path_params ]
45+
3646 product_team_repo = ProductTeamRepository (
37- table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = dynamodb_client ()
47+ table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = cache [ "DYNAMODB_CLIENT" ]
3848 )
39- return product_team_repo .read (id = product_team_id )
49+ return product_team_repo .read (id = path_params . product_team_id )
4050
4151
4252def generate_cpm_product_id (data , cache ) -> str :
4353 generated_product_id = ProductId .create ()
4454 return generated_product_id .id
4555
4656
57+ @mark_validation_errors_as_inbound
4758def create_cpm_product (data , cache ) -> CpmProduct :
4859 incoming_product = data [parse_incoming_cpm_product ]
4960 product_team : ProductTeam = data [read_product_team ]
@@ -54,17 +65,26 @@ def create_cpm_product(data, cache) -> CpmProduct:
5465 return product
5566
5667
68+ def save_cpm_product (data , cache ) -> dict :
69+ cpm_product = data [create_cpm_product ]
70+ cpm_product_repo = CpmProductRepository (
71+ table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = cache ["DYNAMODB_CLIENT" ]
72+ )
73+ return cpm_product_repo .write (entity = cpm_product )
74+
75+
5776def set_http_status (data , cache ) -> tuple [HTTPStatus , str ]:
58- product : Product = data [create_cpm_product ]
77+ product : CpmProduct = data [create_cpm_product ]
5978 return HTTPStatus .CREATED , str (product .id )
6079
6180
6281steps = [
82+ parse_path_params ,
6383 parse_event_body ,
6484 parse_incoming_cpm_product ,
6585 read_product_team ,
6686 generate_cpm_product_id ,
6787 create_cpm_product ,
68- # save_product_team ,
88+ save_cpm_product ,
6989 set_http_status ,
7090]
0 commit comments