From fa02042a6b82eae2fd89b5ae3f9a018a5bb5a886 Mon Sep 17 00:00:00 2001 From: Iman Feisali Date: Fri, 18 Sep 2020 15:37:13 +0100 Subject: [PATCH 1/3] week 1 done --- week-1/mandatory/2-classes-db/task.md | 67 ++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index 735de6ae..d5abcc2c 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -2,7 +2,7 @@ ## Submission -Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these. +Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these. To submit this homework write the correct commands for each question here: @@ -18,19 +18,82 @@ When you have finished all of the questions - open a pull request with your answ If you haven't completed all the exercises from this lesson then do that first. ### Tasks + 1. Which rooms have a rate of more than 100.00? + +```sql +select * from rooms where rate > 100.00; + +``` + 2. List the reservations that have a checkin date this month and are for more than three nights. + +```sql +select * from reservations where checkin_date between '2020-09-01' and '2020-09-30' and (checkout_date - checkin_date) > 3; + +``` + 3. List all customers from cities that begin with the letter 'M'. +```sql +select * from customers where city like 'M%'; + +``` + Insert some new data into the room_types and rooms tables, querying after each stage to check the data, as follows: 4. Make a new room type of PENTHOUSE with a default rate of £185.00 + +```sql +insert into room_types (room_type, def_rate) values ('PENTHOUSE', 150.00);/ + +``` + 5. Add new rooms, 501 and 502 as room type PENTHOUSE and set the room rate of each to the default value (as in the new room type). + +```sql +insert into rooms(room_no, room_type, rate) values (501, 'PENTHOUSE', 150.00), +(502, 'PENTHOUSE', 150.00); + +``` + 6. Add a new room 503 as a PREMIER PLUS type similar to the other PREMIER PLUS rooms in the hotel but with a room rate of 143.00 to reflect its improved views over the city. +```sql +insert into rooms(room_no, room_type, rate) values (503, 'PREMIER PLUS', 143.00); + +``` + Using what you can learn about aggregate functions in the w3schools SQL classes (or other providers), try: 7. The hotel manager wishes to know how many rooms were occupied any time during the previous month - find that information. + +```sql + select count (distinct room_no) from reservations where checkin_date >= '2020-08-01' and checkout_date <= '2020-08-31'; + +``` + 8. Get the total number of nights that customers stayed in rooms on the second floor (rooms 201 - 299). + +```sql +select sum (checkout_date - checkin_date) as total_nights from reservations +where room_no between 201 and 299; + +``` + 9. How many invoices are for more than £300.00 and what is their grand total and average amount? -10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**) + +```sql +select count(total), sum(total),round(avg(total)) from invoices where total > 300.00; + +``` + +10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**) + +```sql +select floor(room_no/100) as floor, +sum (checkout_date - checkin_date) as nights +from reservations +group by floor order by floor; + +``` From 792e9ab6ed5ab3154d00487d20e8ad99af5a7025 Mon Sep 17 00:00:00 2001 From: Iman Feisali Date: Thu, 24 Sep 2020 21:27:04 +0100 Subject: [PATCH 2/3] week 2 homework done --- week-2/mandatory/2-ecommerce-db/task.md | 98 +++- week-2/mandatory/3-api/cyf_ecommerce.sql | 124 +++++ .../mandatory/3-api/cyf_ecommerce/.gitignore | 1 + .../3-api/cyf_ecommerce/package-lock.json | 485 ++++++++++++++++++ .../3-api/cyf_ecommerce/package.json | 15 + .../mandatory/3-api/cyf_ecommerce/server.js | 43 ++ 6 files changed, 764 insertions(+), 2 deletions(-) create mode 100644 week-2/mandatory/3-api/cyf_ecommerce.sql create mode 100644 week-2/mandatory/3-api/cyf_ecommerce/.gitignore create mode 100644 week-2/mandatory/3-api/cyf_ecommerce/package-lock.json create mode 100644 week-2/mandatory/3-api/cyf_ecommerce/package.json create mode 100644 week-2/mandatory/3-api/cyf_ecommerce/server.js diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index c48d2286..bcd89192 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/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,109 @@ 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'; +``` + 2. Retrieve all the customers in ascending name sequence + +```sql +select * from customers order by name; +``` + 3. Retrieve all the products whose name contains the word `socks` + +```sql +select * from products where product_name like '%socks%'; +``` + 4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. + +```sql +select pa.prod_id, p.product_name, pa.unit_price, pa.supp_id +from product_availability pa join products p on (pa.prod_id=p.id) +where unit_price > 100; +``` + 5. Retrieve the 5 most expensive products + +```sql +select pa.prod_id, p.product_name, pa.unit_price, pa.supp_id +from product_availability pa join products p on (pa.prod_id=p.id) +order by unit_price desc limit 5; +``` + 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 p.product_name, pa.unit_price, s.supplier_name +from product_availability pa join products p on (pa.prod_id=p.id) +join suppliers s on (s.id = pa.supp_id); +``` + 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). + +```sql + select p.product_name, s.supplier_name + from product_availability pa join suppliers s on (s.id = pa.supp_id) + join products p on (p.id = pa.prod_id) + where country = 'United Kingdom'; +``` + +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.id, o.order_reference, o.order_date, oi.quantity, oi.quantity*pa.unit_price as total_cost +from orders o join order_items oi on(o.id = oi.order_id) +join products p on(p.id = oi.product_id) +join product_availability pa on(pa.prod_id = p.id) +where customer_id = 1; +``` + 9. Retrieve all orders, including order items, from customer named `Hope Crosby` + +```sql + +``` + 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 orders o join order_items oi on(o.id = oi.order_id) +join products p on(p.id = oi.product_id) +join product_availability pa on(pa.prod_id = p.id) +where order_reference = 'ORD006'; +``` + 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_item oi on(oi.order_id = o.id) +join suppliers s on(s.id = oi.supplier_id) +join product_availability pa on (pa.supp_id = s.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. -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 distinct name from customers c join orders o on(c.id = o.customer_id) +join order_items oi on(oi.order_id = o.id) +join suppliers s on(s.id = oi.supplier_id) +where s.country = 'China'; +``` + +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_cost +from orders o join customers c on(o.customer_id = c.id) +join order_items oi on (o.id = oi.order_id) +join products p on (p.id = oi.product_id) +join product_availability pa on(pa.prod_id = p.id) +order by total_cost desc; +``` diff --git a/week-2/mandatory/3-api/cyf_ecommerce.sql b/week-2/mandatory/3-api/cyf_ecommerce.sql new file mode 100644 index 00000000..7b335282 --- /dev/null +++ b/week-2/mandatory/3-api/cyf_ecommerce.sql @@ -0,0 +1,124 @@ +drop table if exists order_items; +drop table if exists orders cascade; +DROP TABLE IF EXISTS product_availability cascade; +drop table if exists customers cascade; +drop table if exists products cascade; +drop table if exists suppliers cascade; + +CREATE TABLE customers ( + id SERIAL PRIMARY KEY, + name VARCHAR(50) NOT NULL, + address VARCHAR(120), + city VARCHAR(30), + country VARCHAR(20) +); + +CREATE TABLE suppliers ( + id SERIAL PRIMARY KEY, + supplier_name VARCHAR(100) NOT NULL, + country VARCHAR(20) NOT NULL +); + +CREATE TABLE products ( + id SERIAL PRIMARY KEY, + product_name VARCHAR(100) NOT NULL +); + +create table product_availability ( + prod_id integer references products(id), + supp_id integer references suppliers(id), + unit_price integer not null, + primary key (prod_id, supp_id) +); + +CREATE TABLE orders ( + id SERIAL PRIMARY KEY, + order_date DATE NOT NULL, + order_reference VARCHAR(10) NOT NULL, + customer_id INT REFERENCES customers(id) +); + +CREATE TABLE order_items ( + id SERIAL PRIMARY KEY, + order_id INT NOT NULL REFERENCES orders(id), + product_id INT NOT NULL, + supplier_id INT NOT NULL, + quantity INT NOT NULL, + FOREIGN KEY (product_id, supplier_id) + REFERENCES product_availability (prod_id, supp_id) +); + +INSERT INTO customers (name, address, city, country) VALUES ('Guy Crawford','770-2839 Ligula Road','Paris','France'); +INSERT INTO customers (name, address, city, country) VALUES ('Hope Crosby','P.O. Box 276, 4976 Sit Rd.','Steyr','United Kingdom'); +INSERT INTO customers (name, address, city, country) VALUES ('Britanney Kirkland','P.O. Box 577, 5601 Sem, St.','Little Rock','United Kingdom'); +INSERT INTO customers (name, address, city, country) VALUES ('Amber Tran','6967 Ac Road','Villafranca Asti','United States'); +INSERT INTO customers (name, address, city, country) VALUES ('Edan Higgins','Ap #840-3255 Tincidunt St.','Arles','United States'); +INSERT INTO customers (name, address, city, country) VALUES ('Quintessa Austin','597-2737 Nunc Rd.','Saint-Marc','United Kingdom'); + +INSERT INTO suppliers (supplier_name, country) VALUES ('Amazon', 'United States'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Taobao', 'China'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Argos', 'United Kingdom'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Sainsburys', 'United Kingdom'); + + +INSERT INTO products (product_name) VALUES ('Mobile Phone X'); +INSERT INTO products (product_name) VALUES ('Javascript Book'); +INSERT INTO products (product_name) VALUES ('Le Petit Prince'); +INSERT INTO products (product_name) VALUES ('Super warm socks'); +INSERT INTO products (product_name) VALUES ('Coffee Cup'); +INSERT INTO products (product_name) VALUES ('Ball'); +INSERT INTO products (product_name) VALUES ('Tee Shirt Olympic Games'); + +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 4, 249); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 1, 299); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 2, 41); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 3, 39); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 1, 40); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 4, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 1, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 4, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 3, 8); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 2, 5); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 1, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 4, 5); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 3, 4); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 2, 4); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 1, 3); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 2, 20); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 4, 15); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 1, 14); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 3, 21); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 2, 18); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 1, 20); + +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-06-01', 'ORD001', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-15', 'ORD002', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-11', 'ORD003', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-24', 'ORD004', 2); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-30', 'ORD005', 3); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-05', 'ORD006', 4); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-04-05', 'ORD007', 4); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-23', 'ORD008', 5); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-24', 'ORD009', 5); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-10', 'ORD010', 5); + +INSERT INTO order_items VALUES (1, 1, 7, 2, 1); +INSERT INTO order_items VALUES (2, 1, 4, 2, 5); +INSERT INTO order_items VALUES (3, 2, 4, 3, 4); +INSERT INTO order_items VALUES (4, 2, 3, 4, 1); +INSERT INTO order_items VALUES (5, 3, 5, 3, 10); +INSERT INTO order_items VALUES (6, 3, 6, 2, 2); +INSERT INTO order_items VALUES (7, 4, 1, 1, 1); +INSERT INTO order_items VALUES (8, 5, 2, 3, 2); +INSERT INTO order_items VALUES (9, 5, 3, 1, 1); +INSERT INTO order_items VALUES (10, 6, 5, 2, 3); +INSERT INTO order_items VALUES (11, 6, 2, 2, 1); +INSERT INTO order_items VALUES (12, 6, 3, 4, 1); +INSERT INTO order_items VALUES (13, 6, 4, 4, 3); +INSERT INTO order_items VALUES (14, 7, 4, 3, 15); +INSERT INTO order_items VALUES (15, 8, 7, 1, 1); +INSERT INTO order_items VALUES (16, 8, 1, 4, 1); +INSERT INTO order_items VALUES (17, 9, 6, 4, 2); +INSERT INTO order_items VALUES (18, 10, 6, 2, 1); +INSERT INTO order_items VALUES (19, 10, 4, 1, 5); + diff --git a/week-2/mandatory/3-api/cyf_ecommerce/.gitignore b/week-2/mandatory/3-api/cyf_ecommerce/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/week-2/mandatory/3-api/cyf_ecommerce/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/week-2/mandatory/3-api/cyf_ecommerce/package-lock.json b/week-2/mandatory/3-api/cyf_ecommerce/package-lock.json new file mode 100644 index 00000000..dd1f5348 --- /dev/null +++ b/week-2/mandatory/3-api/cyf_ecommerce/package-lock.json @@ -0,0 +1,485 @@ +{ + "name": "cyf-ecommerce", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + } + }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "bytes": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" + }, + "content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "requires": { + "accepts": "~1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "content-type": "~1.0.4", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.5", + "qs": "6.7.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.1.2", + "send": "0.17.1", + "serve-static": "1.14.1", + "setprototypeof": "1.1.1", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + }, + "mime-types": { + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "requires": { + "mime-db": "1.44.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "pg": { + "version": "8.3.3", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.3.3.tgz", + "integrity": "sha512-wmUyoQM/Xzmo62wgOdQAn5tl7u+IA1ZYK7qbuppi+3E+Gj4hlUxVHjInulieWrd0SfHi/ADriTb5ILJ/lsJrSg==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.3.0", + "pg-pool": "^3.2.1", + "pg-protocol": "^1.2.5", + "pg-types": "^2.1.0", + "pgpass": "1.x", + "semver": "4.3.2" + } + }, + "pg-connection-string": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.3.0.tgz", + "integrity": "sha512-ukMTJXLI7/hZIwTW7hGMZJ0Lj0S2XQBCJ4Shv4y1zgQ/vqVea+FLhzywvPj0ujSuofu+yA4MYHGZPTsgjBgJ+w==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.1.tgz", + "integrity": "sha512-BQDPWUeKenVrMMDN9opfns/kZo4lxmSWhIqo+cSAF7+lfi9ZclQbr9vfnlNaPr8wYF3UYjm5X0yPAhbcgqNOdA==" + }, + "pg-protocol": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.5.tgz", + "integrity": "sha512-1uYCckkuTfzz/FCefvavRywkowa6M5FohNMF5OjKrqo9PSR8gYc8poVmwwYQaBxhmQdBjhtP514eXy9/Us2xKg==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz", + "integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=", + "requires": { + "split": "^1.0.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "proxy-addr": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", + "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "semver": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" + }, + "send": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", + "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.7.2", + "mime": "1.6.0", + "ms": "2.1.1", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, + "serve-static": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", + "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.1" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "requires": { + "through": "2" + } + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/week-2/mandatory/3-api/cyf_ecommerce/package.json b/week-2/mandatory/3-api/cyf_ecommerce/package.json new file mode 100644 index 00000000..7f1e19ea --- /dev/null +++ b/week-2/mandatory/3-api/cyf_ecommerce/package.json @@ -0,0 +1,15 @@ +{ + "name": "cyf-ecommerce", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.17.1", + "pg": "^8.3.3" + } +} diff --git a/week-2/mandatory/3-api/cyf_ecommerce/server.js b/week-2/mandatory/3-api/cyf_ecommerce/server.js new file mode 100644 index 00000000..905383e5 --- /dev/null +++ b/week-2/mandatory/3-api/cyf_ecommerce/server.js @@ -0,0 +1,43 @@ +const express = require("express"); +const app = express(); +const { Pool } = require("pg"); +const db = new Pool({ + user: "iman_", + host: "localhost", + database: "cyf_ecommerce", + password: "iman1364", + port: "5432", +}); + +app.get("/customers", (req, res) => { + db.query("select * from Customers", (err, result) => { + if (err == undefined) { + console.log(result); + } + }); +}); + +app.get("/suppliers", (req, res) => { + db.query("select * from suppliers", (err, result) => { + if (err == undefined) { + console.log(result); + } + }); +}); + +app.get("/products", (req, res) => { + db.query( + "select p.product_name, pa.unit_price, 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)", + (err, result) => { + if (err == undefined) { + console.log(result); + } + } + ); +}); + +app.listen(3000, () => { + console.log("port in listening"); +}); From 1841b391129e693cf5b6b6ba8f42c8318d2614f3 Mon Sep 17 00:00:00 2001 From: Iman Feisali Date: Fri, 2 Oct 2020 00:49:21 +0100 Subject: [PATCH 3/3] week 3 done --- .../mandatory/3-api/cyf_ecommerce/server.js | 14 + week-3/mandatory/2-api/cyf_ecommerce.sql | 124 +++++ .../mandatory/2-api/cyf_ecommerce/.gitignore | 1 + .../2-api/cyf_ecommerce/package-lock.json | 485 ++++++++++++++++++ .../2-api/cyf_ecommerce/package.json | 17 + .../mandatory/2-api/cyf_ecommerce/server.js | 362 +++++++++++++ 6 files changed, 1003 insertions(+) create mode 100644 week-3/mandatory/2-api/cyf_ecommerce.sql create mode 100644 week-3/mandatory/2-api/cyf_ecommerce/.gitignore create mode 100644 week-3/mandatory/2-api/cyf_ecommerce/package-lock.json create mode 100644 week-3/mandatory/2-api/cyf_ecommerce/package.json create mode 100644 week-3/mandatory/2-api/cyf_ecommerce/server.js diff --git a/week-2/mandatory/3-api/cyf_ecommerce/server.js b/week-2/mandatory/3-api/cyf_ecommerce/server.js index 905383e5..5bde58d5 100644 --- a/week-2/mandatory/3-api/cyf_ecommerce/server.js +++ b/week-2/mandatory/3-api/cyf_ecommerce/server.js @@ -38,6 +38,20 @@ app.get("/products", (req, res) => { ); }); +app.get("/customers/:id", (req, res) => { + const customer_id = parseInt(req.params.id); + db.query( + "select * from Customers where id = $1", + [customer_id], + (err, result) => { + if (err == undefined) { + console.log(result); + res.json(result.rows); + } + } + ); +}); + app.listen(3000, () => { console.log("port in listening"); }); diff --git a/week-3/mandatory/2-api/cyf_ecommerce.sql b/week-3/mandatory/2-api/cyf_ecommerce.sql new file mode 100644 index 00000000..7b335282 --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce.sql @@ -0,0 +1,124 @@ +drop table if exists order_items; +drop table if exists orders cascade; +DROP TABLE IF EXISTS product_availability cascade; +drop table if exists customers cascade; +drop table if exists products cascade; +drop table if exists suppliers cascade; + +CREATE TABLE customers ( + id SERIAL PRIMARY KEY, + name VARCHAR(50) NOT NULL, + address VARCHAR(120), + city VARCHAR(30), + country VARCHAR(20) +); + +CREATE TABLE suppliers ( + id SERIAL PRIMARY KEY, + supplier_name VARCHAR(100) NOT NULL, + country VARCHAR(20) NOT NULL +); + +CREATE TABLE products ( + id SERIAL PRIMARY KEY, + product_name VARCHAR(100) NOT NULL +); + +create table product_availability ( + prod_id integer references products(id), + supp_id integer references suppliers(id), + unit_price integer not null, + primary key (prod_id, supp_id) +); + +CREATE TABLE orders ( + id SERIAL PRIMARY KEY, + order_date DATE NOT NULL, + order_reference VARCHAR(10) NOT NULL, + customer_id INT REFERENCES customers(id) +); + +CREATE TABLE order_items ( + id SERIAL PRIMARY KEY, + order_id INT NOT NULL REFERENCES orders(id), + product_id INT NOT NULL, + supplier_id INT NOT NULL, + quantity INT NOT NULL, + FOREIGN KEY (product_id, supplier_id) + REFERENCES product_availability (prod_id, supp_id) +); + +INSERT INTO customers (name, address, city, country) VALUES ('Guy Crawford','770-2839 Ligula Road','Paris','France'); +INSERT INTO customers (name, address, city, country) VALUES ('Hope Crosby','P.O. Box 276, 4976 Sit Rd.','Steyr','United Kingdom'); +INSERT INTO customers (name, address, city, country) VALUES ('Britanney Kirkland','P.O. Box 577, 5601 Sem, St.','Little Rock','United Kingdom'); +INSERT INTO customers (name, address, city, country) VALUES ('Amber Tran','6967 Ac Road','Villafranca Asti','United States'); +INSERT INTO customers (name, address, city, country) VALUES ('Edan Higgins','Ap #840-3255 Tincidunt St.','Arles','United States'); +INSERT INTO customers (name, address, city, country) VALUES ('Quintessa Austin','597-2737 Nunc Rd.','Saint-Marc','United Kingdom'); + +INSERT INTO suppliers (supplier_name, country) VALUES ('Amazon', 'United States'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Taobao', 'China'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Argos', 'United Kingdom'); +INSERT INTO suppliers (supplier_name, country) VALUES ('Sainsburys', 'United Kingdom'); + + +INSERT INTO products (product_name) VALUES ('Mobile Phone X'); +INSERT INTO products (product_name) VALUES ('Javascript Book'); +INSERT INTO products (product_name) VALUES ('Le Petit Prince'); +INSERT INTO products (product_name) VALUES ('Super warm socks'); +INSERT INTO products (product_name) VALUES ('Coffee Cup'); +INSERT INTO products (product_name) VALUES ('Ball'); +INSERT INTO products (product_name) VALUES ('Tee Shirt Olympic Games'); + +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 4, 249); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 1, 299); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 2, 41); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 3, 39); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 1, 40); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 4, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 1, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 4, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 3, 8); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 2, 5); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 1, 10); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 4, 5); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 3, 4); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 2, 4); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 1, 3); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 2, 20); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 4, 15); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 1, 14); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 3, 21); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 2, 18); +INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 1, 20); + +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-06-01', 'ORD001', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-15', 'ORD002', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-11', 'ORD003', 1); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-24', 'ORD004', 2); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-30', 'ORD005', 3); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-05', 'ORD006', 4); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-04-05', 'ORD007', 4); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-23', 'ORD008', 5); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-24', 'ORD009', 5); +INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-10', 'ORD010', 5); + +INSERT INTO order_items VALUES (1, 1, 7, 2, 1); +INSERT INTO order_items VALUES (2, 1, 4, 2, 5); +INSERT INTO order_items VALUES (3, 2, 4, 3, 4); +INSERT INTO order_items VALUES (4, 2, 3, 4, 1); +INSERT INTO order_items VALUES (5, 3, 5, 3, 10); +INSERT INTO order_items VALUES (6, 3, 6, 2, 2); +INSERT INTO order_items VALUES (7, 4, 1, 1, 1); +INSERT INTO order_items VALUES (8, 5, 2, 3, 2); +INSERT INTO order_items VALUES (9, 5, 3, 1, 1); +INSERT INTO order_items VALUES (10, 6, 5, 2, 3); +INSERT INTO order_items VALUES (11, 6, 2, 2, 1); +INSERT INTO order_items VALUES (12, 6, 3, 4, 1); +INSERT INTO order_items VALUES (13, 6, 4, 4, 3); +INSERT INTO order_items VALUES (14, 7, 4, 3, 15); +INSERT INTO order_items VALUES (15, 8, 7, 1, 1); +INSERT INTO order_items VALUES (16, 8, 1, 4, 1); +INSERT INTO order_items VALUES (17, 9, 6, 4, 2); +INSERT INTO order_items VALUES (18, 10, 6, 2, 1); +INSERT INTO order_items VALUES (19, 10, 4, 1, 5); + diff --git a/week-3/mandatory/2-api/cyf_ecommerce/.gitignore b/week-3/mandatory/2-api/cyf_ecommerce/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/week-3/mandatory/2-api/cyf_ecommerce/package-lock.json b/week-3/mandatory/2-api/cyf_ecommerce/package-lock.json new file mode 100644 index 00000000..dd1f5348 --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce/package-lock.json @@ -0,0 +1,485 @@ +{ + "name": "cyf-ecommerce", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + } + }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "bytes": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" + }, + "content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", + "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "requires": { + "accepts": "~1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "content-type": "~1.0.4", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.5", + "qs": "6.7.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.1.2", + "send": "0.17.1", + "serve-static": "1.14.1", + "setprototypeof": "1.1.1", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + }, + "mime-types": { + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "requires": { + "mime-db": "1.44.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "pg": { + "version": "8.3.3", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.3.3.tgz", + "integrity": "sha512-wmUyoQM/Xzmo62wgOdQAn5tl7u+IA1ZYK7qbuppi+3E+Gj4hlUxVHjInulieWrd0SfHi/ADriTb5ILJ/lsJrSg==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.3.0", + "pg-pool": "^3.2.1", + "pg-protocol": "^1.2.5", + "pg-types": "^2.1.0", + "pgpass": "1.x", + "semver": "4.3.2" + } + }, + "pg-connection-string": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.3.0.tgz", + "integrity": "sha512-ukMTJXLI7/hZIwTW7hGMZJ0Lj0S2XQBCJ4Shv4y1zgQ/vqVea+FLhzywvPj0ujSuofu+yA4MYHGZPTsgjBgJ+w==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.1.tgz", + "integrity": "sha512-BQDPWUeKenVrMMDN9opfns/kZo4lxmSWhIqo+cSAF7+lfi9ZclQbr9vfnlNaPr8wYF3UYjm5X0yPAhbcgqNOdA==" + }, + "pg-protocol": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.5.tgz", + "integrity": "sha512-1uYCckkuTfzz/FCefvavRywkowa6M5FohNMF5OjKrqo9PSR8gYc8poVmwwYQaBxhmQdBjhtP514eXy9/Us2xKg==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz", + "integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=", + "requires": { + "split": "^1.0.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "proxy-addr": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", + "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "semver": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" + }, + "send": { + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", + "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.7.2", + "mime": "1.6.0", + "ms": "2.1.1", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, + "serve-static": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", + "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.1" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "requires": { + "through": "2" + } + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/week-3/mandatory/2-api/cyf_ecommerce/package.json b/week-3/mandatory/2-api/cyf_ecommerce/package.json new file mode 100644 index 00000000..96b4f404 --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce/package.json @@ -0,0 +1,17 @@ +{ + "name": "cyf-ecommerce", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "server.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.19.0", + "express": "^4.17.1", + "pg": "^8.3.3" + } +} diff --git a/week-3/mandatory/2-api/cyf_ecommerce/server.js b/week-3/mandatory/2-api/cyf_ecommerce/server.js new file mode 100644 index 00000000..ac77ca57 --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce/server.js @@ -0,0 +1,362 @@ +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: "iman_", + host: "localhost", + database: "cyf_ecommerce", + password: "iman1364", + port: "5432", +}); + +//capitalize First Letter of query +function capitalizeFirstLetter(str) { + return str.charAt(0).toUpperCase() + str.slice(1); +} + +app.get("/customers", (req, res) => { + db.query("select * from Customers", (err, result) => { + if (err == undefined) { + console.log(result); + res.status(200).json(result.rows); + } + }); +}); + +//get a single customer by ID +app.get("/customers/:id", (req, res) => { + const customer_Id = parseInt(req.params.id); + db.query( + "select * from Customers where id = $1", + [customer_Id], + (err, result) => { + if (err == undefined) { + console.log(result); + res.status(200).json(result.rows[0]); + } + } + ); +}); + +//create a new customer +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; + + db.query( + "insert into customers(name, address, city, country) " + + "values($1, $2, $3, $4)", + [newName, newAddress, newCity, newCountry], + (err, result) => { + if (err == undefined) { + res.status(200).json("customer created"); + } else { + console.log({ Error: err }); + } + } + ); +}); + +app.get("/suppliers", (req, res) => { + db.query("select * from suppliers", (err, result) => { + if (err == undefined) { + console.log(result); + } + }); +}); + +app.get("/products", (req, res) => { + const product_Name = req.query.name; + if (product_Name) { + db.query( + "select p.product_name, pa.unit_price, 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 product_name like '%'||$1||'%'", + [capitalizeFirstLetter(product_Name)], + (err, result) => { + if (err == undefined) { + console.log(result); + res.status(200).json(result.rows); + } else { + console.log({ Error: err }); + } + } + ); + } else { + db.query( + "select p.product_name, pa.unit_price, 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)", + (err, result) => { + if (err == undefined) { + console.log(result); + res.status(200).send(result.rows); + } + } + ); + } +}); + +//add new product +app.post("/products", (req, res) => { + const newProduct = req.body.product_name; + db.query( + "insert into products(product_name) " + "values($1)", + [newProduct], + (err, result) => { + if (err == undefined) { + res.status(200).json("new product added!"); + } else { + console.log({ Error: err }); + } + } + ); +}); + +// find a customer by Id +app.get("/customers/:id", (req, res) => { + const customer_id = parseInt(req.params.id); + db.query( + "select * from Customers where id = $1", + [customer_id], + (err, result) => { + if (err == undefined) { + console.log(result); + res.json(result.rows); + } + } + ); +}); + +//create a new order for an existing customer +app.post("/customers/:customerId/orders", (req, res) => { + const customerId = parseInt(req.params.customer_id); + const orderReference = req.body.order_reference; + const query = + "insert into orders(order_date, order_reference, customer_id) values(current_date, $1, $2)"; + db.query( + "SELECT * FROM customers WHERE id = $1", + [customerId], + (err, result) => { + if (err == undefined) { + //console.log("heloooo!"); + if (result.rowCount == 0) { + res.status(400).send(`Customer (${customerId}) does not exist.`); + return; + } + } + console.log(err); + } + ); + + db.query(query, [orderReference, customerId], (err, result) => { + if (err == undefined) { + newId = result.rows[0].id; + res.status(200).json(`new order for customer ${newId} added!`); + } else { + console.log(err); + res.status(400).json({ Error: err }); + } + }); +}); + +//update an existing customer +app.put("/customers/:id", function (req, res) { + const customerId = parseInt(req.params.id); + const customerName = req.body.name; + const customerAddress = req.body.address; + const customerCity = req.body.city; + const custCountry = req.body.country; + + db.query( + "SELECT * FROM customers WHERE id = $1", + [customerId], + (err, result) => { + if (result.rowCount != 1) { + res.status(400).send(`Customer (${customerId}) does not exist.`); + } else { + db.query( + "UPDATE customers SET name = $1, " + + " address = $2, " + + " city = $3, " + + " country = $4 " + + " WHERE id = $5", + [ + customerName, + customerAddress, + customerCity, + custCountry, + customerId, + ], + (err) => { + if (err == undefined) { + res.status(200).send(`Customer id:${customerId} updated.`); + } else { + res.status(500).json(err); + } + } + ); + } + } + ); +}); + +//create a new product availability +app.post("/availability/:prod_id", (req, res) => { + const unitPrice = req.body.unit_price; + const productId = parseInt(req.params.prod_id); + const supplierId = req.body.supp_id; + if (unitPrice > 0) { + db.query( + "insert into product_availability(prod_id, supp_id, unit_price) values($1, $2, $3)", + [productId, supplierId, unitPrice], + (err, result) => { + if (result.rows.length <= 0) { + res.json("please inter a valid product ID"); + } else if (err == undefined) { + res.status(200).json("new price added!"); + } else { + res.status(400).json({ Error: err }); + console.log({ Error: err }); + } + } + ); + } else if (unitPrice <= 0) { + res.json("pleaes insert a positive price!"); + } else { + res.status(400).json({ Error: err }); + } +}); + +//delete an existing order +app.delete("/orders/:id", function (req, res) { + const orderId = parseInt(req.params.id); + db.query( + "delete from order_items WHERE order_id = $1", + [orderId], + (err, result) => { + if (err == undefined) { + db.query( + "delete from orders WHERE id = $1", + [orderId], + (err, result) => { + if (err == undefined) { + if (result.rowCount == 0) { + res.status(400).send("No order with that id found."); + } else if ((result.rowCount = 1)) { + res + .status(200) + .send(`Order number ${orderId} has been deleted.`); + } + } else { + res.status(500).json(err); + } + } + ); + } else { + res.status(500).json(err); + } + } + ); +}); + +//delete an existing customer only if this customer doesn't have orders +app.delete("/customers/:id", function (req, res) { + const custId = parseInt(req.params.id); + db.query( + "select * from orders where customer_id = $1", + [custId], + (error, result) => { + if (error == undefined) { + if (result.rowCount > 0) { + res + .status(400) + .send( + `Cannot delete customer (${custId}) : orders exist for that customer.` + ); + } else { + db.query( + "DELETE FROM customers WHERE id = $1", + [custId], + (err, result) => { + if (err == undefined) { + if (result.rowCount == 1) { + res + .status(200) + .send(`Customer (${custId}) deleted successfully.`); + } else { + res.status(404).send(`Customer id:${custId} not found.`); + } + } else { + res.status(500).json(err); // error in DELETE + } + } + ); + } + } else { + res.status(500).json(error); // error in SELECT + } + } + ); +}); + +//load all the orders along with the items in the orders of a specific customer +app.get("/customers/:id/orders", function (req, res) { + const custId = parseInt(req.params.id); + db.query( + "SELECT o.id, o.order_reference, o.order_date, p.product_name, a.unit_price, " + + " s.supplier_name, i.quantity " + + " FROM orders o JOIN " + + " order_items i ON (i.order_id = o.id) JOIN " + + " product_availability a " + + " ON (a.prod_id = i.product_id AND a.supp_id = i.supplier_id) JOIN " + + " products p ON (p.id = a.prod_id) JOIN " + + " suppliers s ON (s.id = a.supp_id) " + + " WHERE o.customer_id = $1 " + + " ORDER BY o.order_reference", + [custId], + (err, result) => { + if (err == undefined) { + let orders = []; + let order = {}; + let item = {}; + let ordId = 0; + let ordRef = null; + let ordDate = null; + + for (i = 0; i < result.rows.length; i++) { + row = result.rows[i]; + if (ordId != row.id) { + if (ordId != 0) { + orders.push(order); + } + ordId = row.id; + ordRef = row.order_reference; + ordDate = row.order_date; + order = { order_reference: ordRef, order_date: ordDate, items: [] }; + } + item = { + product_name: row.product_name, + unit_price: row.unit_price, + supplier_name: row.supplier_name, + quantity: row.quantity, + }; + order.items.push(item); + } + orders.push(order); + res.status(200).json(orders); + } else { + res.status(500).json(err); + } + } + ); +}); + +app.listen(3000, () => { + console.log("port in listening"); +});