Skip to content

Commit

Permalink
Products: Adding id as PK (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
enrique-ayala authored Feb 20, 2024
1 parent 061c909 commit 9cf576b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
40 changes: 25 additions & 15 deletions src/products/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Own's modules
from product_modules.dao.product_dao import ProductDAO
from product_modules.models.product import HIBerryProduct
from product_modules.models.product import HIBerryProduct, HIBerryProductUpdate
from product_modules.utils.doorman import DoormanUtil
from product_modules.errors.auth_error import AuthError
from product_modules.data_mapper.product_mapper import ProductHelper
Expand Down Expand Up @@ -41,9 +41,9 @@ def create_product(event: Dict[str, Any], context: LambdaContext) -> Dict[str, A
raise AuthError(f"User {username} is not authorized to create a product")

body = doorman.get_body_from_request()

logger.debug(f"Incoming data is {body=} and {username=}")

new_product_data = HIBerryProduct(**body)

logger.info(
Expand All @@ -55,12 +55,19 @@ def create_product(event: Dict[str, Any], context: LambdaContext) -> Dict[str, A
product_data=new_product_data.__dict__, username=username
)
dao = ProductDAO()
dao.create_product(product_db_data)
logger.info("Product received and created")
return doorman.build_response(
payload={"message": "Record was created"}, status_code=201
)
create_response = dao.create_product(product_db_data)

if create_response["status"] == "success":
logger.info("Product received and created")
output_data = {"id": product_db_data["id"]}

logger.debug(f"Outgoing data is {output_data=}")
return doorman.build_response(payload=output_data, status_code=201)
else:
return doorman.build_response(
payload={"message": create_response["message"]},
status_code=create_response.get("status_code", 500),
)
except ValidationError as validation_error:
error_details = "Some fields failed validation"
if validation_error._error_cache:
Expand Down Expand Up @@ -105,13 +112,13 @@ def get_all_products(event: Dict[str, Any], context: LambdaContext) -> Dict[str,
is_auth = doorman.auth_user()
if is_auth is False:
raise AuthError(f"User {username} is not authorized to retrieve a product")

logger.debug(f"Incoming data is {username=}")

dao = ProductDAO()
products = dao.fetch_products()
output_data = products

logger.debug(f"Outgoing data is {output_data=}")
return doorman.build_response(payload=output_data, status_code=200)

Expand Down Expand Up @@ -154,9 +161,9 @@ def delete_product(event: Dict[str, Any], context: LambdaContext) -> Dict[str, A
product_name = doorman.get_query_param_from_request(
_query_param_name="name", _is_required=True
)

logger.debug(f"Incoming data is {product_name=} and {username=}")

dao = ProductDAO()
delete_response = dao.delete_product(product_name)

Expand Down Expand Up @@ -213,12 +220,15 @@ def update_product(event: Dict[str, Any], context: LambdaContext) -> Dict[str, A

logger.debug(f"Incoming data is {body=} and {username=}")

updated_product_data = HIBerryProduct(**body)

updated_product_data = HIBerryProductUpdate(**body)
builder = ProductHelper()
product_db_data = builder.build_product(
product_data=updated_product_data.__dict__, username=username
)
logger.info(f"Updating product: {updated_product_data.name}")

dao = ProductDAO()
update_response = dao.update_product(updated_product_data.__dict__)
update_response = dao.update_product(product_db_data)

if update_response.get("status") == "success":
return doorman.build_response(
Expand Down
5 changes: 5 additions & 0 deletions src/products/product_modules/data_mapper/product_mapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Python's libraries
import uuid
from typing import Dict
from typing import Any
from datetime import datetime
Expand All @@ -20,8 +21,12 @@ def build_product(
Returns:
Object needed by DynamoDB to create a record
"""
id = product_data.get("id", None)
if id is None:
id = str(uuid.uuid4())

data = {
"id": id,
"name": product_data["name"],
"price": product_data["price"],
"created_by": username,
Expand Down
4 changes: 4 additions & 0 deletions src/products/product_modules/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
class HIBerryProduct(BaseModel):
name: StrictStr
price: confloat(ge=0.0)


class HIBerryProductUpdate(HIBerryProduct):
id: StrictStr
4 changes: 2 additions & 2 deletions src/products/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Resources:
Properties:
TableName: Products
AttributeDefinitions:
- AttributeName: name
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: name
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
Expand Down

0 comments on commit 9cf576b

Please sign in to comment.