generated from HackYourFuture-CPH/hyf-assignment-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
andrpavl
wants to merge
5
commits into
main
Choose a base branch
from
node-week2/AndriiP
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Node week2/andrii p #214
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
09a13ec
DB connected finally) and homework is done
andrpavl de57991
homework week2 added
andrpavl ee7643a
general update. Edited files location
andrpavl d0a9bd2
required updates are done
andrpavl 87ff9c7
warmup Homework, week 2 of node
andrpavl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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