Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs week3/parisa #232

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions class30-homework
Submodule class30-homework added at e774a9
46 changes: 46 additions & 0 deletions databases/week1/database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
USE hyf_lesson1

SELECT COUNT(*) AS total_tasks FROM task;

SELECT COUNT(*) AS invalid_due_dates
FROM task
WHERE due_date IS NULL

SELECT *
FROM task
WHERE status_id = (SELECT id FROM status WHERE name = 'Done');

SELECT *
FROM task
WHERE status_id != (SELECT id FROM status WHERE name = 'Done');

SELECT *
FROM task
ORDER BY created DESC;

SELECT *
FROM task
ORDER BY created DESC
LIMIT 1;

SELECT title, due_date
FROM task
WHERE title LIKE '%database%'
OR description LIKE '%database%';

SELECT task.title, status.name AS status
FROM task
JOIN status ON task.status_id = status.id;

SELECT status.name, COUNT(task.id) AS task_count
FROM status
LEFT JOIN task ON status.id = task.status_id
GROUP BY status.name;

SELECT status.name, COUNT(task.id) AS task_count
FROM status
LEFT JOIN task ON status.id = task.status_id
GROUP BY status.name
ORDER BY task_count DESC;


Binary file added databases/week2/Screenshot 2025-02-12 185047.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions databases/week2/database_animal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE TABLE Farmers (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);

CREATE TABLE Barns (
id INT PRIMARY KEY AUTO_INCREMENT,
location VARCHAR(255) NOT NULL,
capacity INT NOT NULL
);

CREATE TABLE Animals (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
species VARCHAR(100) NOT NULL,
age INT NOT NULL,
barn_id INT,
FOREIGN KEY (barn_id) REFERENCES Barns(id) ON DELETE SET NULL
);

CREATE TABLE Farmer_Animal (
farmer_id INT,
animal_id INT,
PRIMARY KEY (farmer_id, animal_id),
FOREIGN KEY (farmer_id) REFERENCES Farmers(id) ON DELETE CASCADE,
FOREIGN KEY (animal_id) REFERENCES Animals(id) ON DELETE CASCADE
);
23 changes: 23 additions & 0 deletions databases/week2/database_school.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE DATABASE School_database;
USE School_database;

CREATE TABLE classes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
begins DATE NOT NULL,
ends DATE NOT NULL,
status ENUM('not-started', 'ongoing', 'finished') NOT NULL DEFAULT 'not-started'
);

CREATE TABLE Students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(20) NOT NULL,
classes_id INT,
FOREIGN KEY (classes_id) REFERENCES classes(id) ON DELETE SET NULL
);

CREATE INDEX idx_student_name ON Students(name);

SELECT* FROM Students;
43 changes: 43 additions & 0 deletions databases/week2/database_tasks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
USE hyf_lesson2;

insert into task (title, description, created, updated, due_date, status_id) values ('Wash dishes', 'This is in priority.', '2025-02-11 06:54:16', '2025-02-11 06:54:16', '2025-02-12', 1);

UPDATE task
SET title = 'Go Playground'
WHERE id = 5;

UPDATE task
SET due_date = '2025-02-25'
WHERE id = 6;

UPDATE task
SET status_id = 2
WHERE id = 7;

UPDATE status
SET name = 'Completed'
WHERE id = 8;

DELETE FROM task
WHERE id = 9;

SELECT* FROM task;

SELECT *
FROM task
WHERE email LIKE '%@spotify.com';

SELECT *
FROM task
WHERE assigned_to = 'Donald Duck'
AND status = 'Not started';

SELECT *
FROM task
WHERE assigned_to = 'Maryrose Meadows'
AND MONTH(created) = 9;

SELECT MONTH(created) AS month, COUNT(*) AS task_count
FROM tasks
GROUP BY MONTH(created)
ORDER BY month;
140 changes: 140 additions & 0 deletions databases/week3/database.meal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
CREATE DATABASE MealReservationDB;
USE MealReservationDB;

