Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
748 changes: 748 additions & 0 deletions javascriptv3/example_code/cross-services/ddb-item-tracker/README.md

Large diffs are not rendered by default.

269 changes: 269 additions & 0 deletions javascriptv3/example_code/cross-services/ddb-item-tracker/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0

Purpose:
app.js is the main file in the Amazon DynamoDB Item Tracker example app.
It interacts with the Express web framework to execute code.

INPUTS:
- REGION

*/
// snippet-start:[cross-service.JavaScript.ddb-item-tracker.app]
import express from "express";
import { v4 as uuidv4 } from "uuid";
const port = process.env.PORT || 3000;
const app = express();
import bodyParser from "body-parser";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
import { UpdateCommand, PutCommand, ScanCommand } from "@aws-sdk/lib-dynamodb";
import { ddbDocClient } from "./public/libs/ddbDocClient.js";
import { SendEmailCommand } from "@aws-sdk/client-ses";
import { sesClient, REGION } from "./public/libs/sesClient.js";
import { s3Client } from "./public/libs/s3Client.js";
import { PutObjectCommand } from "@aws-sdk/client-s3";

// Setting path for public directory
const __dirname = path.dirname(__filename);
const static_path = path.join(__dirname, "/public");
app.use(express.static(static_path));
app.use(express.urlencoded({ extended: true }));

// Variables to update.
export const tableName = "TABLE_NAME";
export const bucketName = "BUCKET_NAME";
export const senderEmail = "SENDER_EMAIL_ADDRESS";

app.use(bodyParser.json());

app.post("/add", (req, res) => {
const body = req.body;
const id = uuidv4();
const d = new Date();
const month = d.getMonth() + 1;
const day = d.getDate();
const todaydate =
d.getFullYear() +
"/" +
(month < 10 ? "0" : "") +
month +
"/" +
(day < 10 ? "0" : "") +
day;
const params = {
TableName: tableName,
Item: {
id: id,
guide: body.guide,
description: body.description,
status: body.status,
date: todaydate,
},
};
const run = async () => {
try {
const data = await ddbDocClient.send(new PutCommand(params));
console.log("Added item:", JSON.stringify(data, null, 2));
console.log(data);
res.contentType = "application/json";
res.send(data);
} catch (err) {
console.error(
"Unable to add item. Error JSON:",
JSON.stringify(err, null, 2)
);
}
};
run();
});

app.post("/request", (req, res) => {
var params = {
TableName: tableName,
};

const run = async () => {
try {
const data = await ddbDocClient.send(new ScanCommand(params));
console.log("data", data);
res.send(data);
} catch (err) {
console.log("Error", err);
}
};
run();
});

app.post("/changewi", (req, res) => {
const body = req.body;
console.log(req.body);
var params = {
TableName: tableName,
Key: {
id: req.body.id,
},
UpdateExpression: "set #description=:d, #status=:s",
ExpressionAttributeValues: {
":d": req.body.description,
":s": req.body.status,
},
ExpressionAttributeNames: {
"#description": "description",
"#status": "status",
},
};
const run = async () => {
try {
const data = await ddbDocClient.send(new UpdateCommand(params));
res.contentType = "application/json";
res.send(data);
} catch (err) {
console.error(err);
}
};
run();
});

app.post("/uploadCSV", (req, res) => {
const d = new Date();
const month = d.getMonth() + 1;
const day = d.getDate();
const hour = d.getHours();
const minute = d.getMinutes();
const todaydate =
d.getFullYear() +
(month < 10 ? "0" : "") +
month +
(day < 10 ? "0" : "") +
day +
hour +
minute;
const uploadParams = {
Bucket: bucketName,
Body: req.body.csv,
Key: "Items_" + todaydate + ".csv",
};
const run = async () => {
try {
const data = await s3Client.send(new PutObjectCommand(uploadParams));
const linkToCSV =
"https://s3.console.aws.amazon.com/s3/buckets/" +
uploadParams.Bucket +
"?region=" +
REGION +
"&tab=objects";
console.log("Success. Report uploaded to " + linkToCSV + ".");
console.log("Email", req.body.email);
try {
const emailParams = {
Destination: {
/* required */
CcAddresses: [
/* more items */
],
ToAddresses: [
req.body.email, //RECEIVER_ADDRESS
/* more To-email addresses */
],
},
Message: {
/* required */
Body: {
/* required */
Html: {
Charset: "UTF-8",
Data:
"<h1>Hello!</h1>" +
"<p> The CSV has been uploaded to <a href=" +
linkToCSV +
">here.</a></p>",
},
},
Subject: {
Charset: "UTF-8",
Data: "Report ready.",
},
},
Source: "SENDER_EMAIL_ADDRESS",
ReplyToAddresses: [
/* more items */
],
};
const data = await sesClient.send(new SendEmailCommand(emailParams));
console.log("Success. Email sent.", data);
res.contentType = "application/json";
res.send(data);
} catch (err) {
console.log("Error", err);
}
} catch (err) {
console.log("Error", err);
}
};
run();
module.exports = run();
});

