Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Amanul/week2 #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions week-1/mandatory/2-classes-db/SQL-week-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1) Retrieve all the mentors who lived more than 5 years in Glasgow
=> SELECT * FROM mentors WHERE years_of_stay_in_glasgow > 2;
2) Retrieve all the mentors whose favourite language is Javascript
=> SELECT * FROM mentors WHERE favorite_programming_language ='Javascript';
3) Retrieve all the students who are CYF graduates
=> SELECT * FROM students WHERE graduated='t';
4) Retrieve all the classes taught before June this year
=> SELECT * FROM classes WHERE date < '2020-6-1';
5) Retrieve all the students (retrieving student ids only is fine) who attended the Javascript class (or any other class that you have in the classes table).
=> SELECT classes.topic, students.id FROM classes, students WHERE students.id=classes.id AND classes.topic='JavaScript';
8 changes: 8 additions & 0 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ When you have finished all of the questions - open a pull request with your answ
## Task

1. Create a new database called `cyf_classes` (hint: use `createdb` in the terminal)

select name,address from customers where country='United States';

2. Create a new table `mentors`, for each mentor we want to save their name, how many years they lived in Glasgow, their address and their favourite programming language.
3. Insert 5 mentors in the `mentors` table (you can make up the data, it doesn't need to be accurate ;-)).
4. Create a new table `students`, for each student we want to save their name, address and if they have graduated from Code Your Future.
Expand All @@ -31,7 +34,12 @@ When you have finished all of the questions - open a pull request with your answ
9. We now want to store who among the students attends a specific class. How would you store that? Come up with a solution and insert some data if you model this as a new table.
10. Answer the following questions using a `select` SQL statement:
- Retrieve all the mentors who lived more than 5 years in Glasgow
<!-- Insert command in the terminal: SELECT * FROM mentors WHERE years_of_stay_in_glasgow > 2; -->
- Retrieve all the mentors whose favourite language is Javascript
<!-- Insert command in the terminal: SELECT * FROM mentors WHERE favorite_programming_language = 'JavaScript'; -->
- Retrieve all the students who are CYF graduates
<!-- Insert command in the terminal: SELECT * FROM students WHERE graduated = 't'; -->
- Retrieve all the classes taught before June this year
<!-- Insert command in the terminal: SELECT * FROM classes WHERE date < '2020-06-01'; -->
- Retrieve all the students (retrieving student ids only is fine) who attended the Javascript class (or any other class that you have in the `classes` table).
<!-- Insert command in the terminal: SELECT classes.topic, students.id FROM classes, students WHERE students.id = classes.id AND classes.topic = 'JavaScript'; -->
33 changes: 33 additions & 0 deletions week-2/mandatory/2-ecommerce-db/SQL-week-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1) select name,address from customers where country='United States';

2) select * from customers order by name;

3) select * from products where unit_price>100;

4) select * from products where product_name like '%socks%';

5)

6) select product_name,unit_price,supplier_name from products inner join suppliers on suppliers.id=products.supplier_id;

7) select product_name,supplier_name from products inner join suppliers on suppliers.id=products.supplier_id where suppliers.country='United Kingdom';

8) select order_date,name,city,country from orders inner join customers on customers.id=orders.customer_id where customers.id=1 ;

9) select order_date,name,city,country from orders inner join customers on customers.id=orders.customer_id where customers.name='Hope Crosby' ;

10) select product_name,unit_price, quantity from order_items inner join products on products.id=order_items.product_id where
order_id=6;

11) select order_reference,order_date,product_name,supplier_name,name, quantity from
orders inner join order_items on order_items.order_id=orders.id
inner join products on products.id=order_items.product_id
inner join customers on customers.id=orders.customer_id
inner join suppliers on suppliers.id=products.supplier_id;

12) select supplier_name,name from
orders inner join order_items on order_items.order_id=orders.id
inner join products on products.id=order_items.product_id
inner join customers on customers.id=orders.customer_id
inner join suppliers on suppliers.id=products.supplier_id where suppliers.country='China';

233 changes: 233 additions & 0 deletions week-2/mandatory/3-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@

