Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c2d9f10
Transaction between 30000 and 31000
AppolinFotso Aug 24, 2023
c96d8c9
description containing fee or fees
AppolinFotso Aug 24, 2023
1ca92dd
Joined spends and expense_areas tables
AppolinFotso Aug 24, 2023
0ef2ee9
Group By date
AppolinFotso Aug 25, 2023
61221bf
Group by supplier_id
AppolinFotso Aug 25, 2023
25b6942
Between clause and Group by date
AppolinFotso Aug 25, 2023
2bad300
INSERT INTO statement
AppolinFotso Aug 25, 2023
387e230
product name containing socks
AppolinFotso Aug 25, 2023
abc6102
join products and product_availability tables
AppolinFotso Aug 25, 2023
0a164fe
5 most expensive products
AppolinFotso Aug 25, 2023
bf918bf
suppliers and products name from the UK
AppolinFotso Aug 25, 2023
415da8c
return ordered items by Hope Crosby
AppolinFotso Aug 25, 2023
e1457c1
All products with the order reference ORD006
AppolinFotso Aug 26, 2023
e1d9ff1
Join all the tables
AppolinFotso Aug 26, 2023
1fb64fa
erDiagram between customers and orders
AppolinFotso Aug 26, 2023
55a7f9b
erDiagram between customers and orders
AppolinFotso Aug 26, 2023
d173295
erDiagram between orders and order_items
AppolinFotso Aug 26, 2023
4b3080b
erDiagram from products, product_availability and suppliers
AppolinFotso Aug 26, 2023
d2491c5
erDiagram product_availability and order_items
AppolinFotso Aug 26, 2023
b027f42
erDiagram product_availability and order_items
AppolinFotso Aug 26, 2023
d8e04a6
Merge pull request #1 from AppolinFotso/e-commerce
AppolinFotso Aug 26, 2023
f3a51c8
Add server port and some middlewares
AppolinFotso Sep 13, 2023
e23fd75
Add database pool connection using env variables
AppolinFotso Sep 13, 2023
a890ab3
Added enpoint for GET /products and the test passed
AppolinFotso Sep 13, 2023
340873e
Added test case and enpoint to search product by name
AppolinFotso Sep 13, 2023
2012e13
Added test case and enpoit to search user by id
AppolinFotso Sep 13, 2023
8b7f7f1
Added test case and enpoint to insert new customer
AppolinFotso Sep 13, 2023
39927d7
Added test case and enpoint to create new product
AppolinFotso Sep 13, 2023
d7410b5
Added test case and enpoint for add product availability
AppolinFotso Sep 15, 2023
b68406c
Ignored package-lock json updates
AppolinFotso Sep 15, 2023
cf8659e
Updated endpoint to reject negative unit price
AppolinFotso Sep 15, 2023
0a3473d
Added test case and endpoint to place new order
AppolinFotso Sep 15, 2023
d603411
Added test case and endpoint to update a customer info
AppolinFotso Sep 15, 2023
a7049f8
Added test case and endpoint to delete an order
AppolinFotso Sep 15, 2023
a274fd2
Added test case and endpoint delete existing customer without order
AppolinFotso Sep 15, 2023
dc4e073
Added test case and endpoint to load orders for one customer
AppolinFotso Sep 15, 2023
83bf154
Added dotenv module
AppolinFotso Sep 16, 2023
6ba4387
Added .env file to .gitignore
AppolinFotso Sep 16, 2023
0d2f0c7
Updated some test cases
AppolinFotso Sep 16, 2023
1993715
Updated CORS middleware
AppolinFotso Sep 16, 2023
9935211
Updated CORS middleware
AppolinFotso Sep 16, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@

E-Commerce-API/node_modules/
E-Commerce-API/package-lock.json
E-Commerce-API/development.env

34 changes: 26 additions & 8 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ You are working with Claire and Farnoosh, who are trying to complete a missing r
**You:** Absolutely. Here's the SQL query you need:

```sql
INSERT YOUR QUERY HERE
SELECT * FROM spends WHERE amount BETWEEN 30000 AND 31000;
```

**Claire:** That's great, thanks. Hey, what about transactions that include the word 'fee' in their description?
Expand All @@ -68,39 +68,49 @@ INSERT YOUR QUERY HERE
**You:** Then here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT * FROM spends WHERE description ILIKE '%fee%';
```

**Farnoosh:** Hi, it's me again. It turns out we also need the transactions that have the expense area of 'Better Hospital Food'. Can you help us with that one?

**You:** No worries. Here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT * FROM expense_areas
FULL OUTER JOIN spends ON expense_areas.id = spends.expense_area_id
WHERE expense_area ILIKE 'Better Hospital Food';

```

**Claire:** Great, that's very helpful. How about the total amount spent for each month?

