62 lines
1.5 KiB
Python
62 lines
1.5 KiB
Python
from decimal import Decimal
|
|
from http import HTTPStatus
|
|
|
|
from aws_lambda_powertools.event_handler.api_gateway import Router
|
|
from aws_lambda_powertools.event_handler.exceptions import (
|
|
BadRequestError,
|
|
)
|
|
from layercake.dateutils import now
|
|
from layercake.dynamodb import (
|
|
DynamoDBPersistenceLayer,
|
|
KeyPair,
|
|
PartitionKey,
|
|
)
|
|
from pydantic import BaseModel
|
|
from pydantic.types import UUID4
|
|
|
|
from api_gateway import JSONResponse
|
|
from boto3clients import dynamodb_client
|
|
from config import COURSE_TABLE
|
|
|
|
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(
|
|
PartitionKey(f'CUSTOM_PRICING#ORG#{id}'),
|
|
limit=100,
|
|
)
|
|
|
|
return result
|
|
|
|
|
|
@router.post('/<id>/custompricing', compress=True)
|
|
def post_custom_pricing(id: str, payload: CustomPricing):
|
|
now_ = now()
|
|
|
|
with course_layer.transact_writer() as transact:
|
|
transact.put(
|
|
item={
|
|
'id': f'CUSTOM_PRICING#ORG#{id}',
|
|
'sk': f'COURSE#{payload.course_id}',
|
|
'created_at': now_,
|
|
}
|
|
)
|
|
transact.condition(
|
|
key=KeyPair(str(payload.course_id), '0'),
|
|
cond_expr='attribute_exists(sk)',
|
|
exc_cls=CourseNotFoundError,
|
|
)
|
|
|
|
return JSONResponse(status_code=HTTPStatus.CREATED)
|
|
|
|
|
|
class CourseNotFoundError(BadRequestError): ...
|