From 574dd1bc9c4ef7cbc83c52b4d4fad7a7fb35e5cb Mon Sep 17 00:00:00 2001 From: supertata Date: Fri, 18 Sep 2020 16:56:59 +0100 Subject: [PATCH 1/6] completed HW --- week-1/mandatory/2-classes-db/task.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index 735de6ae..ea1ca598 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -2,13 +2,23 @@ ## 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: ```sql - - +1. select * from rooms where rate > 100; +2. select *, checkout_date - checkin_date as night from reservations where checkin_date between '2020-09-01'and '2020-09-30' and checkout_date - checkin_date > 3; +3. select * from customers where city like 'M%'; +4. insert into room_types (room_type, def_rate) +values ('PENTHOUSE', '185'); +5.insert into rooms (room_no, rate, room_type) +values ('501' ,185, 'PENTHOUSE'),('502' ,185, 'PENTHOUSE') ; +6. insert into rooms (room_no, rate, room_type) +values ('503' ,'143', 'PREMIER PLUS'); +7. select count(room_no) from reservations where checkin_date <= '2020-08-31' and checkout_date > '2020-08-01' +8.select sum(checkout_date - checkin_date) from reservations where room_no between 201 and 299; +9. select count(*) as invoice_count, sum(total) as total_invoices, avg(total) as average_invoice from invoices where total > 300 ``` When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. @@ -18,6 +28,7 @@ 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? 2. List the reservations that have a checkin date this month and are for more than three nights. 3. List all customers from cities that begin with the letter 'M'. @@ -33,4 +44,4 @@ Using what you can learn about aggregate functions in the w3schools SQL classes 7. The hotel manager wishes to know how many rooms were occupied any time during the previous month - find that information. 8. Get the total number of nights that customers stayed in rooms on the second floor (rooms 201 - 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**) +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**) From 45fcf0eaffe8838b03f338998a34ea6d8d8117b5 Mon Sep 17 00:00:00 2001 From: supertata Date: Wed, 23 Sep 2020 21:02:44 +0100 Subject: [PATCH 2/6] completed HW --- week-2/mandatory/2-ecommerce-db/task.md | 61 +++++++++++- week-2/mandatory/cyf_ecommerce.sql | 124 ++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 week-2/mandatory/cyf_ecommerce.sql diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index c48d2286..f019ccc2 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -7,7 +7,63 @@ 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 +1. select * from customers +where country = 'United States'; +2. select * from customers +order by name asc; +3. select * from products +where product_name like '%socks%'; +4. select p.id, p.product_name, a.supp_id, a.unit_price from prodcuts p +join product_availability a on (a.prod_id = p.id) where unit_price > 100; +5. select p.id, p.product_name, a.supp_id, a.unit_price from products p +join product_availability a on a.prod_id = p.id +order by a.unit_price desc limit 5; +6. 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 pa.supp_id = s.id; +7. select p.product_name, s.supplier_name from products p +join product_availability pa on p.id = pa.prod_id +join suppliers s on pa.supp_id = s.id where s.country = 'United Kingdom'; + +select p.product_name, s.supplier_name from products p +join product_availability pa on p.id = pa.prod_id +join suppliers s on pa.supp_id = s.id where upper(s.country) = 'UNITED KINGDOM'; +8. select o.id, 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 product_availability pa on oi.product_id = pa.prod_id where c.id = 1; +9. select * from orders o +join order_items oi on o.id = oi.order_id where customer_id = (select id from customers where name = 'Hope Crosby'); +select * from orders o +join order_items oi on o.id = oi.order_id +join customers c on o.customer_id = c.id where c.name = 'Hope Crosby'; +10. select p.product_name, oi.quantity, pa.unit_price from products p +join product_availability pa on pa.prod_id = p.id +join order_items oi on oi.product_id = pa.prod_id +join orders o on o.id = oi.order_id where o.order_reference = 'ORD006'; +11. select c.name, o.order_reference, o.order_date, p.product_name, s.supplier_name, oi.quantity from customers c +join orders o on c.id = o.customer_id +join order_items oi on oi.order_id = o.id +join products p on p.id = oi.product_id +join suppliers s on s.id = oi.supplier_id; +12. select distinct c.name, s.country 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 oi.supplier_id = s.id +where s.country = 'China'; +13. select c.name, o.order_reference, o.order_date, sum(oi.quantity*pa.unit_price) as "total amount" from orders o +join customers c on c.id = o.customer_id +join order_items oi on oi.order_id = o.id +join product_availability pa on pa.prod_id = oi.product_id +group by c.name, o.order_reference, o.order_date +order by "total amount" desc; + + + + + ``` @@ -41,10 +97,9 @@ Once you understand the database that you are going to work with, solve the foll 5. Retrieve the 5 most expensive products 6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name` 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). +8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity \* unit price). 9. Retrieve all orders, including order items, from customer named `Hope Crosby` 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. 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`. 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. - +13. List all orders giving customer name, order reference, order date and order total amount (quantity \* unit price) in descending order of total. diff --git a/week-2/mandatory/cyf_ecommerce.sql b/week-2/mandatory/cyf_ecommerce.sql new file mode 100644 index 00000000..7b335282 --- /dev/null +++ b/week-2/mandatory/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); + From b987ed88655ddb2e7748c7276e3b58790ebe37ae Mon Sep 17 00:00:00 2001 From: supertata Date: Wed, 23 Sep 2020 21:41:10 +0100 Subject: [PATCH 3/6] added api --- .../3-api/cyf-ecommerce-api/.gitignore | 1 + .../3-api/cyf-ecommerce-api/index.js | 39 ++ .../3-api/cyf-ecommerce-api/package-lock.json | 485 ++++++++++++++++++ .../3-api/cyf-ecommerce-api/package.json | 15 + 4 files changed, 540 insertions(+) create mode 100644 week-2/mandatory/3-api/cyf-ecommerce-api/.gitignore create mode 100644 week-2/mandatory/3-api/cyf-ecommerce-api/index.js create mode 100644 week-2/mandatory/3-api/cyf-ecommerce-api/package-lock.json create mode 100644 week-2/mandatory/3-api/cyf-ecommerce-api/package.json diff --git a/week-2/mandatory/3-api/cyf-ecommerce-api/.gitignore b/week-2/mandatory/3-api/cyf-ecommerce-api/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/week-2/mandatory/3-api/cyf-ecommerce-api/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/week-2/mandatory/3-api/cyf-ecommerce-api/index.js b/week-2/mandatory/3-api/cyf-ecommerce-api/index.js new file mode 100644 index 00000000..6c77fda1 --- /dev/null +++ b/week-2/mandatory/3-api/cyf-ecommerce-api/index.js @@ -0,0 +1,39 @@ +const express = require("express"); +const app = express(); +const { Pool } = require("pg"); + +const db = new Pool({ + user: "laetitianode", + host: "localhost", + database: "cyf_ecommerce", + password: "123", + port: 5432, +}); + +app.get("/suppliers", function (req, res) { + db.query("Select * from suppliers", (error, results) => { + console.log(results); + res.json(results); + }); +}); + +app.get("/products", function (req, res) { + db.query( + "Select pa.unit_price, s.supplier_name, product_name from products p join product_availability pa on p.id = pa.prod_id join suppliers s on s.id = supp_id", + (error, results) => { + console.log(results); + res.json(results); + } + ); +}); + +app.get("/customers", function (req, res) { + db.query("Select * from customers", (error, results) => { + console.log(results); + res.json(results); + }); +}); + +app.listen(3009, function () { + console.log("Server is listening on port 3009."); +}); diff --git a/week-2/mandatory/3-api/cyf-ecommerce-api/package-lock.json b/week-2/mandatory/3-api/cyf-ecommerce-api/package-lock.json new file mode 100644 index 00000000..ac376746 --- /dev/null +++ b/week-2/mandatory/3-api/cyf-ecommerce-api/package-lock.json @@ -0,0 +1,485 @@ +{ + "name": "cyf-ecommerce-api", + "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-api/package.json b/week-2/mandatory/3-api/cyf-ecommerce-api/package.json new file mode 100644 index 00000000..1d9cee6c --- /dev/null +++ b/week-2/mandatory/3-api/cyf-ecommerce-api/package.json @@ -0,0 +1,15 @@ +{ + "name": "cyf-ecommerce-api", + "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" + } +} From 4c74f687115b0435e49f641eddbceb18eec30201 Mon Sep 17 00:00:00 2001 From: supertata Date: Sun, 27 Sep 2020 17:13:36 +0100 Subject: [PATCH 4/6] completed all tasks HW week 3 --- .../3-api/cyf-ecommerce-api/index.js | 273 +++++++++++++++++- 1 file changed, 265 insertions(+), 8 deletions(-) diff --git a/week-2/mandatory/3-api/cyf-ecommerce-api/index.js b/week-2/mandatory/3-api/cyf-ecommerce-api/index.js index 6c77fda1..5e848e8c 100644 --- a/week-2/mandatory/3-api/cyf-ecommerce-api/index.js +++ b/week-2/mandatory/3-api/cyf-ecommerce-api/index.js @@ -1,6 +1,9 @@ const express = require("express"); const app = express(); +// const bodyParser = require("body-parser"); +app.use(express.json()); const { Pool } = require("pg"); +const { response } = require("express"); const db = new Pool({ user: "laetitianode", @@ -17,14 +20,25 @@ app.get("/suppliers", function (req, res) { }); }); +// load all products names, prices and supplier name app.get("/products", function (req, res) { - db.query( - "Select pa.unit_price, s.supplier_name, product_name from products p join product_availability pa on p.id = pa.prod_id join suppliers s on s.id = supp_id", - (error, results) => { - console.log(results); - res.json(results); + const name = req.query.name; + let queryName = " "; + let sqlQuery = + "Select pa.unit_price, s.supplier_name, p.product_name from products p" + + " join product_availability pa on p.id = pa.prod_id join suppliers s on s.id = pa.supp_id "; + let sqlParameter = []; + if (name !== undefined) { + queryName = " where p.product_name like '%'|| $1 ||'%'"; + sqlQuery = sqlQuery + queryName; + sqlParameter = [name]; + } + db.query(sqlQuery, sqlParameter, (error, results) => { + if (!error) { + res.json(results.rows); + return; } - ); + }); }); app.get("/customers", function (req, res) { @@ -33,7 +47,250 @@ app.get("/customers", function (req, res) { res.json(results); }); }); +//load a single customer by ID. +app.get("/customers/:id", function (req, res) { + const id = parseInt(req.params.id); + db.query("Select * from customers where id = $1", [id], function ( + err, + results + ) { + res.json({ customer: results.rows[0] }); + }); +}); + +// app.get("/customers/:id", function (req, res) { +// db.query("Select * from customers ", (error, results) => { +// console.log(results); +// res.json(results); +// }); +// }); + +//to create a new customer with name, address, city and country. +app.post("/customers", function (req, res) { + const name = req.body.name; + const address = req.body.address; + const city = req.body.city; + const country = req.body.country; + db.query( + "insert into customers (name, address, city, country)" + + // "values ($1, $2, $3, $4) returning name", + "values ($1, $2, $3, $4) returning name, address, city, country", + [name, address, city, country], + (err, result) => { + console.log(err); + console.log({ customer: result.rows[0] }); + + if (err == undefined) { + // res.json("insert OK"); + res.json({ customer: result.rows[0] }); + } + } + ); +}); +//create a new product +app.post("/products", function (req, res) { + let newProductName = req.body.product_name; + db.query( + "insert into products (product_name)" + "values ($1)", + [newProductName], + (err) => { + if (!err) { + res.json("insert ok"); + } + } + ); +}); +//see all products +app.get("/products", function (req, res) { + db.query("Select * from products", (error, results) => { + console.log(results); + res.json(results); + }); +}); + +//create a new product availability +app.post("/products/:id/availability", function (req, res) { + const productId = Number(req.params.id); + const price = Number(req.body.unit_price); + const supplierId = Number(req.body.supp_id); + if (!Number.isInteger(price) || price <= 0) { + res.status(400).send("The price is invalid. Please enter a correct price"); + return; + } + db.query( + "select * from products where id = $1", + [productId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find your product"); + return; + } + } + } + ); + db.query( + "select * from suppliers where id = $1", + [supplierId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find your supplier"); + return; + } + } + } + ); + db.query( + "insert into product_availability (prod_id, supp_id, unit_price)" + + "values ($1, $2, $3)", + [productId, supplierId, price], + (err) => { + if (!err) { + res.json("insert ok"); + } + } + ); +}); + +//create a new order for an existing customer +app.post("/customers/:customerId/orders", (req, res) => { + const customerId = Number(req.params.customerId); + const orderDate = req.body.order_date; + const orderReference = req.body.order_reference; + db.query( + "select * from customers where id = $1", + [customerId], + (err, result) => { + if (!err) { + if (result.rowCount == 0) { + res.send("We couldn't find this customer"); + return; + } + } + } + ); + db.query( + "insert into orders (order_date, order_reference, customer_id)" + + "values ($1, $2, $3)", + [orderDate, orderReference, customerId], + (err) => { + if (!err) { + res.send("New order added to customer account"); + } + } + ); +}); +//update an existing customer +app.put("/customers/:customerId", (req, res) => { + const customerId = Number(req.params.customerId); + const name = req.body.name; + const address = req.body.address; + const city = req.body.city; + const country = req.body.country; + db.query( + "select * from customers where id = $1", + [customerId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find this customer"); + return; + } + } + } + ); + db.query( + "update customers set name = $1, address = $2, city = $3, country = $4" + + "where id = $5", + [name, address, city, country, customerId], + (err) => { + if (!err) { + res.send("succeeded"); + } + } + ); +}); + +//delete an existing order +app.delete("/orders/:orderId", (req, res) => { + let orderId = Number(req.params.orderId); + db.query("select * from orders where id = $1", [orderId], (err, result) => { + if (!err) { + if (result.rowCount == 0) { + res.send("We couldn't find your order"); + } + } + }); + // db.query("delete from order_items where order_id = $1", [orderId], (err) => { + // if (!err) { + // db.query("delete from orders where id = $1", [orderId], (err) => { + // if (!err) { + // res.send("item deleted"); + // } + // }); + // } + // }); + db.query("delete from order_items where order_id = $1", [orderId]) + .then((result) => db.query("delete from orders where id = $1", [orderId])) + .then((result) => { + res.send("item deleted"); + return; + }) + .catch((error) => res.send("something went wrong")); +}); +//delete an existing customer +app.delete("/customers/:customerId", (req, res) => { + let customerId = Number(req.params.customerId); + db.query( + "select * from orders where customer_id = $1", + [customerId], + (err, result) => { + if (!err) { + if (result.rowCount > 0) { + res.send("There is an order attached to this customer"); + return; + } + } + } + ); + db.query( + "delete from customers where id = $1", + [customerId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("We couldn't find your customer"); + } else { + res.send("customer has been deleted"); + } + } + } + ); +}); + +//load all the orders along with the items in the orders of a specific customer +app.get("/customers/:customerId/orders", (req, res) => { + const customerId = Number(req.params.customerId); + let sqlQuery = `select o.order_reference, o.order_date, p.product_name, pa.unit_price, s.supplier_name, oi.quantity + from orders o +join order_items oi on o.id = oi.order_id +join product_availability pa on oi.supplier_id = pa.supp_id and +oi.product_id = pa.prod_id +join products p on p.id = pa.prod_id +join suppliers s on s.id = pa.supp_id +where o.customer_id = $1;`; + db.query(sqlQuery, [customerId], (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.status(403).send("no customer number found"); + } else { + res.json(results.rows); + } + } + }); +}); -app.listen(3009, function () { - console.log("Server is listening on port 3009."); +app.listen(3012, function () { + console.log("Server is listening on port 3012."); }); From b3d0af5854c73935eea400c53edddcb46e1791ac Mon Sep 17 00:00:00 2001 From: supertata Date: Sun, 27 Sep 2020 17:40:10 +0100 Subject: [PATCH 5/6] complete HW week 3 --- .../2-api/cyf-ecommerce-api copy/.gitignore | 1 + .../2-api/cyf-ecommerce-api copy/index.js | 296 +++++++++++ .../cyf-ecommerce-api copy/package-lock.json | 485 ++++++++++++++++++ .../2-api/cyf-ecommerce-api copy/package.json | 15 + week-3/mandatory/2-api/cyf_ecommerce copy.sql | 124 +++++ 5 files changed, 921 insertions(+) create mode 100644 week-3/mandatory/2-api/cyf-ecommerce-api copy/.gitignore create mode 100644 week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js create mode 100644 week-3/mandatory/2-api/cyf-ecommerce-api copy/package-lock.json create mode 100644 week-3/mandatory/2-api/cyf-ecommerce-api copy/package.json create mode 100644 week-3/mandatory/2-api/cyf_ecommerce copy.sql diff --git a/week-3/mandatory/2-api/cyf-ecommerce-api copy/.gitignore b/week-3/mandatory/2-api/cyf-ecommerce-api copy/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/week-3/mandatory/2-api/cyf-ecommerce-api copy/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js b/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js new file mode 100644 index 00000000..074f68bc --- /dev/null +++ b/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js @@ -0,0 +1,296 @@ +const express = require("express"); +const app = express(); +// const bodyParser = require("body-parser"); +app.use(express.json()); +const { Pool } = require("pg"); +const { response } = require("express"); + +const db = new Pool({ + user: "laetitianode", + host: "localhost", + database: "cyf_ecommerce", + password: "123", + port: 5432, +}); + +app.get("/suppliers", function (req, res) { + db.query("Select * from suppliers", (error, results) => { + console.log(results); + res.json(results); + }); +}); + +// load all products names, prices and supplier name +app.get("/products", function (req, res) { + const name = req.query.name; + let queryName = " "; + let sqlQuery = + "Select pa.unit_price, s.supplier_name, p.product_name from products p" + + " join product_availability pa on p.id = pa.prod_id join suppliers s on s.id = pa.supp_id "; + let sqlParameter = []; + if (name !== undefined) { + queryName = " where p.product_name like '%'|| $1 ||'%'"; + sqlQuery = sqlQuery + queryName; + sqlParameter = [name]; + } + db.query(sqlQuery, sqlParameter, (error, results) => { + if (!error) { + res.json(results.rows); + return; + } + }); +}); + +app.get("/customers", function (req, res) { + db.query("Select * from customers", (error, results) => { + console.log(results); + res.json(results); + }); +}); +//load a single customer by ID. +app.get("/customers/:id", function (req, res) { + const id = parseInt(req.params.id); + db.query("Select * from customers where id = $1", [id], function ( + err, + results + ) { + res.json({ customer: results.rows[0] }); + }); +}); + +// app.get("/customers/:id", function (req, res) { +// db.query("Select * from customers ", (error, results) => { +// console.log(results); +// res.json(results); +// }); +// }); + +//to create a new customer with name, address, city and country. +app.post("/customers", function (req, res) { + const name = req.body.name; + const address = req.body.address; + const city = req.body.city; + const country = req.body.country; + db.query( + "insert into customers (name, address, city, country)" + + // "values ($1, $2, $3, $4) returning name", + "values ($1, $2, $3, $4) returning name, address, city, country", + [name, address, city, country], + (err, result) => { + console.log(err); + console.log({ customer: result.rows[0] }); + + if (err == undefined) { + // res.json("insert OK"); + res.json({ customer: result.rows[0] }); + } + } + ); +}); +//create a new product +app.post("/products", function (req, res) { + let newProductName = req.body.product_name; + db.query( + "insert into products (product_name)" + "values ($1)", + [newProductName], + (err) => { + if (!err) { + res.json("insert ok"); + } + } + ); +}); +//see all products +app.get("/products", function (req, res) { + db.query("Select * from products", (error, results) => { + console.log(results); + res.json(results); + }); +}); + +//create a new product availability +app.post("/products/:id/availability", function (req, res) { + const productId = Number(req.params.id); + const price = Number(req.body.unit_price); + const supplierId = Number(req.body.supp_id); + if (!Number.isInteger(price) || price <= 0) { + res.status(400).send("The price is invalid. Please enter a correct price"); + return; + } + db.query( + "select * from products where id = $1", + [productId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find your product"); + return; + } + } + } + ); + db.query( + "select * from suppliers where id = $1", + [supplierId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find your supplier"); + return; + } + } + } + ); + db.query( + "insert into product_availability (prod_id, supp_id, unit_price)" + + "values ($1, $2, $3)", + [productId, supplierId, price], + (err) => { + if (!err) { + res.json("insert ok"); + } + } + ); +}); + +//create a new order for an existing customer +app.post("/customers/:customerId/orders", (req, res) => { + const customerId = Number(req.params.customerId); + const orderDate = req.body.order_date; + const orderReference = req.body.order_reference; + db.query( + "select * from customers where id = $1", + [customerId], + (err, result) => { + if (!err) { + if (result.rowCount == 0) { + res.send("We couldn't find this customer"); + return; + } + } + } + ); + db.query( + "insert into orders (order_date, order_reference, customer_id)" + + "values ($1, $2, $3)", + [orderDate, orderReference, customerId], + (err) => { + if (!err) { + res.send("New order added to customer account"); + } + } + ); +}); +//update an existing customer +app.put("/customers/:customerId", (req, res) => { + const customerId = Number(req.params.customerId); + const name = req.body.name; + const address = req.body.address; + const city = req.body.city; + const country = req.body.country; + db.query( + "select * from customers where id = $1", + [customerId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("we didn't find this customer"); + return; + } + } + } + ); + db.query( + "update customers set name = $1, address = $2, city = $3, country = $4" + + "where id = $5", + [name, address, city, country, customerId], + (err) => { + if (!err) { + res.send("succeeded"); + } + } + ); +}); + +//delete an existing order +app.delete("/orders/:orderId", (req, res) => { + let orderId = Number(req.params.orderId); + db.query("select * from orders where id = $1", [orderId], (err, result) => { + if (!err) { + if (result.rowCount == 0) { + res.send("We couldn't find your order"); + } + } + }); + db.query("delete from order_items where order_id = $1", [orderId], (err) => { + if (!err) { + db.query("delete from orders where id = $1", [orderId], (err) => { + if (!err) { + res.send("item deleted"); + } + }); + } + }); +// db.query("delete from order_items where order_id = $1", [orderId]) +// .then((result) => db.query("delete from orders where id = $1", [orderId])) +// .then((result) => { +// res.send("item deleted"); +// return; +// }) +// .catch((error) => res.send("something went wrong")); +// }); +//delete an existing customer +app.delete("/customers/:customerId", (req, res) => { + let customerId = Number(req.params.customerId); + db.query( + "select * from orders where customer_id = $1", + [customerId], + (err, result) => { + if (!err) { + if (result.rowCount > 0) { + res.send("There is an order attached to this customer"); + return; + } + } + } + ); + db.query( + "delete from customers where id = $1", + [customerId], + (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.send("We couldn't find your customer"); + } else { + res.send("customer has been deleted"); + } + } + } + ); +}); + +//load all the orders along with the items in the orders of a specific customer +app.get("/customers/:customerId/orders", (req, res) => { + const customerId = Number(req.params.customerId); + let sqlQuery = `select o.order_reference, o.order_date, p.product_name, pa.unit_price, s.supplier_name, oi.quantity + from orders o +join order_items oi on o.id = oi.order_id +join product_availability pa on oi.supplier_id = pa.supp_id and +oi.product_id = pa.prod_id +join products p on p.id = pa.prod_id +join suppliers s on s.id = pa.supp_id +where o.customer_id = $1;`; + db.query(sqlQuery, [customerId], (err, results) => { + if (!err) { + if (results.rowCount == 0) { + res.status(403).send("no customer number found"); + } else { + res.json(results.rows); + } + } + }); +}); + +app.listen(3012, function () { + console.log("Server is listening on port 3012."); +}); diff --git a/week-3/mandatory/2-api/cyf-ecommerce-api copy/package-lock.json b/week-3/mandatory/2-api/cyf-ecommerce-api copy/package-lock.json new file mode 100644 index 00000000..ac376746 --- /dev/null +++ b/week-3/mandatory/2-api/cyf-ecommerce-api copy/package-lock.json @@ -0,0 +1,485 @@ +{ + "name": "cyf-ecommerce-api", + "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-api copy/package.json b/week-3/mandatory/2-api/cyf-ecommerce-api copy/package.json new file mode 100644 index 00000000..1d9cee6c --- /dev/null +++ b/week-3/mandatory/2-api/cyf-ecommerce-api copy/package.json @@ -0,0 +1,15 @@ +{ + "name": "cyf-ecommerce-api", + "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-3/mandatory/2-api/cyf_ecommerce copy.sql b/week-3/mandatory/2-api/cyf_ecommerce copy.sql new file mode 100644 index 00000000..7b335282 --- /dev/null +++ b/week-3/mandatory/2-api/cyf_ecommerce copy.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); + From ed543626f616576217abe691cab5e29e1bcfd2c4 Mon Sep 17 00:00:00 2001 From: supertata Date: Sun, 27 Sep 2020 17:56:27 +0100 Subject: [PATCH 6/6] completed hw 3 --- week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js b/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js index 074f68bc..ae911253 100644 --- a/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js +++ b/week-3/mandatory/2-api/cyf-ecommerce-api copy/index.js @@ -13,6 +13,7 @@ const db = new Pool({ port: 5432, }); + app.get("/suppliers", function (req, res) { db.query("Select * from suppliers", (error, results) => { console.log(results);