**You:** You can get that by using the GROUP BY clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT date, SUM(amount) as total_spent_per_month
FROM spends
GROUP BY date;
```

**Farnoosh:** Thanks, that's really useful. We also need to know the total amount spent on each supplier. Can you help us with that?

**You:** Sure thing. Here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT supplier_id, SUM(amount) as total_spent_per_month
FROM spends
GROUP BY supplier_id;
```

**Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here.

**You:** Whoops! I gave you ids to key the totals, but let me give you names instead.

```sql
INSERT YOUR QUERY HERE
SELECT suppliers.id, suppliers.supplier, SUM(amount) as total_spent_per_month
FROM suppliers
FULL OUTER JOIN spends ON suppliers.id = spends.supplier_id
GROUP BY suppliers.id;
```

**Claire:** Thanks, that's really helpful. I can't quite figure out...what is the total amount spent on each of these two dates (1st March 2021 and 1st April 2021)?
Expand All @@ -112,7 +122,10 @@ INSERT YOUR QUERY HERE
**You:** Then you need an extra clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT date, SUM(amount) as total_spent_per_month
FROM spends
WHERE date BETWEEN '2021-03-01' AND '2021-04-01'
GROUP BY date;
```

**Farnoosh:** Fantastic. One last thing, looks like we missed something. Can we add a new transaction to the spends table with a description of 'Computer Hardware Dell' and an amount of £32,000?
Expand All @@ -124,7 +137,12 @@ CREATE YOUR QUERY HERE
**You:** Sure thing. To confirm, the date is August 19, 2021, the transaction number is 38104091, the supplier invoice number is 3780119655, the supplier is 'Dell', the expense type is 'Hardware' and the expense area is 'IT'. Here's the query for that:

```sql
INSERT YOUR QUERIES HERE
INSERT INTO expense_types(expense_type) VALUES('Hardware');
INSERT INTO expense_areas(expense_area) VALUES('IT');
INSERT INTO suppliers(supplier) VALUES('Dell');
INSERT INTO spends(expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount) VALUES(42, 46, 66,'2021-08-19', 38104091, 378011955, 'Computer Hardware Dell', 32000);