CREATE TABLE Meal (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
location VARCHAR(255),
`when` DATETIME NOT NULL,
max_reservations INT,
price DECIMAL(10,2),
created_date DATE NOT NULL
);

CREATE TABLE Reservation (
id INT AUTO_INCREMENT PRIMARY KEY,
number_of_guests INT NOT NULL,
meal_id INT,
created_date DATE NOT NULL,
contact_phonenumber VARCHAR(20),
contact_name VARCHAR(255),
contact_email VARCHAR(255),
FOREIGN KEY (meal_id) REFERENCES Meal(id) ON DELETE CASCADE
);

CREATE TABLE Review (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
meal_id INT,
stars INT CHECK (stars BETWEEN 1 AND 5),
created_date DATE NOT NULL,
FOREIGN KEY (meal_id) REFERENCES Meal(id) ON DELETE CASCADE
);

SELECT * FROM Meal;

INSERT INTO Meal (title, description, location, `when`, max_reservations, price, created_date)
VALUES
('Pasta Carbonara', 'Italian pasta with creamy sauce', 'Rome, Italy', '2025-03-01 18:30:00', 20, 85.50, '2025-02-18');
INSERT INTO Meal (title, description, location, `when`, max_reservations, price, created_date)
VALUES ('Kebab Koobideh', 'Grilled minced lamb kebabs served with saffron rice', 'Tehran, Iran', '2025-03-05 19:00:00', 20, 75.00, '2025-02-18'),

('Ghormeh Sabzi', 'Persian herb stew with lamb and kidney beans', 'Shiraz, Iran', '2025-03-10 20:00:00', 15, 85.00, '2025-02-18'),

('Fesenjan', 'Rich pomegranate and walnut stew with chicken', 'Isfahan, Iran', '2025-03-15 19:30:00', 12, 95.00, '2025-02-18'),

('Zereshk Polo ba Morgh', 'Saffron rice with barberries and slow-cooked chicken', 'Mashhad, Iran', '2025-03-20 18:00:00', 25, 70.00, '2025-02-18'),

('Baghali Polo', 'Dill and fava bean rice served with tender lamb shank', 'Tabriz, Iran', '2025-03-25 19:00:00', 18, 100.00, '2025-02-18');

SELECT * FROM Meal WHERE id = 1;

UPDATE Meal
SET title = 'Spaghetti Bolognese', price = 95.00
WHERE id = 1;

DELETE FROM Meal WHERE id = 1;

ALTER TABLE Meal AUTO_INCREMENT = 1;
INSERT INTO Meal (id, title, description, location, `when`, max_reservations, price, created_date)
VALUES
(1, 'Pasta Carbonara', 'Italian pasta with creamy sauce', 'Rome, Italy', '2025-03-01 18:30:00', 20, 85.50, '2025-02-18');

SELECT * FROM Reservation;

INSERT INTO Reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES (4, 1, '2025-02-19', '50108318', 'Parisa Mousavi', '[email protected]');

INSERT INTO Reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES
(3, 2, '2025-02-19', '9123456789', 'Ali Rezai', '[email protected]'),

(2, 3, '2025-02-19', '9356781234', 'Sara Mohammadi', '[email protected]'),

(5, 4, '2025-02-19', '9012345678', 'Hossein Gholami', '[email protected]'),

(4, 5, '2025-02-19', '9387654321', 'Leila Khosravi', '[email protected]'),

(6, 1, '2025-02-19', '9154326789', 'Reza Farhadi', '[email protected]');


SELECT * FROM Reservation WHERE id = 1;

UPDATE Reservation
SET number_of_guests = 6, contact_name = 'Milad Zamani'
WHERE id = 1;

DELETE FROM Reservation WHERE id = 1;

SELECT * FROM Review;

INSERT INTO Review (title, description, meal_id, stars, created_date)
VALUES ('Delicious meal', 'The pasta was amazing!', 1, 5, '2025-02-19');
INSERT INTO Review (title, description, meal_id, stars, created_date)
VALUES
('Amazing Kebab', 'The Koobideh was juicy and full of flavor!', 2, 5, '2025-02-19'),

