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

desafio backend + aws #14

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
47 changes: 33 additions & 14 deletions checkout/package-lock.json

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

34 changes: 34 additions & 0 deletions checkout/src/products/handlers/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const dynamodb = require("aws-sdk/clients/dynamodb");
const uuid = require("uuid/v4");
const docClient = new dynamodb.DocumentClient();
const tableName = process.env.SAMPLE_TABLE;

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

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

var params = {
TableName: tableName,
Item
};

await docClient.put(params).promise();
const response = {
statusCode: 201,
headers: {
"Access-Control-Allow-Origin": "*"
}
};
return response;
} catch (error) {
return error;
}
};
35 changes: 35 additions & 0 deletions checkout/src/products/handlers/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const dynamodb = require("aws-sdk/clients/dynamodb");
const db = new dynamodb.DocumentClient();
const tableName = process.env.SAMPLE_TABLE;

module.exports.deleteProduct = async event => {
try {
if (event.httpMethod !== "DELETE") {
throw new Error(
`delete product only accepts GET method, you tried: ${event.httpMethod} method.`
);
}

const id = event.pathParameters.id;

const params = {
TableName: tableName,
Key: {
id: id
}
};

await db.delete(params).promise();

const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
}
};

return response;
} catch (error) {
return error;
}
};
60 changes: 0 additions & 60 deletions checkout/src/products/handlers/get-all-mocking.js

This file was deleted.

31 changes: 31 additions & 0 deletions checkout/src/products/handlers/get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const tableName = process.env.SAMPLE_TABLE;
const dynamodb = require("aws-sdk/clients/dynamodb");
const docClient = new dynamodb.DocumentClient();

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

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;
};
33 changes: 33 additions & 0 deletions checkout/src/products/handlers/get-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const tableName = process.env.SAMPLE_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}`
);
}

const id = event.pathParameters.id;

const params = {
TableName: tableName,
Key: { id: id }
};

const data = await docClient.scan(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;
};
Loading