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

Node week2/andrii p #214

Open
wants to merge 5 commits into
base: main
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions databases/week1/homework_1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--1. Find out how many tasks are in the task table

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You used good formatting for your SQL code, good job

SELECT COUNT (*)
FROM task

--2. Find out how many tasks in the task table do not have a valid due date

SELECT COUNT (*)
FROM task
WHERE due_date IS NULL

--3. Find all the tasks that are marked as done

SELECT task.title, status.name
FROM task JOIN status ON task.status_id = status.id
WHERE status.name IN ('Done')

--4. Find all the tasks that are not marked as done

SELECT task.title, status.name
FROM task JOIN status ON task.status_id = status.id
WHERE status.name NOT IN ('Done')

--5. Get all the tasks, sorted with the most recently created first

SELECT *
FROM task
ORDER BY created

--6. Get the single most recently created task

SELECT *
FROM task
ORDER BY created
LIMIT 1

--7. Get the title and due date of all tasks where the title or description contains database

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

--8. Get the title and status (as text) of all tasks

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

--9. Get the name of each status, along with a count of how many tasks have that status

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

--10. Get the names of all statuses, sorted by the status with most tasks first

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
74 changes: 74 additions & 0 deletions databases/week2/HW2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- Part 1: Working with tasks

USE hw

INSERT INTO task (title, description, created, updated, due_date, status_id, user_id)
VALUES ('Do your homework', 'You need to complete your homework, consists of 4 parts', '2025-02-09', '2025-02-11', '2025-02-16', 2, 5 )

UPDATE task
SET title = 'Do your hw and class preparation',
due_date = '2025-02-14 22:30:00',
status_id = 3
WHERE id = 36

DELETE FROM task
WHERE id = 36

SELECT * from task

-- Part 2: School database

USE DB_HW2

CREATE TABLE `class` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`begins` DATETIME NOT NULL,
`ends` DATETIME NULL
);

CREATE TABLE `student` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR (255) NOT NULL,
`email` VARCHAR(255) NULL,
`phone` VARCHAR(255) NULL,
`class_id` int(10) UNSIGNED NOT NULL,
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`) ON DELETE SET NULL
);

CREATE INDEX idx_student_name
ON student (name);

ALTER TABLE class
ADD status ENUM('not-started', 'ongoing', 'finished') NOT NULL

-- Part 3: More queries

USE hw

SELECT *
FROM task
JOIN user_task ON task.id = user_task.task_id
JOIN user ON user_task.user_id = user.id
WHERE user.email LIKE '%@spotify.com';

SELECT *
FROM task
JOIN user_task ON task.id = user_task.task_id
JOIN user ON user_task.user_id = user.id
JOIN status ON task.status_id = status.id
WHERE user.name = 'Donald Duck'
AND status.name = 'Not started';

SELECT *
FROM task
JOIN user_task ON task.id = user_task.task_id
JOIN user ON user_task.user_id = user.id
JOIN status ON task.status_id = status_id
WHERE user.name = 'Maryrose Meadows'
AND MONTH(created) = 9

SELECT YEAR(created) AS year, MONTH(created) AS month, COUNT(*) AS task_count
FROM task
GROUP BY YEAR(created), MONTH(created)
ORDER BY year, month;
Binary file added databases/week2/factory_db.pdf
Binary file not shown.
72 changes: 72 additions & 0 deletions databases/week2/task4CreatingDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
CREATE DATABASE `factory_db`
DEFAULT CHARACTER SET = 'utf8mb4';

USE factory_db

CREATE TABLE `departments` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR (50) NOT NULL
);

INSERT INTO departments (name)
VALUES ('Compact'), ('Maxi'), ('Lager');

CREATE TABLE `duties` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`duty_name` VARCHAR(50) NOT NULL UNIQUE,
`description` TEXT
);

INSERT INTO duties (duty_name, description) VALUES
('Quality Control', 'Inspect products for defects'),
('Employee Hiring', 'Find more employees'),
('Inventory Management', 'Monitor stock levels');

CREATE TABLE `shifts` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`shift_name` ENUM('morning', 'afternoon', 'night') NOT NULL
);

INSERT INTO shifts (shift_name)
VALUES ('morning'), ('afternoon'), ('night');

CREATE TABLE `staff` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`depart_id` INT UNSIGNED NOT NULL,
`shift_id` INT UNSIGNED NOT NULL,
FOREIGN KEY(`depart_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE,
FOREIGN KEY(`shift_id`) REFERENCES `shifts` (`id`) ON DELETE CASCADE
);

