Skip to content
Closed
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
5 changes: 5 additions & 0 deletions E-Commerce-API/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cyf_ecommerce
DB_USERNAME=farnooshmoayeri
DB_PASSWORD=Farnic
40 changes: 40 additions & 0 deletions E-Commerce-API/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
const express = require("express");
const { Pool } = require("pg");
require("dotenv").config();
const app = express();

const db = new Pool({
user: process.env.DB_USERNAME,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: 5432,
});

// Your code to run the server should go here
// Don't hardcode your DB password in the code or upload it to GitHub! Never ever do this ever.
// Use environment variables instead:
// https://www.codementor.io/@parthibakumarmurugesan/what-is-env-how-to-set-up-and-run-a-env-file-in-node-1pnyxw9yxj
app.get("/products", async (req, res) => {
await db
.query(
`SELECT products.product_name, product_availability.unit_price, suppliers.supplier_name
FROM products
JOIN product_availability ON products.id = product_availability.prod_id
JOIN suppliers ON product_availability.supp_id = suppliers.id`
)
.then((result) => {
res.json(
result.rows.map((row) => ({
name: row.product_name,
price: row.unit_price,
supplierName: row.supplier_name,
}))
);
})
.catch((error) => {
console.log(error);
});
});
// res.json([
// {
// name: " ",
// price: 0,
// supplierName: "",
// },
// ]);
// });

module.exports = app;
Loading