Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Desafio Casa Magalhães #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
venv/
*.config
*.config
.vscode
4 changes: 4 additions & 0 deletions checkout/events/sales/event-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"httpMethod": "POST",
"body": "{\"cardNumber\": \"q34135\",\"cardHolder\": \"135\",\"expirationMonth\": \"1\",\"expirationYear\": \"1\",\"cvvCode\": \"1\"}",
}
41 changes: 30 additions & 11 deletions checkout/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion checkout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"aws-sdk": "^2.437.0"
"aws-sdk": "^2.437.0",
"uuid": "^3.3.3"
},
"devDependencies": {
"jest": "^24.7.1"
Expand Down
45 changes: 45 additions & 0 deletions checkout/src/products/handlers/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const dynamodb = require('aws-sdk/clients/dynamodb');
const uuid = require('uuid/v4');
const docClient = new dynamodb.DocumentClient();
const tableName = process.env.PRODUCT_TABLE;
exports.createProductHandler = async (event) => {
try {
if (event.httpMethod !== 'POST') {
throw new Error(`postMethod only accepts POST method, you tried: ${event.httpMethod} method.`);
}

console.info('received:', event);

const body = JSON.parse(event.body);
body.id = uuid();
const Item = Object.assign({}, body);

var params = {
TableName: tableName,
Item
};

const { requestContext } = event;
const Location = !!requestContext ? `https://${requestContext.domainName}${requestContext.path}/${body.id}` : body.id;
const result = await docClient.put(params).promise();
const response = {
statusCode: 201,
headers: {
Location,
"Access-Control-Allow-Origin": "*"
}
};

return response;
} catch (error) {
console.log('error %j', error);
const response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(error)
}
return response;
}
}
28 changes: 28 additions & 0 deletions checkout/src/products/handlers/get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const tableName = process.env.PRODUCT_TABLE;
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient();

exports.getAllHandler = async (event) => {
if (event.httpMethod !== 'GET') {
throw new Error(`getAllItems only accept GET method, you tried: ${event.httpMethod}`);
}

console.info('received:', event);
var params = {
TableName: tableName
};

const data = await docClient.scan(params).promise();
const items = data.Items;

const response = {
statusCode: 200,
body: JSON.stringify(items),
headers: {
"Access-Control-Allow-Origin": "*"
}
};

console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
return response;
}
30 changes: 30 additions & 0 deletions checkout/src/products/handlers/get-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const tableName = process.env.PRODUCT_TABLE;
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient();

exports.getByIdHandler = async (event) => {
if (event.httpMethod !== 'GET') {
throw new Error(`getMethod only accept GET method, you tried: ${event.httpMethod}`);
}
console.info('received:', event);

const id = event.pathParameters.id;

var params = {
TableName: tableName,
Key: { id: id },
};
const data = await docClient.get(params).promise();
const item = data.Item;

const response = {
statusCode: 200,
body: JSON.stringify(item),
headers: {
"Access-Control-Allow-Origin": "*"
}
};

console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
return response;
}
71 changes: 40 additions & 31 deletions checkout/src/sales/handlers/create.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
const dynamodb = require('aws-sdk/clients/dynamodb');
const uuid = require('uuid/v4');
const docClient = new dynamodb.DocumentClient();

const tableName = process.env.SAMPLE_TABLE;

exports.createSaleHandler = async(event) => {
if (event.httpMethod !== 'POST') {
throw new Error(`postMethod only accepts POST method, you tried: ${event.httpMethod} method.`);
exports.createSaleHandler = async (event) => {
try {
if (event.httpMethod !== 'POST') {
throw new Error(`postMethod only accepts POST method, you tried: ${event.httpMethod} method.`);
}

console.info('received:', event);

const body = JSON.parse(event.body);
body.id = uuid();
const Item = Object.assign({}, body);

var params = {
TableName: tableName,
Item
};

const { requestContext } = event;
const Location = !!requestContext ? `https://${requestContext.domainName}${requestContext.path}/${body.id}` : body.id;
const result = await docClient.put(params).promise();
const response = {
statusCode: 201,
headers: {
Location,
"Access-Control-Allow-Origin": "*"
}
};

return response;
} catch (error) {
console.log('error %j', error);
const response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(error)
}
return response;
}

console.info('received:', event);

const body = JSON.parse(event.body)
const id = body.id;
const timestamp = new Date().toISOString();
const totalPrice = body.totalPrice;
const items = body.items;
const payment = body.payment;

var params = {
TableName: tableName,
Item: { id, timestamp, totalPrice, items, payment }
};

const result = await docClient.put(params).promise();

const response = {
statusCode: 201,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(body)
};

return response;
}
Loading