const express = require("express");
const cors = require("cors");
const app = express();
const bodyParser = require("body-parser");

app.use(bodyParser.json());
app.use(cors());


const { Pool, Client } = require("pg");

const pool = new Pool({
user: "Beatz",
host: "localhost",
database: "cyf_ecommerce",
password: "Alomuddin91!",
port: "5432",
});

// Add a new GET endpoint `/customers` to load all the customers from the database
app.get("/customers", function (req, res) {
const Query="SELECT * FROM customers";
pool.query(Query)
.then((result) => res.send(result.rows))
.catch((e) => console.log(e));
});

// Add a new GET endpoint `/suppliers` to load all the suppliers from the database
app.get("/suppliers", function (req, res) {

const Query="SELECT * FROM suppliers";
pool.query(Query)
.then((result) => res.send(result.rows))
.catch((e) => console.log(e));
});

// Add a new GET endpoint `/products` to load all the product names along with their supplier names
// Update the previous GET endpoint `/products` to filter the list of products by name using a query parameter, for example `/products?name=Cup`. This endpoint should still work even if you don't use the `name` query parameter!
app.get("/products", function (req, res) {
let name = req.query.name;
console.log(name);
if(typeof (name)=="undefined")
{
console.log("Name is undefined")
const Query="SELECT product_name, supplier_name FROM products INNER JOIN suppliers ON suppliers.id=products.supplier_id";
pool.query(Query)
.then((result) => res.send(result.rows))
.catch((e) => console.log(e));
}else{
const Query="SELECT product_name, supplier_name FROM products INNER JOIN suppliers ON suppliers.id=products.supplier_id WHERE product_name=$1";
pool.query(Query,[name])
.then((result) => res.send(result.rows))
.catch((e) => console.log(e));
}
});

// Add a new GET endpoint `/customers/:customerId` to load a single customer by ID
app.get("/customers/:customerId", function (req, res) {
let {customerId} = req.params;
console.log(customerId)
if(typeof (customerId)!="undefined")
{
const Query="SELECT * FROM customers WHERE id=$1";
pool.query(Query,[customerId])
.then((result) => res.send(result.rows))
.catch((e) => console.log(e));
}
else{
res.send(false);
console.log("Present Id");
}
});

// Add a new POST endpoint `/customers` to create a new customer
app.post("/customers", function (req, res) {
let {name} = req.body;
let {address} = req.body;
let {city} = req.body;
let {country} = req.body;
if(typeof (name)!="undefined" || typeof (address)!="undefined" || typeof (city)!="undefined" || typeof (country)!="undefined" )
{
const Query="INSERT INTO customers(name, address, city, country) VALUES($1,$2,$3,$4)";
pool.query(Query, [name, address, city, country])
.then(result => res.send("Details added")) // confirm if it has been added or not.
.catch((e) => console.log(e));
}else{
res.send("Cannot write data, please fill in the missing fields")
}
});

// Add a new POST endpoint /products to create a new product (with a product name, a price and a supplier id). Check that the price is a positive integer and that the supplier ID exists in the database, otherwise return an error.
app.post("/products", function (req, res) {
if (!req.body) {
return res.status(400).send("Body not found");
}
const newProductName = req.body.product_name;
const newProductPrice = req.body.unit_price;
const newProductSupplierId = req.body.supplier_id;
if (!Number.isInteger(newProductPrice) || newProductPrice <= 0) {
console.log(newProductPrice);
return res
.status(400)
.send("The price of products should be a positive integer.");
}
pool
.query("SELECT * FROM suppliers WHERE id =$1", [newProductSupplierId])
.then((result) => {
if (!result.rows.length) {
return res
.status(400)
.send(`Supplier with the ${newProductSupplierId} does not exists!`);
}});
const query =
"INSERT INTO products (product_name, unit_price, supplier_id) VALUES ($1, $2, $3)";
pool
.query(query, [newProductName, newProductPrice, newProductSupplierId])
.then(() => res.send("product has been created!"))
.catch((e) => console.error(e));
});