('Authentic Taste', 'Ghormeh Sabzi tasted just like home!', 3, 4, '2025-02-19'),

('Rich and Delicious', 'Fesenjan had the perfect balance of sweet and sour.', 4, 5, '2025-02-19'),

('Perfectly Cooked', 'The chicken in Zereshk Polo was so tender!', 5, 4, '2025-02-19'),

('Aromatic and Flavorful', 'Baghali Polo with lamb shank was amazing.', 6, 5, '2025-02-19');


SELECT * FROM Review WHERE id = 1;

UPDATE Review
SET title = 'Amazing experience', stars = 4
WHERE id = 1;

DELETE FROM Review WHERE id = 1;

SELECT * FROM Meal WHERE price < 90;

SELECT * FROM Meal
WHERE max_reservations > (
SELECT COALESCE(SUM(number_of_guests), 0) FROM Reservation WHERE Meal.id = Reservation.meal_id
);

SELECT * FROM Meal WHERE title LIKE '%Rød grød med%';

SELECT * FROM Meal WHERE created_date BETWEEN '2025-01-01' AND '2025-02-18';

SELECT * FROM Meal LIMIT 5;

SELECT DISTINCT Meal.*
FROM Meal
JOIN Review ON Meal.id = Review.meal_id
WHERE Review.stars >= 4;

SELECT * FROM Reservation WHERE meal_id = 1 ORDER BY created_date;

SELECT Meal.*, COALESCE(AVG(Review.stars), 0) AS average_rating
FROM Meal
LEFT JOIN Review ON Meal.id = Review.meal_id
GROUP BY Meal.id
ORDER BY average_rating DESC;
12 changes: 12 additions & 0 deletions nodejs/week1/avg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Excluding "node" and script name
const args = process.argv.slice(2);

// Convert to numbers
const numbers = args.map(Number).filter(n => !isNaN(n));

if (numbers.length>0) {
console.log(numbers.reduce((a, b) => a + b, 0) / numbers.length);

} else {
console.log("Please provide valid numbers.");
}
15 changes: 15 additions & 0 deletions nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express'
import { searchRouter } from "./search.js"
const app = express();
const port = process.env.PORT || 3000;

// Support parsing JSON requests
app.use(express.json());
app.use("/search", searchRouter);
app.get("/", (req, res) => {
res.send("This is a search engine");
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
13 changes: 13 additions & 0 deletions nodejs/week2/documents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"id": 1,
"name": "Used Apple Airpods",
"price": "50",
"description": "Battery life is not great"
},
{
"id": 2,
"type": "doc",
"value": "hello world"
}
]
50 changes: 50 additions & 0 deletions nodejs/week2/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import express from "express";
import documents from "./documents.json" with { type: 'json' };

export const searchRouter = express.Router();

// GET: Fetch all documents or filter by query
searchRouter.get("/", (req, res) => {
const query = req.query.q;

if (!query) {
res.status(200).json(documents);
return
}

const filteredResults = documents.filter(item =>
item.value?.toLowerCase().includes(query.toLowerCase())
);

res.status(200).json(filteredResults);
});

// GET: Fetch a document by ID
searchRouter.get('/:id', (req, res) => {
const id = Number(req.params.id);
const document = documents.find(item => item.id === id);

if (!document) {
res.status(404).json({ message: "Not Found. Check your ID and try again." });
return
}

res.status(200).json(document);
});

// POST: Filter documents based on provided fields
searchRouter.post("/", (req, res) => {
const { fields } = req.body;

if (!fields || typeof fields !== "object") {
res.status(400).json({ message: "Invalid request body. Expected an object with fields." });
return
}

const filteredDocuments = documents.filter(item => {
Object.entries(fields).every(([key, value]) => item[key] === value);
return
});

res.status(200).json(filteredDocuments);
});
2 changes: 2 additions & 0 deletions nodejs/week3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
Loading