```

Expand Down
266 changes: 266 additions & 0 deletions E-Commerce-API/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,270 @@ const app = express();
// Use environment variables instead:
// https://www.codementor.io/@parthibakumarmurugesan/what-is-env-how-to-set-up-and-run-a-env-file-in-node-1pnyxw9yxj

require("dotenv").config({ path: "./development.env" });
const { Pool } = require("pg");
const bodyParser = require("body-parser");
// const CORS = require("cors");
const port = process.env.PORT || 5000;
// app.use(CORS());

app.listen(port, () => console.log(`Listening on port ${port}`));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));

const pool = new Pool();

app.get("/products", (req, res) => {
const allProducts = async () => {
try {
const result = await pool.query(
"SELECT p.product_name as name, s.unit_price as price, s.supplier_name as supplierName FROM (SELECT * FROM suppliers FULL OUTER JOIN product_availability as pa ON suppliers.id = pa.supp_id ) as s FULL OUTER JOIN products as p ON s.prod_id = p.id"
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
allProducts();
});
//
app.get("/products/:name", (req, res) => {
const singleProduct = async () => {
try {
const result = await pool.query(
"SELECT p.product_name as name, s.unit_price as price, s.supplier_name as supplierName FROM (SELECT * FROM suppliers FULL OUTER JOIN product_availability as pa ON suppliers.id = pa.supp_id ) as s FULL OUTER JOIN products as p ON s.prod_id = p.id WHERE p.product_name ILIKE $1",
[req.params.name]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
singleProduct();
});
//
app.get("/customers/:customerId", (req, res) => {
const customerId = async () => {
try {
const result = await pool.query("SELECT * FROM customers WHERE id = $1", [
req.params.customerId,
]);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
customerId();
});
//
app.post("/customers", (req, res) => {
const newCustomer = async () => {
try {
await pool.query(
"INSERT INTO customers (name, address, city, country) VALUES($1, $2, $3, $4)",
[req.body.name, req.body.address, req.body.city, req.body.country]
);
const result = await pool.query(
"SELECT * FROM customers WHERE name = $1",
[req.body.name]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
newCustomer();
});
//
app.post("/products", (req, res) => {
const newProduct = async () => {
try {
await pool.query("INSERT INTO products (product_name) VALUES($1)", [
req.body.name,
]);
const result = await pool.query(
"SELECT * FROM products WHERE product_name = $1",
[req.body.name]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
newProduct();
});
//
app.post("/availability", (req, res) => {
const productAvailability = async () => {
try {
let unitPrice = await pool.query(
"INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES($1, $2, $3)",
[
req.body.prodId,
req.body.suppId,
req.body.unitPrice < 0
? "Need a positive integer as unit price"
: req.body.unitPrice,
]
);
const result = await pool.query(
"SELECT * FROM product_availability WHERE prod_id = $1 AND supp_id = $2",
[req.body.prodId, req.body.suppId]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
productAvailability();
});
//
app.post("/customers/:customerId/orders", (req, res) => {
const newOrder = async () => {
try {
const checkCustomerId = await pool.query(
"SELECT id FROM customers WHERE id = $1",
[req.params.customerId]
);
let customerId =
checkCustomerId.rows.length !== 0
? checkCustomerId.rows[0].id
: res.json("Customer ID does not exist");
await pool.query(
"INSERT INTO orders (order_date, order_reference, customer_id) VALUES($1, $2, $3)",
[req.body.orderDate, req.body.orderReference, customerId]
);
const result = await pool.query(
"SELECT * FROM orders WHERE order_reference = $1",
[req.body.orderReference]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
newOrder();
});
//
app.post("/customers/:customerId", (req, res) => {
const customerUpdate = async () => {
try {
const checkCustomerId = await pool.query(
"SELECT id FROM customers WHERE id = $1",
[req.params.customerId]
);
let customerId =
checkCustomerId.rows.length !== 0
? checkCustomerId.rows[0].id
: res.json("Customer ID does not exist");
await pool.query(
"UPDATE customers SET name = $1, address = $2, city = $3, country = $4 WHERE id = $5",
[
req.body.name,
req.body.address,
req.body.city,
req.body.country,
customerId,
]
);
const result = await pool.query("SELECT * FROM customers WHERE id = $1", [
req.params.customerId,
]);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
customerUpdate();
});
//
app.delete("/orders/:orderId", (req, res) => {
const deleteCustomer = async () => {
try {
const checkOrderId = await pool.query(
"SELECT id FROM orders WHERE id = $1",
[req.params.orderId]
);
let orderId =
checkOrderId.rows.length !== 0
? checkOrderId.rows[0].id
: res.json("Order ID does not exist");
const result = await pool.query("DELETE FROM orders WHERE id = $1", [
orderId,
]);

res.json(result);
} catch (err) {
console.log(err);
}
};
deleteCustomer();
});
//
app.delete("/customers/:customerId", (req, res) => {
const deleteCustomer = async () => {
try {
//Checking if a customer exist
const checkCustomerId = await pool.query(
"SELECT id FROM customers WHERE id = $1",
[req.params.customerId]
);
let customerId =
checkCustomerId.rows.length !== 0
? checkCustomerId.rows[0].id
: res.json("Customer ID does not exist");
//Checking if a customer placed an order
const checkOrderId = await pool.query(
"SELECT customer_id FROM orders WHERE customer_id = $1",
[customerId]
);
let orderId =
checkOrderId.rows.length !== 0 ? res.json("Order exists") : customerId;
//Delete customer
const result = await pool.query("DELETE FROM customers WHERE id = $1", [
orderId,
]);

res.json(result);
} catch (err) {
console.log(err);
}
};
deleteCustomer();
});
//
app.get("/customers/:customerId/orders", (req, res) => {
const newOrder = async () => {
try {
//Checking if a customer exist
const checkCustomerId = await pool.query(
"SELECT id FROM customers WHERE id = $1",
[req.params.customerId]
);
let customerId =
checkCustomerId.rows.length !== 0
? checkCustomerId.rows[0].id
: res.json("Customer ID does not exist");
//Checking if a customer placed an order
const checkOrderId = await pool.query(
"SELECT customer_id FROM orders WHERE customer_id = $1",
[customerId]
);
let orderId =
checkOrderId.rows.length !== 0
? customerId
: res.json("Order does not exist");
// Load all the orders for one customer
const result = await pool.query(
"SELECT c.name, o.order_reference, o.order_date, o.product_name, o.supplier_name, o.unit_price, o.quantity FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM suppliers as s FULL OUTER JOIN product_availability as pa ON s.id = pa.supp_id) as pa FULL OUTER JOIN products as p ON pa.prod_id = p.id) as p FULL OUTER JOIN order_items as i ON p.prod_id = i.product_id) as i FULL OUTER JOIN orders as o ON i.order_id = o.id) as o FULL OUTER JOIN customers as c ON o.customer_id = c.id WHERE c.id = $1;",
[orderId]
);
res.json(result.rows);
} catch (err) {
console.log(err);
}
};
newOrder();
});
//
module.exports = app;
8 changes: 6 additions & 2 deletions E-Commerce-API/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "API for the CYF ecommerce application",
"main": "index.js",
"scripts": {
"start": "node app.js",
"start": "nodemon app.js",
"test": "jest"
},
"keywords": [
Expand All @@ -16,7 +16,11 @@
"author": "CYF",
"license": "CC-BY-4-0",
"dependencies": {
"express": "^4.17.1"
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.17.1",
"nodemon": "^3.0.1",
"pg": "^8.11.3"
},
"devDependencies": {
"jest": "^27.2.5",
Expand Down
Loading