Skip to content

Commit 480acdb

Browse files
DHAMODHARABALAJI RDHAMODHARABALAJI R
authored andcommitted
..
1 parent 67b8b32 commit 480acdb

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

order-manager/src/handlers/createOrder.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,52 @@ const Order = require("../models/orderModel");
33
// const { setCache } = require("../utils/cache");
44
// const { sendToQueue } = require("../utils/sqs");
55
const { v4: uuidv4 } = require("uuid");
6-
7-
const DynamoDBClient = require('@aws-sdk/client-dynamodb');
6+
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
7+
const { PutCommand, DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
88
const client = new DynamoDBClient({});
99
const docClient = DynamoDBDocumentClient.from(client);
10-
11-
12-
const dynamodb = new AWS.DynamoDB.DocumentClient();
1310
const ordersTable = "OrdersTable";
1411

1512

1613
exports.handler = async (event) => {
1714
try {
1815

19-
console.log('Environment:', process.env);
16+
const { id, items } = event;
2017

21-
const command = new PutCommand({
22-
TableName: ordersTable,
23-
Item: {
24-
id: uuidv4(),
25-
name: "test1"
26-
},
27-
});
18+
if (!items || !items.length) {
19+
throw 'Items not configured in the payload'
20+
}
2821

29-
const order = await docClient.send(command);
30-
console.log(order);
22+
console.log('Processing Order Id:', id, ', Item Count: ', items.length);
3123

32-
// await connectDB();
33-
34-
// const { customerName, items } = JSON.parse(event.body);
24+
const order = {
25+
id,
26+
items
27+
};
3528

36-
// const totalAmount = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
29+
const command = new PutCommand({
30+
TableName: ordersTable,
31+
Item: order,
32+
});
3733

38-
// const order = new Order({ orderId: uuidv4(), customerName, items, totalAmount });
39-
// await order.save();
34+
const response = await docClient.send(command);
35+
console.log('order-created:', order, response);
36+
37+
// Payload Sample
38+
// {
39+
// "id": "2",
40+
// "items": [
41+
// {
42+
// "id": "1",
43+
// "name": "tv"
44+
// },
45+
// {
46+
// "id": "2",
47+
// "name": "monitor"
48+
// }
49+
// ]
50+
// }
4051

41-
// console.log('order-created:', order);
4252

4353
// await setCache(`order:${order.orderId}`, order);
4454

@@ -48,7 +58,7 @@ exports.handler = async (event) => {
4858

4959
return {
5060
statusCode: 201,
51-
body: JSON.stringify({ message: "Order created", orderId: order.orderId }),
61+
body: JSON.stringify({ message: "Order created", orderId: order.id }),
5262
};
5363
} catch (e) {
5464
console.log(e);

order-manager/template.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,28 @@ Resources:
2727
Properties:
2828
Name: OrderServiceAPI
2929
StageName: v1
30+
Tags:
31+
AppGroup: OrdersLambda
32+
AppType: Serverless
3033

3134
# OrderQueue:
3235
# Type: AWS::SQS::Queue
3336
# Properties:
3437
# QueueName: OrderQueue
3538

39+
DynamoDBTable:
40+
Type: AWS::Serverless::SimpleTable
41+
Properties:
42+
PointInTimeRecoverySpecification: PointInTimeRecoverySpecification
43+
PrimaryKey: PrimaryKeyObject
44+
ProvisionedThroughput: ProvisionedThroughput
45+
SSESpecification: SSESpecification
46+
TableName: OrdersTable
47+
Tags:
48+
AppGroup: OrdersLambda
49+
AppType: Serverless
50+
51+
3652
CreateOrderFunction:
3753
Type: AWS::Serverless::Function
3854
Properties:
@@ -65,6 +81,9 @@ Resources:
6581
Path: /orders
6682
Method: post
6783
RestApiId: !Ref OrderApi
84+
Tags:
85+
AppGroup: OrdersLambda
86+
AppType: Serverless
6887

6988
# GetMyOrdersFunction:
7089
# Type: AWS::Serverless::Function

0 commit comments

Comments
 (0)