Skip to content

Commit 80bdf8a

Browse files
DHAMODHARABALAJI RDHAMODHARABALAJI R
authored andcommitted
get orders
1 parent 271a160 commit 80bdf8a

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

order-manager/src/handlers/createOrder.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const connectDB = require("../utils/db");
2-
const Order = require("../models/orderModel");
31
// const { setCache } = require("../utils/cache");
42
// const { sendToQueue } = require("../utils/sqs");
53
const { v4: uuidv4 } = require("uuid");
@@ -19,10 +17,8 @@ exports.handler = async (event) => {
1917
throw 'Items not configured in the payload'
2018
}
2119

22-
console.log('Processing Order Id:', id, ', Item Count: ', items.length);
23-
2420
const order = {
25-
id,
21+
id: uuidv4(),
2622
items
2723
};
2824

@@ -35,6 +31,7 @@ exports.handler = async (event) => {
3531
console.log('order-created:', order, response);
3632

3733
// Payload Sample
34+
3835
// {
3936
// "id": "2",
4037
// "items": [
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
const connectDB = require("../utils/db");
2-
const Order = require("../models/orderModel");
1+
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
2+
const { ScanCommand, DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
3+
const client = new DynamoDBClient({});
4+
const docClient = DynamoDBDocumentClient.from(client);
5+
const ordersTable = "OrdersTable";
36

47
exports.handler = async (event) => {
5-
await connectDB();
68

7-
order = await Order.find({});
8-
if (!order) return { statusCode: 404, body: "Order not found" };
9+
const body = await dynamo.send(
10+
new ScanCommand({
11+
TableName: ordersTable,
12+
})
13+
);
14+
15+
console.log('items: ', body.Items)
16+
17+
if (!body || !body.Items) return { statusCode: 404, body: "Orders not found" };
918

1019
return {
1120
statusCode: 200,
12-
body: JSON.stringify(order),
21+
body: JSON.stringify(body.Items),
1322
};
1423
};

order-manager/template.yaml

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,34 @@ Resources:
8484
AppGroup: OrdersLambda
8585
AppType: Serverless
8686

87-
# GetMyOrdersFunction:
88-
# Type: AWS::Serverless::Function
89-
# Properties:
90-
# CodeUri: src/
91-
# Handler: handlers/getMyOrders.handler
92-
# Runtime: nodejs22.x
93-
# MemorySize: 128
94-
# Timeout: 10
95-
# Policies:
96-
# - AWSLambdaVPCAccessExecutionRole
97-
# - AmazonDynamoDBFullAccess
98-
# - AWSLambdaSQSQueueExecutionRole
99-
# - AWSLambdaBasicExecutionRole
100-
# - AWSSecretsManagerGetSecretValuePolicy:
101-
# SecretArn: !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:mongodb/credentials-*"
102-
# Environment:
103-
# Variables:
104-
# HOSTING_ENV: !Ref HOSTINGENV
105-
# DB_URI: ""
106-
# REDIS_HOST: ""
107-
# MONGODB_SECRET_ARN: !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:mongodb/credentials"
108-
# VpcConfig:
109-
# SecurityGroupIds: !Ref SecurityGroupIds
110-
# SubnetIds: !Ref SubnetIds
111-
# Events:
112-
# ApiEvent:
113-
# Type: Api
114-
# Properties:
115-
# Path: /myorders
116-
# Method: post
117-
# RestApiId: !Ref OrderApi
87+
GetMyOrdersFunction:
88+
Type: AWS::Serverless::Function
89+
Properties:
90+
CodeUri: src/
91+
Handler: handlers/getMyOrders.handler
92+
Runtime: nodejs22.x
93+
MemorySize: 128
94+
Timeout: 10
95+
Policies:
96+
- DynamoDBCrudPolicy:
97+
TableName:
98+
!Ref DynamoDBOrdersTable
99+
- AWSLambdaVPCAccessExecutionRole
100+
- AmazonDynamoDBFullAccess
101+
- AWSLambdaSQSQueueExecutionRole
102+
- AWSLambdaBasicExecutionRole
103+
- AWSSecretsManagerGetSecretValuePolicy:
104+
SecretArn: !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:mongodb/credentials-*"
105+
Environment:
106+
Variables:
107+
HOSTING_ENV: !Ref HOSTINGENV
108+
Events:
109+
ApiEvent:
110+
Type: Api
111+
Properties:
112+
Path: /myorders
113+
Method: get
114+
RestApiId: !Ref OrderApi
118115

119116
Outputs:
120117
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function

0 commit comments

Comments
 (0)