Skip to content
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: 18 additions & 10 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 amount 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,51 +68,52 @@ INSERT YOUR QUERY HERE
**You:** Then here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT amount FROM spends WHERE (LOWER(description) LIKE '%fees%' OR LOWER(description) LIKE '%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 s.id, ea.expense_area FROM spends s INNER JOIN expense_areas ea ON(s.expense_area_id = ea.id ) WHERE ea.expense_area = 'Be
tter 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 to_char(date,'YYYY-MM') AS month FROM spends GROUP BY month;
```

**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 sum(sp.amount),sup.id FROM spends sp INNER JOIN suppliers sup ON (sup.id = sp.supplier_id) GROUP BY sup.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 sum(sp.amount), sup.supplier FROM spends sp INNER JOIN suppliers sup ON (sup.id = sp.supplier_id) GROUP BY sup.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)?

**You:** I think you can use the BETWEEN clause to get the total amount spent on a range of dates, just like we used earlier.

```SELECT sum(amount) as total_per_month, date FROM spends WHERE date BETWEEN '2021-03-01' AND '2021-04-01' GROUP BY date;```
**Claire:** But I think I _only_ want those two dates, not a range, or all the days in between.

**You:** Then you need an extra clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT sum(amount), date FROM spends WHERE date ='2021-03-01' OR date='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,8 +125,15 @@ 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 spends (expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount)
VALUES (7,
18,
16,
'2021-08-19',
38104091,
'3780119655',
'Computer Hardware Dell',
32000);
```

**Claire:** Great, that's everything we need. Thanks for your help.
Expand Down
6 changes: 6 additions & 0 deletions E-Commerce-API/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=localhost
DB_PORT=5432
DB_USER=adniya
DB_PASSWORD=ad6057nya510
DB_NAME=cyf-ecommerce
PORT=3000
105 changes: 99 additions & 6 deletions E-Commerce-API/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,101 @@
const express = require("express");
import express from "express";
import 'dotenv/config';
import pg from "pg"
const { Pool } = pg;
const app = express();
// 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.use(express.json());

module.exports = app;
const db = new Pool({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});

app.get('/products', async (req, res) => {
try {
const result = await db.query('select p.product_name, pa.unit_price ,s.supplier_name from products p inner join product_availability pa on (p.id = pa.prod_id) inner join suppliers s on (s.id =pa.supp_id);');
res.send(result.rows);
} catch (error) {
res.send(500);
}
})

app.get('/products/:name', async (req, res) => {
console.log(req.params.name);
try {
const name = req.params.name;
const result = await db.query(`SELECT product_name FROM products WHERE product_name ILIKE '%$1%';`, [name]);
res.status(200).send(result.rows);
} catch (error) {
res.status(400).send(`failed to get searched name`);
}
});

app.get('/customers/:customerId', async (req, res) => {
try {
const result = await db.query('select * from customers where id= $1;', [req.params.customerId])
res.status(200).send(result.rows);
} catch (error) {
res.status(400).send('Customer not found!')
}
})

app.post('/customers', async (req, res) => {
try {
await db.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]);
res.status(201).send('Added the customer successfully');
} catch (error) {
res.status(500).send(`failed to add new customer! ${error}`)
}
})

app.post('/products', async (req, res) => {
try {
await db.query(`INSERT INTO products ( product_name)
VALUES($1)`, [req.body.product_name]);
res.status(201).send('Added the new product successfully');
} catch (error) {
res.status(500).send(`failed to add new products! ${error}`);
}
})

app.post('/availability', async (req, res) => {
try {
if (req.body.unit_price > 0) {
await db.query(`INSERT INTO product_availability (prod_id, supp_id, unit_price)
VALUES((select id from products where id= $1 ), $2, $3)`, [req.body.prod_id, req.body.supp_id, req.body.unit_price]);
res.status(201).send('Price cant be below zero!');
} else {
res.status(404).send('Added the new entry in product availability successfully');
}
} catch (error) {
res.status(500).send(`failed to add new entry in product availability! ${error}`)
}
})

app.post('/customers/:customerId/orders', async (req, res) => {
const result = db.query(`select id from customers where id= $1`, [req.params.customerId]);

if (result !== null) {
try {
await db.query(`INSERT INTO orders (order_date, order_reference, customer_id)
VALUES($1, $2, (select id from customers where id= $3 ))`, [req.body.order_date, req.body.order_reference, req.params.customerId]);
res.status(201).send('Successfully added the order!');
} catch (error) {
res.status(500).send('Failed to add order!')
}
} else {
res.status(400).send('Customer ID is not Valid')
}
})

const port = process.env.PORT;

app.listen(port, () => {
console.log(`Server listening to port ${port}!`);
})

export default app;
3 changes: 3 additions & 0 deletions E-Commerce-API/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
transform: {},
};
Loading