diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b36f88aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.vscode/settings.json diff --git a/2-exercises/task.md b/2-exercises/task.md index c48d2286..df898ddc 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -7,6 +7,7 @@ In this homework, you are going to work with an ecommerce database. In this data Below you will find a set of tasks for you to complete to set up a database for an e-commerce app. To submit this homework write the correct commands for each question here: + ```sql @@ -35,16 +36,305 @@ 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 + +SELECT \* FROM customers WHERE country ='United States'; + +id | name | address | city | country +----+--------------+----------------------------+------------------+--------------- +4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States +5 | Edan Higgins | Ap #840-3255 Tincidunt St. | Arles | United States + 2. Retrieve all the customers in ascending name sequence + +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 + 3. Retrieve all the products whose name contains the word `socks` + +SELECT \* FROM products WHERE product_name ILIKE '%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. + +SELECT products.id,products.product_name, product_availability.supp_id,product_availability.unit_price FROM products +cyf_ecommerce-# INNER JOIN product_availability ON product_availability.prod_id=products.id WHERE product_availability.unit_price >100; +id | product_name | supp_id | unit_price +----+----------------+---------+------------ +1 | Mobile Phone X | 4 | 249 +1 | Mobile Phone X | 1 | 299 +(2 rows) + 5. Retrieve the 5 most expensive products + +SELECT products.product_name, product_availability.unit_price FROM products +INNER JOIN product_availability ON product_availability.prod_id=products.id +ORDER BY unit_price DESC LIMIT 5; + +product_name | unit_price +-----------------+------------ +Mobile Phone X | 299 +Mobile Phone X | 249 +Javascript Book | 41 +Javascript Book | 40 +Javascript Book | 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` + +SELECT products.product_name,product_availability.unit_price, suppliers.supplier_name FROM products +INNER JOIN product_availability ON product_availability.prod_id=products.id +INNER 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`. -8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity * unit price). + +SELECT products.product_name,product_availability.unit_price, suppliers.supplier_name, suppliers.country FROM products +INNER JOIN product_availability ON product_availability.prod_id=products.id +INNER JOIN suppliers ON product_availability.supp_id=suppliers.id WHERE suppliers.country='United Kingdom'; + + product_name | unit_price | supplier_name | country +-------------------------+------------+---------------+---------------- + Javascript Book | 39 | Argos | United Kingdom + Super warm socks | 8 | Argos | United Kingdom + Coffee Cup | 4 | Argos | United Kingdom + Tee Shirt Olympic Games | 21 | Argos | United Kingdom + Mobile Phone X | 249 | Sainsburys | United Kingdom + Le Petit Prince | 10 | Sainsburys | United Kingdom + Super warm socks | 10 | Sainsburys | United Kingdom + Coffee Cup | 5 | Sainsburys | United Kingdom + Ball | 15 | Sainsburys | United Kingdom +(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). + + +SELECT orders.customer_id, order_items.order_id, orders.order_reference, orders.order_date, order_items.quantity*product_availability.unit_price as "Total Cost" FROM order_items +INNER JOIN orders ON order_items.order_id=orders.id +INNER JOIN product_availability ON product_availability.prod_id=order_items.product_id WHERE orders.customer_id=1; + + customer_id | order_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` + +SELECT * from orders +INNER JOIN customers ON customers.id=orders.customer_id AND customers.name = 'Hope Crosby'; + + + id | order_date | order_reference | customer_id | id | name | address | city | country +----+------------+-----------------+-------------+----+-------------+----------------------------+-------+---------------- + 4 | 2019-05-24 | ORD004 | 2 | 2 | Hope Crosby | 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`. + +-->>> I just liked to add total column , and order by the total price the bigger amount on top! + +SELECT products.product_name, orders.order_reference, product_availability.unit_price,order_items.quantity, order_items.quantity * product_availability.unit_price AS "Total" FROM products +INNER JOIN product_availability On product_availability.prod_id=products.id +INNER JOIN order_items ON product_availability.prod_id=order_items.product_id +INNER JOIN orders ON order_items.order_id=orders.id WHERE orders.order_reference = 'ORD006' ORDER BY "Total" DESC; + + product_name | order_reference | unit_price | quantity | Total +------------------+-----------------+------------+----------+------- + Javascript Book | ORD006 | 41 | 1 | 41 + Javascript Book | ORD006 | 40 | 1 | 40 + Javascript Book | ORD006 | 39 | 1 | 39 + Super warm socks | ORD006 | 10 | 3 | 30 + Super warm socks | ORD006 | 10 | 3 | 30 + Super warm socks | ORD006 | 8 | 3 | 24 + Super warm socks | ORD006 | 5 | 3 | 15 + Coffee Cup | ORD006 | 5 | 3 | 15 + Coffee Cup | ORD006 | 4 | 3 | 12 + Coffee Cup | ORD006 | 4 | 3 | 12 + Le Petit Prince | ORD006 | 10 | 1 | 10 + Le Petit Prince | ORD006 | 10 | 1 | 10 + Coffee Cup | ORD006 | 3 | 3 | 9 +(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`. + +SELECT customers.name, orders.order_reference, orders.order_date, products.product_name, suppliers.supplier_name, order_items.quantity +FROM customers +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN products ON order_items.product_id=products.id +JOIN suppliers ON suppliers.id=order_items.supplier_id; + + + name | order_reference | order_date | product_name | supplier_name | quantity +--------------------+-----------------+------------+-------------------------+---------------+---------- + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | Taobao | 1 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | Taobao | 5 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | Argos | 4 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | Sainsburys | 1 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | Argos | 10 + Guy Crawford | ORD003 | 2019-07-11 | Ball | Taobao | 2 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | Amazon | 1 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | Argos | 2 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | Amazon | 1 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | Taobao | 3 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | Taobao | 1 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | Sainsburys | 1 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | Sainsburys | 3 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | Argos | 15 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | Amazon | 1 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | Sainsburys | 1 + Edan Higgins | ORD009 | 2019-07-24 | Ball | Sainsburys | 2 + Edan Higgins | ORD010 | 2019-05-10 | Ball | Taobao | 1 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | Amazon | 5 +(19 rows) + + 12. Retrieve the names of all customers who bought a product from a supplier based in China. -13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total. +SELECT customers.name, suppliers.supplier_name,suppliers.country FROM customers +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN suppliers ON order_items.supplier_id=suppliers.id +WHERE suppliers.country='China'; + + name | supplier_name | country +--------------+---------------+--------- + Guy Crawford | Taobao | China + Guy Crawford | Taobao | China + Guy Crawford | Taobao | China + Amber Tran | Taobao | China + Amber Tran | Taobao | China + Edan Higgins | Taobao | China +(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. + +SELECT customers.name, orders.order_reference, orders.order_date, products.product_name, +order_items.quantity, product_availability.unit_price, +(order_items.quantity * product_availability.unit_price) AS "Total Amount" +FROM customers +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN products ON order_items.product_id=products.id +JOIN product_availability ON products.id=product_availability.prod_id; + + + name | order_reference | order_date | product_name | quantity | unit_price | Total Amount +--------------------+-----------------+------------+-------------------------+----------+------------+-------------- + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 20 | 20 + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 18 | 18 + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 21 | 21 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 10 | 50 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 5 | 25 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 8 | 40 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 10 | 50 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 10 | 40 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 5 | 20 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 8 | 32 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 10 | 40 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | 1 | 10 | 10 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | 1 | 10 | 10 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 3 | 30 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 4 | 40 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 4 | 40 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 5 | 50 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 14 | 28 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 15 | 30 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 20 | 40 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | 1 | 299 | 299 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | 1 | 249 | 249 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 40 | 80 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 39 | 78 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 41 | 82 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | 1 | 10 | 10 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 3 | 9 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 4 | 12 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 4 | 12 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 5 | 15 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 40 | 40 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 39 | 39 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 41 | 41 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 10 | 30 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 5 | 15 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 8 | 24 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 10 | 30 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 10 | 150 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 5 | 75 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 8 | 120 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 10 | 150 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 20 | 20 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 18 | 18 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 21 | 21 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | 1 | 299 | 299 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | 1 | 249 | 249 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 14 | 28 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 15 | 30 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 20 | 40 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 14 | 14 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 15 | 15 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 20 | 20 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 10 | 50 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 5 | 25 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 8 | 40 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 10 | 50 +(59 rows) \ No newline at end of file diff --git a/3-projects/server.js b/3-projects/server.js new file mode 100644 index 00000000..b3b86a5b --- /dev/null +++ b/3-projects/server.js @@ -0,0 +1,222 @@ +const { query } = require("express"); +const express = require("express"); +const app = express(); +const port = 5000; + +app.use(express.json()); + +const { Pool } = require("pg"); + +const pool = new Pool({ + user: "postgres", + host: "localhost", + database: "cyf_ecommerce", + password: "Kdagaal123", + port: 5432, +}); + +const productsBySuppliers = `SELECT products.product_name, suppliers.supplier_name, product_availability.unit_price FROM products +JOIN product_availability on product_availability.prod_id=products.id +JOIN suppliers on product_availability.supp_id=suppliers.id +ORDER BY supplier_name`; + +app.get("/cyf-ecommerce-api", (req, res) => { + res.status(200).send(`Wellcom cyf-ecommerce-api + visit these roots; +/customers +/suppliers +/products + `); +}); + +app.get("/cyf-ecommerce-api/customers", (req, res) => { + pool + .query("SELECT * FROM customers") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("/cyf-ecommerce-api/suppliers", (req, res) => { + pool + .query("SELECT * FROM suppliers") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("/cyf-ecommerce-api/products", (req, res) => { + pool + .query("SELECT * FROM products") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("/cyf-ecommerce-api/productsBySuppliers", (req, res) => { + pool + .query(`${productsBySuppliers}`) + .then((data) => res.json(data)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +//------------- FROM HERE IS SQL WEEK 3 ------------------------ + +app.get("/customers/:customerId", (req, res) => { + const customerId = +req.params.customerId; + + pool + .query( + `SELECT c.name, c.address, c.city, c.country FROM customers c WHERE id=${customerId}` + ) + .then((customer) => res.json(customer.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("/product_avaialability", (req, res) => { + pool + .query(`SELECT * FROM product_availability;`) + .then((availableProducts) => res.json(availableProducts.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addNewCustomer", (req, res) => { + pool + .query( + ` + INSERT INTO customers (name,address,city,country) + VALUES ('New name3','New address3', 'XYZ City3','ABC Country3'); + ` + ) + .then(() => res.send("A new customer is added")) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addNewProduct", (req, res) => { + pool + .query(`INSERT INTO products (product_name) VALUES ('Book');`) + .then(() => res.send("New row is added")) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addProductAvailability", (req, res) => { + pool + .query( + `INSERT INTO product_availability (prod_id,supp_id,unit_price) + VALUES (13,2,15);` + ) + .then(() => + res.status(200).json("new record is added to product_availability table") + ) + .catch((err) => { + console.error(err); + res.send(err); + }); +}); + +app.post("/customers/:customerId/AddNewOrders", (req, res) => { + const custID = +req.params.customerId; + const query = `INSERT INTO orders (order_date,order_reference,customer_id) + VALUES ('2023-01-10','0RD012',${Number(custID)})`; + + pool + .query(query) + .then(() => res.send("Anew order is added for customer " + custID)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + +//Look this put block and it's fucntionality thing about update and put methods +app.put("/updateCustomerRecord/:customerId", (req, res) => { + const custID = +req.params.customerId; + console.log(custID, `${Number(custID)}`); + pool + .query( + ` +UPDATE customers +SET name='Ahmed',address='10 AliClose M82UY',city='Manchester',country='Canada' +WHERE id ='${custID}' +` + ) + .then(() => res.send(`customer id : ${custID} has been updated`)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + +app.delete("/deleteOrder/:orderId", (req, res) => { + const orderId = +req.params.orderId; + pool + .query(`DELETE FROM orders WHERE orders.id='${Number(orderId)}'`) + .then(() => { + res.send("order number " + orderId + " has been deleted").catch((err) => { + console.error(err); + res.send(err); + }); + }); +}); + + + +app.delete("/deleteCustomer/:customerId", (req, res) => { + const customerId = +req.params.customerId; + + pool + .query( + `Delete FROM customers + JOIN orders ON orders.customer_id=customers.id + WHERE customers.id='${customerId} AND orders.customer_id='${customerId}'` + ) + .then((customer) => res.json(customer.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get(`/getAllCustomerOrders/:customerId/orders`, (req, res) => { + const custID = +req.params.customerId; + + pool + .query( + `SELECT c.name, o.order_reference,o.order_date, p.product_name, oi.quantity,s.supplier_name, pa.unit_price from customers c +JOIN orders o ON o.customer_id=c.id +JOIN order_items oi on oi.order_id=o.id +JOIN products p ON p.id=oi.product_id +JOIN suppliers s ON s.id=oi.supplier_id +JOIN product_availability pa ON pa.prod_id=oi.product_id +WHERE c.id = '${custID}'`) + .then((customerOrders) => res.json(customerOrders.rows)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + +app.listen(port, () => { + console.log(`server is running ${port}`); +}); diff --git a/3-projects/taskSQL-W3.md b/3-projects/taskSQL-W3.md new file mode 100644 index 00000000..1aef922c --- /dev/null +++ b/3-projects/taskSQL-W3.md @@ -0,0 +1,33 @@ +# Homework + +## Submission + +You can continue working on the code from last week for this weeks task. + +To submit you should open a pull request with all of your code in this folder. + +## Task + +In the following homework, you will create new API endpoints in the NodeJS application `cyf-ecommerce-api` that you created for last week's homework for the Database 2 class. + +- If you don't have it already, add a new GET endpoint `/products` to load all the product names along with their prices and supplier names. + +- Update the previous GET endpoint `/products` to filter the list of products by name using a query parameter, for example `/products?name=Cup`. This endpoint should still work even if you don't use the `name` query parameter! + +- Add a new GET endpoint `/customers/:customerId` to load a single customer by ID. ---------->>>>>>>>> DONE + +- Add a new POST endpoint `/customers` to create a new customer with name, address, city and country. ---------->>>>>>>>> DONE + +- Add a new POST endpoint `/products` to create a new product. + +- Add a new POST endpoint `/availability` to create a new product availability (with a price and a supplier id). Check that the price is a positive integer and that both the product and supplier ID's exist in the database, otherwise return an error. + +- Add a new POST endpoint `/customers/:customerId/orders` to create a new order (including an order date, and an order reference) for a customer. Check that the customerId corresponds to an existing customer or return an error. + +- Add a new PUT endpoint `/customers/:customerId` to update an existing customer (name, address, city and country). + +- Add a new DELETE endpoint `/orders/:orderId` to delete an existing order along with all the associated order items. + +- Add a new DELETE endpoint `/customers/:customerId` to delete an existing customer only if this customer doesn't have orders. + +- Add a new GET endpoint `/customers/:customerId/orders` to load all the orders along with the items in the orders of a specific customer. Especially, the following information should be returned: order references, order dates, product names, unit prices, suppliers and quantities.