add delete

This commit is contained in:
2025-07-22 16:02:19 -03:00
parent 1a1d545544
commit 896e0f1054
3 changed files with 60 additions and 10 deletions

View File

@@ -22,11 +22,6 @@ router = Router()
course_layer = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
class CustomPricing(BaseModel):
course_id: UUID4
unit_price: Decimal
@router.get('/<id>/custompricing', compress=True)
def get_custom_pricing(id: str):
result = course_layer.collection.query(
@@ -37,6 +32,11 @@ def get_custom_pricing(id: str):
return result
class CustomPricing(BaseModel):
course_id: UUID4
unit_price: Decimal
@router.post('/<id>/custompricing', compress=True)
def post_custom_pricing(id: str, payload: CustomPricing):
now_ = now()
@@ -46,8 +46,11 @@ def post_custom_pricing(id: str, payload: CustomPricing):
item={
'id': f'CUSTOM_PRICING#ORG#{id}',
'sk': f'COURSE#{payload.course_id}',
'unit_price': payload.unit_price,
'created_at': now_,
}
},
cond_expr='attribute_not_exists(sk)',
exc_cls=CoursConflictError,
)
transact.condition(
key=KeyPair(str(payload.course_id), '0'),
@@ -58,4 +61,22 @@ def post_custom_pricing(id: str, payload: CustomPricing):
return JSONResponse(status_code=HTTPStatus.CREATED)
class Delete(BaseModel):
course_id: UUID4
@router.delete('/<id>/custompricing', compress=True)
def delete_custom_pricing(id: str, payload: Delete):
if course_layer.delete_item(
KeyPair(
f'CUSTOM_PRICING#ORG#{id}',
f'COURSE#{payload.course_id}',
)
):
return JSONResponse(status_code=HTTPStatus.OK)
class CoursConflictError(BadRequestError): ...
class CourseNotFoundError(BadRequestError): ...

View File

@@ -142,6 +142,33 @@ def test_get_custom_pricing(
assert json.loads(r['body']) == expected
def test_delete_custom_pricing(
mock_app,
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
http_api_proxy: HttpApiProxy,
lambda_context: LambdaContext,
):
mock_app.lambda_handler(
http_api_proxy(
raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/custompricing',
method=HTTPMethod.DELETE,
body={'course_id': '281198c2-f293-4acc-b96e-e4a2d5f6b73c'},
),
lambda_context,
)
data = dynamodb_persistence_layer.collection.get_item(
KeyPair(
'CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG',
'COURSE#15ee05a3-4ceb-4b7e-9979-db75b28c9ade',
),
raise_on_error=False,
)
assert not data
def test_post_custom_pricing(
mock_app,
dynamodb_seeds,