From a169c5e31ee4f702863345ba66507640950c9737 Mon Sep 17 00:00:00 2001 From: Jihad7898 <107761291+Barryeasy90@users.noreply.github.com> Date: Mon, 13 Feb 2023 02:56:16 +0000 Subject: [PATCH 1/5] 2-exercises done SQL queries done --- 2-exercises/task.md | 162 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 1 deletion(-) diff --git a/2-exercises/task.md b/2-exercises/task.md index c48d2286..3732415b 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -35,16 +35,176 @@ Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a pie Once you understand the database that you are going to work with, solve the following challenge by writing SQL queries using everything you learned about SQL: 1. Retrieve all the customers' names and addresses who live in the United States +```sql +SELECT name, address FROM customers WHERE country = 'United States'; + name | address +--------------+---------------------------- + Amber Tran | 6967 Ac Road + Edan Higgins | Ap #840-3255 Tincidunt St. +(2 rows) +``` 2. Retrieve all the customers in ascending name sequence +```sql +SELECT * FROM customers ORDER BY name; + id | name | address | city | country +----+--------------------+-----------------------------+------------------+---------------- + 4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States + 3 | Britanney Kirkland | P.O. Box 577, 5601 Sem, St. | Little Rock | United Kingdom + 5 | Edan Higgins | Ap #840-3255 Tincidunt St. | Arles | United States + 1 | Guy Crawford | 770-2839 Ligula Road | Paris | France + 2 | Hope Crosby | P.O. Box 276, 4976 Sit Rd. | Steyr | United Kingdom + 6 | Quintessa Austin | 597-2737 Nunc Rd. | Saint-Marc | United Kingdom +(6 rows) +``` 3. Retrieve all the products whose name contains the word `socks` +```sql +SELECT * FROM products WHERE product_name like '%socks%'; + id | product_name +----+------------------ + 4 | Super warm socks +(1 row) +``` 4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. +```sql +SELECT p.id, p.product_name, pa.unit_price, pa.supp_id FROM products p JOIN product_availability pa ON (p.id = pa.prod_id) WHERE pa.unit_price > 100; + id | product_name | unit_price | supp_id +----+----------------+------------+--------- + 1 | Mobile Phone X | 249 | 4 + 1 | Mobile Phone X | 299 | 1 +(2 rows) +``` 5. Retrieve the 5 most expensive products +```sql +SELECT * FROM product_availability ORDER BY unit_price DESC limit 5; + prod_id | supp_id | unit_price +---------+---------+------------ + 1 | 1 | 299 + 1 | 4 | 249 + 2 | 2 | 41 + 2 | 1 | 40 + 2 | 3 | 39 +(5 rows) +``` + 6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name` +```sql +SELECT product_name, unit_price, supplier_name FROM products JOIN product_availability ON (products.id = product_availability.prod_id) JOIN suppliers ON (product_availability.supp_id = suppliers.id); + product_name | unit_price | supplier_name +-------------------------+------------+--------------- + Mobile Phone X | 249 | Sainsburys + Mobile Phone X | 299 | Amazon + Javascript Book | 41 | Taobao + Javascript Book | 39 | Argos + Javascript Book | 40 | Amazon + Le Petit Prince | 10 | Sainsburys + Le Petit Prince | 10 | Amazon + Super warm socks | 10 | Sainsburys + Super warm socks | 8 | Argos + Super warm socks | 5 | Taobao + Super warm socks | 10 | Amazon + Coffee Cup | 5 | Sainsburys + Coffee Cup | 4 | Argos + Coffee Cup | 4 | Taobao + Coffee Cup | 3 | Amazon + Ball | 20 | Taobao + Ball | 15 | Sainsburys + Ball | 14 | Amazon + Tee Shirt Olympic Games | 21 | Argos + Tee Shirt Olympic Games | 18 | Taobao + Tee Shirt Olympic Games | 20 | Amazon +(21 rows) +``` + 7. Retrieve all the products sold by suppliers based in the United Kingdom. The result should only contain the columns `product_name` and `supplier_name`. +```sql +SELECT p.product_name, s.supplier_name FROM products p JOIN product_availability pa ON (p.id = pa.prod_id) JOIN suppliers s ON (s.id = pa.supp_id) WHERE s.country = 'United Kingdom'; + product_name | supplier_name +-------------------------+--------------- + Javascript Book | Argos + Super warm socks | Argos + Coffee Cup | Argos + Tee Shirt Olympic Games | Argos + Mobile Phone X | Sainsburys + Le Petit Prince | Sainsburys + Super warm socks | Sainsburys + Coffee Cup | Sainsburys + Ball | Sainsburys +(9 rows) +``` 8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity * unit price). +```sql +SELECT o.customer_id, o.id AS "Oder ID", o.order_reference, o.order_date, oi.quantity*pa.unit_price AS "total cost" FROM product_availability pa JOIN order_items oi ON (pa.prod_id = oi.product_id) JOIN orders o ON (o.id = oi.order_id) WHERE o.customer_id = 1; + customer_id | Oder ID | order_reference | order_date | total cost +-------------+---------+-----------------+------------+------------ + 1 | 1 | ORD001 | 2019-06-01 | 20 + 1 | 1 | ORD001 | 2019-06-01 | 18 + 1 | 1 | ORD001 | 2019-06-01 | 21 + 1 | 1 | ORD001 | 2019-06-01 | 50 + 1 | 1 | ORD001 | 2019-06-01 | 25 + 1 | 1 | ORD001 | 2019-06-01 | 40 + 1 | 1 | ORD001 | 2019-06-01 | 50 + 1 | 2 | ORD002 | 2019-07-15 | 40 + 1 | 2 | ORD002 | 2019-07-15 | 20 + 1 | 2 | ORD002 | 2019-07-15 | 32 + 1 | 2 | ORD002 | 2019-07-15 | 40 + 1 | 2 | ORD002 | 2019-07-15 | 10 + 1 | 2 | ORD002 | 2019-07-15 | 10 + 1 | 3 | ORD003 | 2019-07-11 | 30 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 50 + 1 | 3 | ORD003 | 2019-07-11 | 28 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 30 +(20 rows) + +``` 9. Retrieve all orders, including order items, from customer named `Hope Crosby` +```sql +SELECT c.name, o.id, oi.id, oi.quantity, c.address, c.city, c.country FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) WHERE c.name = 'Hope Crosby'; + name | id | id | quantity | address | city | country +-------------+----+----+----------+----------------------------+-------+---------------- + Hope Crosby | 4 | 7 | 1 | P.O. Box 276, 4976 Sit Rd. | Steyr | United Kingdom +(1 row) +``` 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. +```sql +SELECT p.product_name, pa.unit_price, oi.quantity FROM products p JOIN product_availability pa ON (p.id = pa.prod_id) JOIN order_items oi ON (oi.product_id = pa.prod_id) JOIN orders o ON (o.id = oi.order_id) WHERE o.order_reference = 'ORD006'; + product_name | unit_price | quantity +------------------+------------+---------- + Coffee Cup | 3 | 3 + Coffee Cup | 4 | 3 + Coffee Cup | 4 | 3 + Coffee Cup | 5 | 3 + Javascript Book | 40 | 1 + Javascript Book | 41 | 1 + Javascript Book | 39 | 1 + Le Petit Prince | 10 | 1 + Le Petit Prince | 10 | 1 + Super warm socks | 10 | 3 + Super warm socks | 5 | 3 + Super warm socks | 8 | 3 + Super warm socks | 10 | 3 +(13 rows) +``` 11. Retrieve all the products with their supplier for all orders of all customers. The result should only contain the columns `name` (from customer), `order_reference`, `order_date`, `product_name`, `supplier_name` and `quantity`. +```sql +SELECT c.name, o.order_reference, o.order_date, p.product_name, s.supplier_name, oi.quantity FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN suppliers s ON (oi.supplier_id = s.id) JOIN product_availability pa ON (s.id = pa.supp_id) JOIN products p ON (p.id = pa.prod_id); +``` 12. Retrieve the names of all customers who bought a product from a supplier based in China. +```sql +SELECT c.name FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN suppliers s ON (oi.supplier_id = s.id) WHERE s.country = 'China'; + name +-------------- + Guy Crawford + Guy Crawford + Guy Crawford + Amber Tran + Amber Tran + Edan Higgins +(6 rows) +``` 13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total. - +```sql +SELECT c.name, o.order_reference, o.order_date, oi.quantity * pa.unit_price AS TOTAL_AMOUNT FROM customers c JOIN orders o ON ( c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN product_availability pa ON (oi.product_id = pa.prod_id) ORDER BY TOTAL_AMOUNT DESC; +``` From d22332d61da7828fc2bb55348656e954f9c242f3 Mon Sep 17 00:00:00 2001 From: Jihad7898 <107761291+Barryeasy90@users.noreply.github.com> Date: Mon, 13 Feb 2023 18:48:25 +0000 Subject: [PATCH 2/5] I have completed the project The server is up and running --- 3-projects/cyf_ecommerce_api | 1 + 1 file changed, 1 insertion(+) create mode 160000 3-projects/cyf_ecommerce_api diff --git a/3-projects/cyf_ecommerce_api b/3-projects/cyf_ecommerce_api new file mode 160000 index 00000000..0ff5cf4f --- /dev/null +++ b/3-projects/cyf_ecommerce_api @@ -0,0 +1 @@ +Subproject commit 0ff5cf4f7c042b6e080cee244020923b1b3bf8d9 From f8797ecb06a474d394edae0c69bb674645c0905f Mon Sep 17 00:00:00 2001 From: Jihad7898 <107761291+Barryeasy90@users.noreply.github.com> Date: Mon, 20 Feb 2023 01:18:16 +0000 Subject: [PATCH 3/5] Week3 sommes end points added --- 3-projects/severs.js | 92 ++++++++++++++++++++++++++++++++ 3-projects/{task.md => task1.md} | 0 2 files changed, 92 insertions(+) create mode 100644 3-projects/severs.js rename 3-projects/{task.md => task1.md} (100%) diff --git a/3-projects/severs.js b/3-projects/severs.js new file mode 100644 index 00000000..e57b0039 --- /dev/null +++ b/3-projects/severs.js @@ -0,0 +1,92 @@ +const express = require("express") +const app = express(); + +const bodyParser = require("body-parser"); +app.use(bodyParser.json()); + +const { Pool } = require('pg'); + +const db = new Pool({ + user: 'postgres', + host: 'localhost', + database: 'cyf_ecommerce', + password: 'free2way', + port: 5432 +}) + + + +app.get("/", (req, res) => { + res.send(" Welcome to my ecommerce Database "); +}) + +// +app.get("/customers", (req, res) => { + db.query("select * from customers", (err, result) => { + res.json(result.rows); + }); +}); + +app.get("/suppliers", (req, res) => { + db.query('SELECT * FROM suppliers', (error, result) => { + res.json(result.rows); + }) +}) +app.get("/products", (req, res) => { + db.query('SELECT product_name, unit_price, supplier_name FROM products JOIN product_availability ON (products.id = product_availability.prod_id) JOIN suppliers ON (product_availability.supp_id = suppliers.id)', (error, result) => { + res.json(result.rows) + }) +}) +app.get("/products?name=Cup", (req, res) => { + //Need some clarification +}) + +app.get("/customers/:id", (req, res) => { + const custId = req.params.id + db.query('SELECT * FROM customers WHERE id = $1', [custId], (err, result) => { + res.json(result.rows) + }) +}) + +app.post("/customers", (req, res) => { + const newName = req.body.name + const newAddress = req.body.address + const newCity = req.body.city + const newCountry = req.body.country + + const query = "INSERT INTO customers (name,address, city, country)" + + "VALUES($1, $2, $3, $4)"; + db + .query(query, [newName, newAddress, newCity, newCountry], (err, result) => { + res.json(result.rows) + }) +}); +app.post("/availability", (req, res) => { + //Have to be checked +}) +app.post("`/customers/:customerId/orders`", (req, res) => { + //Have to be checked +}) + +app.put("/customers/:id", (req, res) => { + const custId = req.params.id; + const newName = req.body.name; + const newAddress = req.body.address; + const newCity = req.body.city; + const newCountry = req.body.country; + + db.query('UPDATE customers SET name = $4, address = $4, city = $2, country = $3,WHERE id=$1', + [newName, newAddress, newCity, newCountry,]) + .then(() => res.send(`Customer ${custId} updated!`)) + .catch((err) => { + res.status(500).json({ error: err }) + }); +}) + + + + +app.listen(3000, function () { + console.log("Server is listening on port 3000. ready to accept requrests!") + +}) \ No newline at end of file diff --git a/3-projects/task.md b/3-projects/task1.md similarity index 100% rename from 3-projects/task.md rename to 3-projects/task1.md From 82c4bc14e51128f5a5d8d585c22682b7a91b9775 Mon Sep 17 00:00:00 2001 From: Jihad7898 <107761291+Barryeasy90@users.noreply.github.com> Date: Tue, 21 Feb 2023 00:22:02 +0000 Subject: [PATCH 4/5] All end points done I still have some end point to cross check before commiting --- 3-projects/severs.js | 92 -------------------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 3-projects/severs.js diff --git a/3-projects/severs.js b/3-projects/severs.js deleted file mode 100644 index e57b0039..00000000 --- a/3-projects/severs.js +++ /dev/null @@ -1,92 +0,0 @@ -const express = require("express") -const app = express(); - -const bodyParser = require("body-parser"); -app.use(bodyParser.json()); - -const { Pool } = require('pg'); - -const db = new Pool({ - user: 'postgres', - host: 'localhost', - database: 'cyf_ecommerce', - password: 'free2way', - port: 5432 -}) - - - -app.get("/", (req, res) => { - res.send(" Welcome to my ecommerce Database "); -}) - -// -app.get("/customers", (req, res) => { - db.query("select * from customers", (err, result) => { - res.json(result.rows); - }); -}); - -app.get("/suppliers", (req, res) => { - db.query('SELECT * FROM suppliers', (error, result) => { - res.json(result.rows); - }) -}) -app.get("/products", (req, res) => { - db.query('SELECT product_name, unit_price, supplier_name FROM products JOIN product_availability ON (products.id = product_availability.prod_id) JOIN suppliers ON (product_availability.supp_id = suppliers.id)', (error, result) => { - res.json(result.rows) - }) -}) -app.get("/products?name=Cup", (req, res) => { - //Need some clarification -}) - -app.get("/customers/:id", (req, res) => { - const custId = req.params.id - db.query('SELECT * FROM customers WHERE id = $1', [custId], (err, result) => { - res.json(result.rows) - }) -}) - -app.post("/customers", (req, res) => { - const newName = req.body.name - const newAddress = req.body.address - const newCity = req.body.city - const newCountry = req.body.country - - const query = "INSERT INTO customers (name,address, city, country)" + - "VALUES($1, $2, $3, $4)"; - db - .query(query, [newName, newAddress, newCity, newCountry], (err, result) => { - res.json(result.rows) - }) -}); -app.post("/availability", (req, res) => { - //Have to be checked -}) -app.post("`/customers/:customerId/orders`", (req, res) => { - //Have to be checked -}) - -app.put("/customers/:id", (req, res) => { - const custId = req.params.id; - const newName = req.body.name; - const newAddress = req.body.address; - const newCity = req.body.city; - const newCountry = req.body.country; - - db.query('UPDATE customers SET name = $4, address = $4, city = $2, country = $3,WHERE id=$1', - [newName, newAddress, newCity, newCountry,]) - .then(() => res.send(`Customer ${custId} updated!`)) - .catch((err) => { - res.status(500).json({ error: err }) - }); -}) - - - - -app.listen(3000, function () { - console.log("Server is listening on port 3000. ready to accept requrests!") - -}) \ No newline at end of file From b7f3723a82d173c5abb91016a4c3554a8cf55d0f Mon Sep 17 00:00:00 2001 From: Jihad7898 <107761291+Barryeasy90@users.noreply.github.com> Date: Tue, 21 Feb 2023 01:27:51 +0000 Subject: [PATCH 5/5] The server is up and runing now, files commited --- 3-projects/cyf_ecommerce_api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3-projects/cyf_ecommerce_api b/3-projects/cyf_ecommerce_api index 0ff5cf4f..b5c07bdb 160000 --- a/3-projects/cyf_ecommerce_api +++ b/3-projects/cyf_ecommerce_api @@ -1 +1 @@ -Subproject commit 0ff5cf4f7c042b6e080cee244020923b1b3bf8d9 +Subproject commit b5c07bdb855013b63509761222b7809a73ba4949