From ff79e78e0614aa6a225db08e12d180d5992bba0e Mon Sep 17 00:00:00 2001 From: Mareh Date: Wed, 1 Apr 2026 23:54:50 +0200 Subject: [PATCH] week 12 --- task-1/books_library.db | Bin 24576 -> 24576 bytes task-1/queries.sql | 33 +++++++++++--- task-2/data/data.json | 91 -------------------------------------- task-2/data/flashcards.db | Bin 0 -> 16384 bytes task-2/migrate.js | 28 ++++++++++++ task-2/setup.sql | 17 +++++++ task-2/src/database.js | 81 +++++++++++++++++++++++---------- task-2/src/storage.js | 4 +- 8 files changed, 132 insertions(+), 122 deletions(-) delete mode 100644 task-2/data/data.json create mode 100644 task-2/data/flashcards.db create mode 100644 task-2/migrate.js create mode 100644 task-2/setup.sql diff --git a/task-1/books_library.db b/task-1/books_library.db index 88b0418c6cf5291e897c14742bfcd1b1868c352a..5e13b9d629fa3533b9e5b852297263ab485fa8d9 100644 GIT binary patch delta 186 zcmZoTz}Rqrae_1>$3z)tRt^R|oq&xg3;3BOdHzji7cgYv`M0t02#=hMBpZXcyd;xv zVo_>_V^V&4Mq*xSZe(h1YF;M$h0QXu*M#^|xfz+I852uOGV+UxB{vx`3a~ReF~l*P zVVun2u$@(o;U~kH$wwS&IT(OI-;+slvWKH0n@4_8QGU_p0>>$Q0&M&`4E&e*H}X&9 jPvy7g*V!znAi*yzn99Z=$Rx?=;mQ6AqFQpYef&WHby74> delta 130 zcmV-|0Db>}zyW~30gxL31d$v=1q1*tS4y#DpbrBW4IGmP5HYcV do?", - "answer": "Creates a block-level container element", - "learned": false, - "deckId": 3 - }, - { - "id": 10, - "question": "What is the CSS box model?", - "answer": "Every element is a box with content, padding, border, and margin", - "learned": false, - "deckId": 3 - } - ] -} \ No newline at end of file diff --git a/task-2/data/flashcards.db b/task-2/data/flashcards.db new file mode 100644 index 0000000000000000000000000000000000000000..5e1d050846c573e6381b95d38ddddbd8e74534fd GIT binary patch literal 16384 zcmeI3O>g5w7{{HLO`Fiz^wjH90j1TZbkQEr3bG+h-8a&7H%TcKLhZz!CZjrbI^(27 zoXTpCdQSIh_-QC%$X=}?@EmxrKZn|~1 zR&g7k%ha;)S0K0YESXt&_%Lx~2&nS_*UZO?qx;a~ul-HkqVihha>%;8fIU|((OV3uB~>l*CT>1go)o=n;JMt2UYk4lmzMLp^P_X=)SdU8r2U8ell{VO z+Dmpi|0@4;{$>7~`#Ckq6%il;M1Tko0U|&IhyW2F0z}}R5LlW|TOT~x5-}_RKDYa= zFS%5Dx$8w?ETaHgObJhcleQK=-fr$yv0Y?<&#s%C#I6u>;I(lr{(;G54wl|5g-CY zfCvx)B0vO)01+SpM1Tkofw!DMHkn#Tm?Vw4Yyy9p%z}Bn{?FwXE$3IK=RCFlwtulZ zHspW9J^#hIS8w@{$vzPv0z`la5CI}U1c(3;AOin?0=sh=YwdhCaoE8#%|Zdg{q}BG z#HBK*ko8n2iXl3MVbqu4by$cS?4Z~HGs@!#;tmJNdyx`xB!}~@+>BMe%-;ml^|(h5 zt>|0mMm`Tp?tsf7@PO-W`H`Nkyby86Z1iJk0k`pey`^8ke$wZ%04(&O%Vb-GsY*6u zee(5{gdd?B2A}z2@D%*0RFRy;rl=J}-q~uv2RtxwnFujI{z^C1oXuEIzPs_sU?RaC zNA5*IzZ>H4kVzoIGc_J;PYU!ggfl+WR9G0{5G9zj^yKDD#(K=NBb3J4;Wj47Yh#-t zh%uc3WOSiGgy>nP9}IO)1Cqof2aP@SF-0K?liL||=S{ksI2nNM`k}zycya(?iBp04 zcqAP-^Ex{IDE!FL#)FJiJiLk2aI0>m34JY!eM)s6V-Cb%oEHJ7GU}q0>l}%zC}|v* zH=8w6#Ek2tSS+3x;2oX#Z}b(0@?yY5e}*alLs%ZL09}@A8xRkBeC1IJCt4{xilQeP z4q8e!ER{nrpBX&`LYdiSx~kn|#`?5 { + stmt.run(deck.name, deck.description); +}); +const allDecks = db.prepare("SELECT * FROM decks").all(); +console.log(allDecks); +const stmt_card = db.prepare( + "INSERT INTO cards(question,answer,learned,deck_id) VALUES(?,?,?,?)", +); +data.cards.forEach((card) => { + stmt_card.run(card.question, card.answer, card.learned ? 1 : 0, card.deckId); +}); +const allCards = db.prepare("SELECT * FROM cards").all(); +console.log(allCards); + +db.close(); diff --git a/task-2/setup.sql b/task-2/setup.sql new file mode 100644 index 0000000..7c7146e --- /dev/null +++ b/task-2/setup.sql @@ -0,0 +1,17 @@ +DROP TABLE IF EXISTS cards; +DROP TABLE IF EXISTS decks; + +CREATE TABLE decks( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + description TEXT + +); +CREATE TABLE cards( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question TEXT NOT NULL, + answer TEXT NOT NULL, + learned INTEGER NOT NULL DEFAULT 0, + deck_id INTEGER NOT NULL, + FOREIGN KEY(deck_id) REFERENCES decks(id) +); \ No newline at end of file diff --git a/task-2/src/database.js b/task-2/src/database.js index a737fcb..a6a355b 100644 --- a/task-2/src/database.js +++ b/task-2/src/database.js @@ -5,13 +5,12 @@ // // When every function works correctly, `node app.js` should // print exactly the same output as it did with storage.js. - -import Database from 'better-sqlite3'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import Database from "better-sqlite3"; +import { fileURLToPath } from "url"; +import { dirname, join } from "path"; const __dirname = dirname(fileURLToPath(import.meta.url)); -const DB_FILE = join(__dirname, '../data/flashcards.db'); +const DB_FILE = join(__dirname, "../data/flashcards.db"); const db = new Database(DB_FILE); @@ -20,24 +19,37 @@ const db = new Database(DB_FILE); // ---------------------------------------------------------------- export function getAllDecks() { - // TODO: return all rows from the decks table - throw new Error('Not implemented'); + const stmt = db.prepare("SELECT * FROM decks"); + const rows = stmt.all(); + return rows; + //throw new Error('Not implemented'); } export function getDeckById(id) { - // TODO: return the deck row with the given id, or null if not found - throw new Error('Not implemented'); + const stmt = db.prepare("SELECT * FROM decks WHERE id = ? "); + const getDeck = stmt.get(id); + return getDeck ?? null; + //throw new Error('Not implemented'); } export function addDeck(name, description) { - // TODO: insert a new deck and return the new row (including its id) - throw new Error('Not implemented'); + const stmt = db.prepare( + "INSERT INTO decks (name, description) VALUES (?, ?)", + ); + const add_deck = stmt.run(name, description); + return { + id: add_deck.lastInsertRowid, + name, + description, + }; + //throw new Error('Not implemented'); } export function deleteDeck(deckId) { - // TODO: delete the deck with the given id - // return true if a row was deleted, false otherwise - throw new Error('Not implemented'); + const stmt = db.prepare("DELETE FROM decks WHERE id = ?"); + const deleteDeck = stmt.run(deckId); + return deleteDeck.changes > 0; + //throw new Error('Not implemented'); } // ---------------------------------------------------------------- @@ -45,23 +57,44 @@ export function deleteDeck(deckId) { // ---------------------------------------------------------------- export function getAllCardsForDeck(deckId) { - // TODO: return all card rows whose deckId matches - throw new Error('Not implemented'); + const stmt = db.prepare( + "SELECT id, question, answer, learned, deck_id AS deckId FROM cards WHERE deck_id = ?", + ); + const getAll = stmt.all(deckId); + return getAll; + //throw new Error('Not implemented'); } export function addCard(question, answer, deckId) { - // TODO: insert a new card and return the new row (including its id) - throw new Error('Not implemented'); + const stmt = db.prepare( + "INSERT INTO cards (question, answer ,deck_id) VALUES (?, ?,?)", + ); + const added_card = stmt.run(question, answer, deckId); + return { + id: added_card.lastInsertRowid, + question, + answer, + learned: 0, + deckId, + }; + //throw new Error('Not implemented'); } export function markCardLearned(cardId) { - // TODO: set learned = 1 for the card with the given id - // return the updated row, or null if not found - throw new Error('Not implemented'); + const stmt = db + .prepare("UPDATE cards SET learned = 1 WHERE id = ?") + .run(cardId); + const updatedCard = db + .prepare( + "SELECT id, question, answer, learned, deck_id AS deckId FROM cards WHERE id = ?", + ) + .get(cardId); + return updatedCard ?? null; } export function deleteCard(cardId) { - // TODO: delete the card with the given id - // return true if a row was deleted, false otherwise - throw new Error('Not implemented'); + const stmt = db.prepare("DELETE FROM cards WHERE id = ?"); + const deleteCard = stmt.run(cardId); + return deleteCard.changes > 0; + //throw new Error('Not implemented'); } diff --git a/task-2/src/storage.js b/task-2/src/storage.js index 4e59b53..3307669 100644 --- a/task-2/src/storage.js +++ b/task-2/src/storage.js @@ -2,11 +2,11 @@ // This file handles all reading and writing of data. // Currently it uses a JSON file on disk. // In the assignment you will replace each function here with a SQLite query. - +import Database from 'better-sqlite3'; import { readFileSync, writeFileSync } from 'fs'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; - +const db = new Database(join(__dirname, '../flashcards.db')); const __dirname = dirname(fileURLToPath(import.meta.url)); const DATA_FILE = join(__dirname, '../data/data.json');