INSERT INTO staff (name, depart_id, shift_id) VALUES
('Jens Jakobsen', 1, 2),
('Andrii Pavliuk', 2, 3),
('Lene Vestergaard', 3, 1);

CREATE TABLE staff_duties (
staff_id INT UNSIGNED,
duty_id INT UNSIGNED,
created DATETIME NOT NULL,
PRIMARY KEY (staff_id, duty_id),
FOREIGN KEY (staff_id) REFERENCES staff(id) ON DELETE CASCADE,
FOREIGN KEY (duty_id) REFERENCES duties(id) ON DELETE CASCADE
);

INSERT INTO staff_duties (staff_id, duty_id, created) VALUES
(1, 1, '2025-02-13'),
(2, 2, '2025-02-14'),
(3, 3, '2025-02-15');

SELECT staff.name AS Employee, staff_duties.created, duties.duty_name, duties.description
FROM staff
JOIN staff_duties ON staff.id = staff_id
JOIN duties ON staff_duties.duty_id = duties.id

SELECT
staff.name AS Employee,
departments.name AS Department,
shifts.shift_name AS Shift
FROM staff
JOIN departments ON staff.depart_id = departments.id
JOIN shifts ON staff.shift_id = shifts.id;
1 change: 0 additions & 1 deletion javascript/javascript1/week1/readme.md

This file was deleted.

1 change: 0 additions & 1 deletion javascript/javascript1/week2/readme.md

This file was deleted.

1 change: 1 addition & 0 deletions nodejs/week2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
79 changes: 79 additions & 0 deletions nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import express from "express";
import fs from "fs";

const docs = JSON.parse(fs.readFileSync("documents.json", "utf-8"));

const app = express();
const port = process.env.PORT || 3000;

// Support parsing JSON requests
app.use(express.json());

app.get("/", (req, res) => {
res.send(docs);
});

app.get("/search", (req, res) => {
const { q } = req.query;

const filteredDocs = docs.filter((doc) =>
Object.values(doc).some(
(value) =>
typeof value === "string" &&
value.toLowerCase().includes(q.toLowerCase())
)
);

!q ? res.json(docs) : res.json(filteredDocs);
});

app.get("/documents/:id", (req, res) => {
const { id } = req.params;

const document = docs.find((doc) => doc.id === Number(id));

!document
? res.status(404).json({ message: "Document not found." })
: res.json(document);
});

app.post("/search", (req, res) => {
const { q } = req.params;
const { fields } = req.body;

if (q && fields) {
return res
.status(400)
.json({
error: "Cannot provide both query parameter and fields in body",
});
}

if (q && fields) {
return res.status(400).json({ error: "Both q and fields cannot be provided together" });
}

if (q) {
const filteredDocs = docs.filter((doc) =>
Object.values(doc).some(
(value) =>
typeof value === "string" &&
value.toLowerCase().includes(q.toLowerCase())
)
);
return res.json(filteredDocs);
}

if (fields) {
const filteredDocs = docs.filter((doc) =>
Object.entries(fields).every(([key, value]) => doc[key] === value)
);
return res.json(filteredDocs);
}

res.json(docs);
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
13 changes: 13 additions & 0 deletions nodejs/week2/documents.json
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"
}
]
Loading