Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backend] added: function add dummy data #84

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
128 changes: 128 additions & 0 deletions backend/functions/add-dummy-data/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const dbConnection = require("dbConnection.js");
var mysql = require("mysql");

/**
* Sample Lambda function which mocks the operation of buying a random number of shares for a stock.
* For demonstration purposes, this Lambda function does not actually perform any actual transactions. It simply returns a mocked result.
*
* @param {Object} event - Input event to the Lambda function
* @param {Object} context - Lambda Context runtime methods and attributes
*
* @returns {Object} object - Object containing details of the stock buying transaction
*
*/
exports.lambdaHandler = async (event, context) => {
const con = await dbConnection.connectDB(
process.env.DatabaseAddress,
"user",
"Password1234",
"databaseAmazonianPrime"
);

var addUserQueries = [`INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (1, "John", "Doe", "[email protected]", "Marketing", false)`,
EllynChan marked this conversation as resolved.
Show resolved Hide resolved
`INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (2, "Alice", "Ather", "[email protected]", null, false)`,
`INSERT INTO Users (UserID, FirstName, LastName, Email, Department, IsAdmin) VALUES (3, "Bob", "Carlson", "[email protected]", "Management", true)`];
EllynChan marked this conversation as resolved.
Show resolved Hide resolved

addUserQueries.forEach(async (queryString) => {
let insertUsers = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into users");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the error message, not a generic message.
if (err) { reject(err);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although returning the error might give you a better idea of what failed, you still need to return where the error failed as well. Perhaps turning the existing reject to a console.log and adding the reject(err) would be best.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes please review again

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! @alkhatibmahmoud

}
resolve(res);
});
});
});

var addListingsQueries = [`INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (1, 1, "listing1", "listing1 description", 10.50, 5, "category1", "used", "2023-03-01 12:00:00", true)`,
`INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (2, 2, "listing2", "listing2 description", 20.00, 1, "category2", "used - good", "2023-03-01 12:00:00", true)`,
`INSERT INTO Listing (ListingID, UserID, ListingName, Description, Cost, Quantity, Category, ItemCondition, PostedTimestamp, IsActiveListing) VALUES (3, 3, "listing3", "listing3 description", 5.00, 10, "category3", "used", "2023-03-10 12:00:00", false)`];

addListingsQueries.forEach(async (queryString) => {
let insertListing = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into listings");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, please do that for all of them. Thanks!

}
resolve(res);
});
});
});

var addCountryQueries = [`INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Vanvouver", "BC", "100 St", "V0V 0V0", "Canada")`,
`INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Richmond", "BC", "1000 St", "V1V 1V1", "Canada")`,
`INSERT INTO Country (CityName, Province, StreetAddress, PostalCode, Country) VALUES ("Toronto", "ON", "2000 St", "V2V 2V2", "Canada")`];

addCountryQueries.forEach(async (queryString) => {
let insertCountry = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into country");
}
resolve(res);
});
});
});

var addAddressQueries = [`INSERT INTO Address (AddressID, UserID, CityName, Province, StreetAddress, IsBillingAddress, IsShippingAddress) VALUES (1, 1, "Vanvouver", "BC", "100 St", true, true)`,
`INSERT INTO Address (AddressID, UserID, CityName, Province, StreetAddress, IsBillingAddress, IsShippingAddress) VALUES (2, 2, "Richmond", "BC", "1000 St", false, true)`,
`INSERT INTO Address (AddressID, UserID, CityName, Province, StreetAddress, IsBillingAddress, IsShippingAddress) VALUES (3, 3, "Toronto", "ON", "2000 St", true, false)`];

addAddressQueries.forEach(async (queryString) => {
let insertAddress = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into address");
}
resolve(res);
});
});
});

var addPaymentDetailsQueries = [`INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (1, 1, 1, 1234123412341234, "1025", 100, "John Doe")`,
`INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (2, 2, 3, 4321432143214321, "0626", 200, "Alice Ather")`,
`INSERT INTO PaymentDetails (PaymentID, UserID, AddressID, CreditCardNum, ExpiryDate, CVV, CardHolderName) VALUES (3, 3, 2, 1234567812345678, "0825", 300, "Bob Carlson")`];

addPaymentDetailsQueries.forEach(async (queryString) => {
let insertPaymentDetails = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into users");
EllynChan marked this conversation as resolved.
Show resolved Hide resolved
}
resolve(res);
});
});
});

var addPaymentMethodQueries = [`INSERT INTO PaymentMethod (PaymentID, UserID) VALUES (1, 1)`,
`INSERT INTO PaymentMethod (PaymentID, UserID) VALUES (2, 2)`,
`INSERT INTO PaymentMethod (PaymentID, UserID) VALUES (3, 3)`];

addPaymentMethodQueries.forEach(async (queryString) => {
let insertPaymentMethod = await new Promise((resolve, reject) => {
con.query(queryString, function (err, res) {
if (err) {
reject("Error insert into users");
}
resolve(res);
});
});
});


const getAllUsersQuery = `SELECT * FROM Users`;

const getAllUsers = await new Promise((resolve, reject) => {
con.query(getAllUsersQuery, function (err, res) {
if (err) {
reject("Couldn't get the address from database!");
}
resolve(res);
});
});

return {
statusCode: 200,
body: JSON.stringify(getAllUsers),
};
};
20 changes: 20 additions & 0 deletions backend/functions/add-dummy-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "hello_world",
"version": "1.0.0",
"description": "hello world sample for NodeJS",
"main": "app.js",
"repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs",
"author": "SAM CLI",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"mysql": "^2.18.1"
},
"scripts": {
"test": "mocha tests/unit/"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^9.1.4"
}
}