From 14df9a4e48dd091c75b9153180e43bcb28ad3d23 Mon Sep 17 00:00:00 2001 From: NihalAli2 <62475392+NihalAli2@users.noreply.github.com> Date: Sat, 21 Nov 2020 18:03:56 +0000 Subject: [PATCH 1/3] classes-db-Done --- week-1/mandatory/2-classes-db/task.md | 94 ++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index be8b7cf0..8bb1d79e 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -8,6 +8,81 @@ To submit this homework write the correct commands for each question here: ```sql +CREATE DATABASE cyf_classes; + +//Create the mentors table: + +CREATE TABLE mentors ( +id SERIAL PRIMARY KEY, name varchar(200) NOT NULL, years_in_Glasgow INT(100) NOT NULL, address varchar(300) NOT NULL, favourate_programming_language varchar(100) NOT NULL +); + +//Insert the mentors data: + +INSERT INTO mentors values(1,'ola', 10, 'London', 'JavaScript'), +(2, 'Donald', 4, 'glasgow', 'C'), +(3,'Martin', 11, 'Birmingham', 'Java'), +(4,'Sanna', 6, 'London', 'C'), +(5,'Hind', 1, 'Bristol', 'C'); + +//Create the student table: + +CREATE TABLE student ( +name varchar (100) NOT NULL, address varchar(300) NOT NULL, graduated BOOLEAN NOT NULL + +); + +// Insert student data: + +INT INTO student values ('Noor', 'Ladywood' , False), +('Lama', 'Birmingham' , False), +('Nihal', 'Manchester' , True), +('Omar', 'London' , True), +('Sozan', 'London' , False), +('Dorman', 'Darby' , False), +('Soliman', 'Lister' , True), +('Sammar', 'Lister' , False), +('Hossain', 'London' , True), +('Ali', 'Birmingham' , True), +('Rana', 'Birmingham' , False); + +// Select from the student table: + +select\*from students; + + +// Create classes table: + +CREATE TABLE classes ( +id SERIAL PRIMARY KEY, leading_mentor varchar(100), topic varchar(300) primary key, class_date date, class_location varchar(300) + +); + +//Insert the data: + +INSERT INTO classes values(1,'Noorhan', 'C++', '01-02-2021', 'center building'), +(2, 'Ali', 'Node JS', '01-02-2021', 'main tower'), +(3, 'Susan', 'JavaScrept', '28-08-2020', ' main tower'), +(4, 'Ala', 'C++', '01-12-2020', 'CLT building'), +(5, 'Flora', 'Java', '01-03-2020', 'center building'), +(6, 'Mosa', 'C++', '20-05-2020', ' center building'); + +g +//9-Create table to show students attends: + +CREATE TABLE student_attends (student_id INT REFERENCES student(id),class_id INT REFERENCES classes(id)); +INSERT INTO student_attends(student_id,class_id)values +(0,1), +(8,1), +(3,3) +(1,0); + +//select from mentor: +select * from mentor where years_in_Glasgow > 5; +select * from mentor where favourate_programming_language = 'JavasCript'; +select * from student where graduated = True; +select * from classes where class_date < '2020-06-01'; +select * from student_attends where topic = 'Node JS'; + ``` @@ -16,22 +91,23 @@ 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) -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. +2. Create a new table `mentors`, for each mentor we want to save their name, how many years they lived in mentors, 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. 5. Insert 10 students in the `students` table. 6. Verify that the data you created for mentors and students are correctly stored in their respective tables (hint: use a `select` SQL statement). 7. Create a new `classes` table to record the following information: - - A class has a leading mentor - - A class has a topic (such as Javascript, NodeJS) - - A class is taught at a specific date and at a specific location +- A class has a leading mentor +- A class has a topic (such as Javascript, NodeJS) +- A class is taught at a specific date and at a specific location 8. Insert a few classes in the `classes` table 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 - - Retrieve all the mentors whose favourite language is Javascript - - Retrieve all the students who are CYF graduates - - Retrieve all the classes taught before June this year - - 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). +- Retrieve all the mentors who lived more than 5 years in Glasgow +- Retrieve all the mentors whose favourite language is Javascript +- Retrieve all the students who are CYF graduates +- Retrieve all the classes taught before June this year +- 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). + From b678c975768cd3d3aa1b66378a866822a58e434d Mon Sep 17 00:00:00 2001 From: NihalAli2 <62475392+NihalAli2@users.noreply.github.com> Date: Tue, 1 Dec 2020 16:27:17 +0000 Subject: [PATCH 2/3] 2-ecommerce-db.done --- week-2/mandatory/2-ecommerce-db/task.md | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index 70e9ce0c..01dfa7f9 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -9,6 +9,33 @@ Below you will find a set of tasks for you to complete to set up a databases of To submit this homework write the correct commands for each question here: ```sql +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- SELECT * FROM products ORDER BY unit_price DESC LIMIT 5; +6-SELECT products.product_name, products.unit_price, suppliers.supplier_name FROM products + INNER JOIN suppliers ON suppliers.id = products.supplier_id; +7-SELECT products.product_name,suppliers.supplier_name From products + INNER JOIN suppliers ON suppliers.id = products.supplier_id + WHERE country = 'United Kingdom'; +8-SELECT * FROM orders WHERE customer_id = '1'; +9-SELECT customers.name,customers.id, orders.order_reference FROM orders + INNER JOIN customers ON customers.id = orders.customer_id + where customers.name = 'Hope Crosby'; +10-SELECT products.product_name, products.unit_price, order_items.quantity, +11- SELECT customers.name, orders.order_reference, orders.order_date , products.product_name, suppliers.supplier_name, order_items.quantity from orders + INNER JOIN order_items ON order_items.id = orders.id + INNER JOIN products ON products.id = order_items.product_id + INNER JOIN suppliers ON suppliers.id = products.supplier_id + INNER JOIN customers ON customers.id = orders.customer_id; + +12- SELECT customers.name, suppliers.country FROM orders + INNER JOIN order_items ON order_items.id = orders.id + INNER JOIN products ON products.id = order_items.product_id + INNER JOIN suppliers ON suppliers.id = products.supplier_id + INNER JOIN customers ON customers.id = orders.customer_id + WHERE suppliers.country = 'China'; ``` From e0aa73d30ec27aee330d4a39f11aef63af337e39 Mon Sep 17 00:00:00 2001 From: NihalAli2 <62475392+NihalAli2@users.noreply.github.com> Date: Tue, 1 Dec 2020 16:41:08 +0000 Subject: [PATCH 3/3] Done --- week-2/mandatory/3-api/package.json | 19 ++++++++++++++++ week-2/mandatory/3-api/sever.js | 34 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 week-2/mandatory/3-api/package.json create mode 100644 week-2/mandatory/3-api/sever.js diff --git a/week-2/mandatory/3-api/package.json b/week-2/mandatory/3-api/package.json new file mode 100644 index 00000000..a8d077ae --- /dev/null +++ b/week-2/mandatory/3-api/package.json @@ -0,0 +1,19 @@ +{ + "name": "cyf-ecomerce-api", + "version": "1.0.0", + "description": "", + "main": "server.js", + "dependencies": { + "dotenv": "^8.2.0", + "express": "^4.17.1", + "pg": "^8.5.1" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "author": "", + "license": "ISC" + } + \ No newline at end of file diff --git a/week-2/mandatory/3-api/sever.js b/week-2/mandatory/3-api/sever.js new file mode 100644 index 00000000..cbf83f26 --- /dev/null +++ b/week-2/mandatory/3-api/sever.js @@ -0,0 +1,34 @@ +const express = require("express"); +const app = express(); +const { Pool } = require('pg'); +require('dotenv').config() + +const pool = new Pool({ + user: process.env.USERNAME, + host: 'localhost', + database: 'cyf_ecommerce', + password: process.env.PASSWORD, + port: 5432 +}); + +app.get("/customers", function(req, res) { + pool.query('SELECT * FROM customers', (error, result) => { + res.json(result.rows); + }); + }); + + app.get("/suppliers", function(req, res) { + pool.query('SELECT * FROM suppliers', (error, result) => { + res.json(result.rows); + }); +}); + +app.get("/products", function(req, res) { + pool.query('SELECT products.product_name, suppliers.supplier_name FROM products INNER JOIN suppliers ON suppliers.id = products.supplier_id', (error, result) => { + res.json(result.rows); + }); +}); + +app.listen(3000, function() { + console.log("Server is listening on port 3000. Ready to accept requests!"); +});