app.post("/report", (req, res) => {
// Helper function to send an email to user.
// Set the parameters
console.log("This is the email address: ", req.body.email);
const params = {
Destination: {
/* required */
CcAddresses: [
/* more items */
],
ToAddresses: [
req.body.email, //RECEIVER_ADDRESS
/* more To-email addresses */
],
},
Message: {
/* required */
Body: {
/* required */
Html: {
Charset: "UTF-8",
Data:
"<h1>Hello!</h1>" +
"<p> The Amazon DynamoDB table " +
tableName +
" has been updated with PPE information <a href='https://" +
REGION +
".console.aws.amazon.com/dynamodb/home?region=" +
REGION +
"#item-explorer?table=" +
tableName +
"'>here.</a></p>",
},
},
Subject: {
Charset: "UTF-8",
Data: "PPE image report ready.",
},
},
Source: senderEmail,
ReplyToAddresses: [
/* more items */
],
};
const run = async () => {
try {
const data = await sesClient.send(new SendEmailCommand(params));
console.log("Success. Email sent.", data);
res.contentType = "application/json";
res.send(data);
} catch (err) {
console.log("Error", err);
}
};
run();
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
// snippet-end:[cross-service.JavaScript.ddb-item-tracker.app]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "Amazon DynamoDB Item Tracker cross-service example (AWS SDK for JavaScript)",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"dependencies": {
"@aws-sdk/client-cognito-identity": "^3.32.0",
"@aws-sdk/client-dynamodb": "^3.32.0",
"@aws-sdk/client-ses": "^3.32.0",
"@aws-sdk/credential-provider-cognito-identity": "^3.32.0",
"@aws-sdk/lib-dynamodb": "^3.32.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"jest": "^26.6.3",
"node-uuid": "^1.4.8",
"uuid": "^8.1.0"
},
"author": "",
"license": "Apache 2.0",
"type": "module",
"devDependencies": {
"prettier": "2.5.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0

Purpose:
add.html is a HTML template for an Amazon DynamoDB item tracker app.-->

<!--snippet-start:[cross-service.HTML.ddb-item-tracker.add]-->

<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<html>
<head>
<title>Modify Items</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ></script>
<script src="../js/items.js" ></script>

<!-- CSS files -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" >
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.css"/>
<link rel="stylesheet" href="/css/styles.css" />
<link rel="stylesheet" href="/css/col.css" />
<link rel="stylesheet" href="/css/button.css" />
<link rel="stylesheet" href="/css/common.css" />

</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="index.html">Welcome</a>
<a class="nav-item nav-link" href="add.html">Add items <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="items.html">Get items</a>
</div>
</div>
</nav>
<div class="container" id="newItem">
<h3>Welcome to Amazon DynamoDB Item Tracker</h3>
<p>Add new items by filling in this table and clicking <i>Create Item</i></p>

<div class="row">
<div class="col-lg-8 mx-auto">

<form>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Guide</label>
<input class="form-control" id="guide" type="guide" placeholder="AWS Guide/AWS API" required="required" data-validation-required-message="Please enter the AWS Guide.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Description</label>
<textarea class="form-control" id="description" rows="5" placeholder="Description" required="required" data-validation-required-message="Please enter a description."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Status</label>
<textarea class="form-control" id="status" rows="5" placeholder="Status" required="required" data-validation-required-message="Please enter the status."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
</form>
<button onclick="addItem()">Create Item</button>

</div>
</div>
</div>
</body>
</html>
<!--snippet-end:[cross-service.HTML.ddb-item-tracker.add]-->
Loading