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

Dev Academy Challenge #10

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
28 changes: 21 additions & 7 deletions checkout/package-lock.json

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

56 changes: 56 additions & 0 deletions checkout/src/products/handlers/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const TableName = process.env.SAMPLE_TABLE;

const dynamodb = require("aws-sdk/clients/dynamodb");
const uuid = require("uuid/v4");
const docClient = new dynamodb.DocumentClient();

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

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

const body = JSON.parse(event.body);
body.id = uuid();

const Item = Object.assign({}, body);

var params = {
TableName,
Item
};

const { requestContext } = event;
const Location = !!requestContext
? `https://${requestContext.domainName}${requestContext.path}/${body.id}`
: body.id;

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;
}
};
35 changes: 35 additions & 0 deletions checkout/src/products/handlers/get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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(
`getAllItems only accept GET method, you tried: ${event.method}`
);
}

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

var params = {
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;
};
38 changes: 38 additions & 0 deletions checkout/src/products/handlers/get-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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}`
);
}

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

const id = event.pathParameters.id;

var params = {
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;
};
80 changes: 42 additions & 38 deletions checkout/src/sales/handlers/create.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
const dynamodb = require('aws-sdk/clients/dynamodb');
const uuid = require('uuid/v4');
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) => {
try {
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);
console.info("received:", event);

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

var params = {
TableName: tableName,
Item
};
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": "*"
}
};
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;
}
}
return response;
} catch (error) {
console.log("error %j", error);
const response = {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(error)
};
return response;
}
};
46 changes: 25 additions & 21 deletions checkout/src/sales/handlers/get-all.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
const tableName = process.env.SAMPLE_TABLE;
const dynamodb = require('aws-sdk/clients/dynamodb');
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}`);
}
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
};
console.info("received:", event);
var params = {
TableName: tableName
};

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

const response = {
statusCode: 200,
body: JSON.stringify(items),
headers: {
"Access-Control-Allow-Origin": "*"
}
};
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;
}
console.info(
`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`
);
return response;
};
Loading