Shadi A.#3
Conversation
…ase functions - Added sqlite3 as a dependency in package.json. - Created setup.sql to define decks and cards tables. - Implemented database functions in database.js to manage decks and cards: - getAllDecks: retrieves all decks. - getDeckById: retrieves a specific deck by ID. - addDeck: inserts a new deck and returns it. - deleteDeck: removes a deck by ID. - getAllCardsForDeck: retrieves all cards for a specific deck. - addCard: inserts a new card and returns it. - markCardLearned: marks a card as learned. - deleteCard: removes a card by ID.
📝 HackYourFuture auto gradeAssignment Score: 0 / 100 ✅Status: ✅ Passed Test Details |
remarcmij
left a comment
There was a problem hiding this comment.
Hi @shmoonwalker, there are some minor issues with task-1. In task 2, I see some inconsistencies in how you wrote some of the code. Please check my comments below.
| -- **Question 3** — Show every book in the database along with its author's full name. Combine `first_name` and `last_name` into a single column called `author`. _(Hint: you will need a JOIN.)_ | ||
|
|
||
| SELECT a.first_name || ' ' || a.last_name AS author, b.title, b.genre, b.published_year FROM authors a | ||
| JOIN books b ON a.id = b.author_id |
There was a problem hiding this comment.
The required terminating semicolon is missing.
| JOIN books b ON a.id = b.author_id | |
| JOIN books b ON a.id = b.author_id; |
|
|
||
| SELECT a.first_name || ' ' || a.last_name AS author, b.title, b.published_year FROM authors a | ||
| JOIN books b ON a.id = b.author_id | ||
| WHERE author = 'Stephen King' ORDER BY published_year DESC; |
There was a problem hiding this comment.
For better readability, place the ORDER BY clause on a new line.
| WHERE author = 'Stephen King' ORDER BY published_year DESC; | |
| WHERE author = 'Stephen King' | |
| ORDER BY published_year DESC; |
Note that Question 4 does not mention that the author's name should be shown. So the following SQL may be more appropriate:
SELECT b.title, b.published_year FROM authors a
JOIN books b ON a.id = b.author_id
WHERE a.first_name = "Stephen" AND a.last_name = "King"
ORDER BY published_year DESC;|
|
||
| INSERT INTO books (title, published_year,genre, author_id) VALUES | ||
| ('The Story of My Cats', 2023, 'Biography', | ||
| (SELECT id FROM authors WHERE first_name = 'Shadi' AND last_name = 'Abedinpour')); |
| WHERE title = 'The Dark Tower: The Gunslinger'; | ||
| SELECT * FROM books WHERE title = 'The Dark Tower: The Gunslinger'; |
There was a problem hiding this comment.
Use consistent indentation:
| WHERE title = 'The Dark Tower: The Gunslinger'; | |
| SELECT * FROM books WHERE title = 'The Dark Tower: The Gunslinger'; | |
| WHERE title = 'The Dark Tower: The Gunslinger'; | |
| SELECT * FROM books WHERE title = 'The Dark Tower: The Gunslinger'; |
|
|
||
| -- **Question 8** — Delete the book you added in Question 6. Make sure your query targets only that specific row. | ||
| DELETE FROM books | ||
| WHERE id = 101 |
There was a problem hiding this comment.
- Rather than hard-coding the
id, you could use a subquery here too. - The required terminating semicolon is missing.
| WHERE id = 101 | |
| WHERE id = 101; |
| -- **Bonus B** — Find any authors in the database who have no books at all. _(Hint: you will need a LEFT JOIN and check for NULL.) | ||
| SELECT a.first_name || ' ' || a.last_name AS author FROM authors a | ||
| LEFT JOIN books b ON a.id = b.author_id | ||
| WHERE b.id IS NULL; No newline at end of file |
There was a problem hiding this comment.
The full name is not required. So this alternative would also be fine:
SELECT a.first_name, a.last_name FROM authors a
LEFT JOIN books b ON a.id = b.author_id
WHERE b.id IS NULL;| @@ -0,0 +1,34 @@ | |||
| import fs from "fs"; | |||
| import sqlite3 from "sqlite3"; | |||
There was a problem hiding this comment.
Unfortunately, this code doesn't work at all. The sqlite3 npm package is deprecated, see: https://www.npmjs.com/package/sqlite3. The db.run() function in this deprecated package is asynchronous and consequently produces this error:
[Error: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
Emitted 'error' event on Statement instance at:
] {
errno: 19,
code: 'SQLITE_CONSTRAINT'
}
This makes me wonder how you managed to create a populated flashcards.db file, with migrate.js being defective.
| "dependencies": { | ||
| "better-sqlite3": "^12.8.0" | ||
| "better-sqlite3": "^12.8.0", | ||
| "sqlite3": "^6.0.1" |
There was a problem hiding this comment.
Why did you add the deprecated sqlite3 dependency? You only need better-sqlite3.
| } | ||
| const info = db.prepare("DELETE FROM cards WHERE id = ?").run(cardId); | ||
| return info.changes > 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
Looks perfect. Perhaps too perfect to be completely your own work?
Finished tasks for week 12