// Add a new POST endpoint /customers/:customerId/orders to create a new order (including an order date, and an order reference) for a customer. Check that the customerId corresponds to an existing customer or return an error.
app.post("/customers/:customerId/orders", function (req, res) {
if (!req.body) {
return res.status(400).send("Body not found");
}

let {order_date} = req.body;
let {order_reference} = req.body;
let customer_id = req.params.customerId;

if(typeof (order_date)=="undefined" || typeof (order_reference)=="undefined" || typeof (customer_id)=="undefined" )
{
res.send("There is a missing field");
}
pool
.query("SELECT * FROM customers WHERE id =$1", [customer_id])
.then((result) => {
if (!result.rows.length) {
return res
.status(400)
.send(`Customer with the ${customer_id} does not exist!`);
}});
const query =
"INSERT INTO orders (order_date, order_reference, customer_id) VALUES ($1, $2, $3)";
pool
.query(query, [order_date, order_reference, customer_id])
.then(() => res.send("Order created!"))
.catch((e) => console.error(e));
});

// Add a new PUT endpoint /customers/:customerId to update an existing customer (name, address, city and country)
app.put("/customers/:customerId", function (req, res) {
let customer_id = req.params.customerId;
let {name} = req.body;
let {address} = req.body;
let {city} = req.body;
let {country} = req.body;
if(typeof (name)!="undefined" || typeof (address)!="undefined" || typeof (city)!="undefined" || typeof (country)!="undefined" || typeof (customer_id)!="undefined" )
{
const Query="UPDATE customers SET name = $1, address = $2, city=$3,country=$4 WHERE id=$5;";
pool.query(Query,[name,address,city,country,customer_id])
.then(result => res.send("It has been updated")) // confirm if it has been added or not.
.catch((e) => console.log(e));
}

else{
res.send("Cannot write data, please fill in the missing fields")
}
});

// Add a new DELETE endpoint /orders/:orderId to delete an existing order along all the associated order items
app.delete("/orders/:orderId", function (req, res) {
if (!req.body) {
return res.status(400).send("Body not found");
}

let order_id = req.params.orderId;

if(typeof (order_id) == "undefined" )
{
res.send("missing Id");
}
const Query="DELETE FROM orders WHERE id=$1";
pool.query(Query, [order_id])
.then(result => res.send("Order deleted"))
.catch((e) => console.log(e))
});

// Add a new DELETE endpoint /customers/:customerId to delete an existing customer only if this customer doesn't have orders
app.delete("/customers/:customerId", function (req, res) {
if (!req.body) {
return res.status(400).send("Body not found");
}

let customer_id = req.params.customerId;

if(typeof customer_id == "undefined" )
{
res.send("Missing Id");
}

pool
.query("SELECT * FROM orders WHERE customer_id =$1", [customer_id])
.then((result) => {
if (result.rows.length) {
res.send("This customer has orders, we can not delete")
}});
const Query="DELETE FROM customers WHERE id=$1";
pool.query(Query,[customer_id])
.then(result => res.send("Deleted customer with no orders"))
.catch((e) => console.log(e))
});

// Add a new GET endpoint /customers/:customerId/orders to load all the orders along the items in the orders of a specific customer
// Especially, the following information should be returned: order references, order dates, product names, unit prices, suppliers and quantities.
app.get("/customers/:customerId/orders", function (req, res) {

let customer_id=req.params.customerId;

if(typeof (customer_id) == "undefined" )
{
res.send("Missing Id");
}
const Query="SELECT order_reference, order_date, unit_price, supplier_name, quantity FROM orders INNER JOIN order_items ON order_items.order_id=orders.id INNER JOIN products ON products.id= order_items.product_id INNER JOIN suppliers ON suppliers.id=products.supplier_id WHERE customer_id=$1";
pool.query(Query,[customer_id])
.then(result => res.send(result.rows))
.catch((e) => console.log(e))
});

app.listen(3001,function(){
console.log("Listening at port 3001");
});
15 changes: 15 additions & 0 deletions week-2/mandatory/3-api/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions week-2/mandatory/3-api/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading