From b1c30fbcc11fa71361606c9fd58adaa415af6e26 Mon Sep 17 00:00:00 2001 From: Dagim02 Date: Wed, 1 Apr 2026 17:26:36 +0200 Subject: [PATCH 1/2] complete: Task1 & 2. --- package.json | 21 ++ task-1/books_library.db | Bin 24576 -> 24576 bytes task-1/queries.sql | 50 +++- task-2/data/flashcards.db | Bin 0 -> 16384 bytes task-2/migrate.js | 35 +++ task-2/package-lock.json | 467 ++++++++++++++++++++++++++++++++++++++ task-2/setup.sql | 16 ++ task-2/src/database.js | 90 +++++++- 8 files changed, 662 insertions(+), 17 deletions(-) create mode 100644 package.json create mode 100644 task-2/data/flashcards.db create mode 100644 task-2/migrate.js create mode 100644 task-2/package-lock.json create mode 100644 task-2/setup.sql diff --git a/package.json b/package.json new file mode 100644 index 0000000..80aeacc --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "c55-core-week-12", + "version": "1.0.0", + "description": "The week 12 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-12-assignment", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Unlock7/c55-core-week-12.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "module", + "bugs": { + "url": "https://github.com/Unlock7/c55-core-week-12/issues" + }, + "homepage": "https://github.com/Unlock7/c55-core-week-12#readme" +} diff --git a/task-1/books_library.db b/task-1/books_library.db index 88b0418c6cf5291e897c14742bfcd1b1868c352a..9c03a1e25fe12041ed07420a5fbc77c47fdddf9b 100644 GIT binary patch delta 256 zcmV+b0ssDhzyW~30gxL32$38^1qc8xUmdYzpbrBX4EmD?5HSJ_`muq)3?d*I1^^i! z9|A~WWMpY`L2PAhVR>a}bwzY&X>V|8VQvR@vm71P5)Nex0RtNWVRdw9Z*p@PvM~P- z2r>--001%w29pOs#RLEV2RgBVHV3l?Nd5<2G6)6$4-yXG4!aJN4t@?|4m%DC4c!f~ z4S@|$4H*pL46O`y3{wm(3=IqG3&9JR3vvri3n~it3b_h}3TFyE3LXjq3Fry736BYF z2|x)C2%S0JxRu%@maIcLi3;3BOcx5KD3m9%}e8RI?M)sNzUkW!PvovF3 zX-P(YQL)4(ga7;-@w^NS4Dk$SCbK&noy-7!!Cb3N^ZKW{Y9q@?tu9;bz zn4VD2Jyq%-&_hmD|BKxE7gSa6Jy+@_m%bUmEU2nDAVrbhO8aYe=gs$hX7;f2-n5@L zM8x4BlzkS#6YD)Ik(l`$z_P4|xTkTy+@|m%exJB}@IU8=)}tTZmzx0z`la5CI}U1c(3;AOb|-&Is72iQn(c*t=lp>>xEgH#e8~elcQQpSw(Y${frJDf zaQ@l?w49pLa++1A4Z27bgtw4ec$qMMo^2aH%>#2tUh~rw-P+H9_4#$#TC8%V7);#H zEj&wR<`x$dd!}J^?Bh@K3&oL9;PM-{scT|xJS^zuidb_22iED^A6$9+wUxFXSoTkL z&#u`I?B8bpdHbqJ0TCbqM1Tko0U|&IhyW2F0z`la5P`cOFn=#?efV@;L@*C{+!%Jh zUQrzI&FRMc%!q`K!eqGdvz#$PXvc>!n-hFhg=m?X=|y} ze%^rEFz|2=Ac&Z+cBP2)YaYXS1*SOQgn|@YEhW>|a_I{;Vr?@I0jow%HIy7;r6|_r zgGeEgW!&epw^JJ@n8%x>{ikLBg$LS*01+SpM1Tko0U|&IhyW2F0z`la5P^4)KsK40 zOPI6|JDb2ulNphXJEH&p@6WtS+P~P}+Viu2%>FoA%fHG0od0^}@0nLKd+~Sang|d9 zB0vO)01+SpM1Tko0V4391UBuAwR~(Rc6+$4SSVl^TJ85mRIY#u*g*Bd2*LvxheHW& zj|CVb1;s{~LLP+>^*BgA2$hIJIbO+b-oHCzQ#?0qRnGG7Po(MAFlY$o>%jC_N5!#&Fifs{Gw?8Y9R`ol)Mf@Xx^a!Mg$1Z_7zi`~!b5Q4K%fd-a1NZg zJ>A543lm-IQyHtYdu6QcG@NP&hPr92R&MbKW8|aDxH%liu#bW!G4zR{jWjxVsmk=g zeyLR2H~f2gQGrpR(3%?o=JGSl>K{X4#C+6RX|6#u9`FT3mHa4$=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/end-of-stream": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", + "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", + "license": "MIT", + "dependencies": { + "once": "^1.4.0" + } + }, + "node_modules/expand-template": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", + "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", + "license": "(MIT OR WTFPL)", + "engines": { + "node": ">=6" + } + }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "license": "MIT" + }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT" + }, + "node_modules/github-from-package": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", + "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", + "license": "MIT" + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ini": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "license": "ISC" + }, + "node_modules/mimic-response": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", + "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "license": "MIT" + }, + "node_modules/napi-build-utils": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", + "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", + "license": "MIT" + }, + "node_modules/node-abi": { + "version": "3.89.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.89.0.tgz", + "integrity": "sha512-6u9UwL0HlAl21+agMN3YAMXcKByMqwGx+pq+P76vii5f7hTPtKDp08/H9py6DY+cfDw7kQNTGEj/rly3IgbNQA==", + "license": "MIT", + "dependencies": { + "semver": "^7.3.5" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/prebuild-install": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", + "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", + "deprecated": "No longer maintained. Please contact the author of the relevant native addon; alternatives are available.", + "license": "MIT", + "dependencies": { + "detect-libc": "^2.0.0", + "expand-template": "^2.0.3", + "github-from-package": "0.0.0", + "minimist": "^1.2.3", + "mkdirp-classic": "^0.5.3", + "napi-build-utils": "^2.0.0", + "node-abi": "^3.3.0", + "pump": "^3.0.0", + "rc": "^1.2.7", + "simple-get": "^4.0.0", + "tar-fs": "^2.0.0", + "tunnel-agent": "^0.6.0" + }, + "bin": { + "prebuild-install": "bin.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/pump": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz", + "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==", + "license": "MIT", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, + "node_modules/rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", + "dependencies": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "bin": { + "rc": "cli.js" + } + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.7.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", + "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/simple-concat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", + "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/simple-get": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz", + "integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "decompress-response": "^6.0.0", + "once": "^1.3.1", + "simple-concat": "^1.0.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tar-fs": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.4.tgz", + "integrity": "sha512-mDAjwmZdh7LTT6pNleZ05Yt65HC3E+NiQzl672vQG38jIrehtJk/J3mNwIg+vShQPcLF/LV7CMnDW6vjj6sfYQ==", + "license": "MIT", + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.1.4" + } + }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + }, + "engines": { + "node": "*" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/task-2/setup.sql b/task-2/setup.sql new file mode 100644 index 0000000..142aea5 --- /dev/null +++ b/task-2/setup.sql @@ -0,0 +1,16 @@ + ---DECKS--- +CREATE TABLE decks ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +name TEXT NOT NULL, +description TEXT +); + + ---CARDS--- +CREATE TABLE cards ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +question TEXT NOT NULL, +answer TEXT NOT NULL, +learned TEXT NOT NULL DEFAULT 0, +deckId INTEGER NOT NULL, +FOREIGN KEY (deckId) 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..b6fddba 100644 --- a/task-2/src/database.js +++ b/task-2/src/database.js @@ -21,23 +21,50 @@ const db = new Database(DB_FILE); export function getAllDecks() { // TODO: return all rows from the decks table - throw new Error('Not implemented'); + try { + const stmt = db.prepare('SELECT * FROM decks'); + const decks = stmt.all(); + return decks; + } catch (err) { + throw new Error(`Failed to load decks: ${err.message}`); + } } export function getDeckById(id) { // TODO: return the deck row with the given id, or null if not found - throw new Error('Not implemented'); + try { + const stmt = db.prepare('SELECT * FROM decks WHERE id = ?'); + const deck = stmt.get(id); + return deck || null; + } catch (err) { + throw new Error(`Failed to fetch deck with id ${id}: ${err.message}`); + } } export function addDeck(name, description) { // TODO: insert a new deck and return the new row (including its id) - throw new Error('Not implemented'); + try { + const stmt = db.prepare(` + INSERT INTO decks (name, description) + VALUES (?, ?) + `); + const result = stmt.run(name, description); + const deck = db.prepare('SELECT * FROM decks WHERE id = ?').get(result.lastInsertRowid); + return deck; + } catch (err) { + throw new Error(`Failed to add deck: ${err.message}`); + } } - -export function deleteDeck(deckId) { +export function deleteDeck(id) { // TODO: delete the deck with the given id // return true if a row was deleted, false otherwise - throw new Error('Not implemented'); + try { + const stmt = db.prepare('DELETE FROM decks WHERE id = ?'); + const result = stmt.run(id); + return result.changes > 0; + } catch (err) { + throw new Error(`Failed to delete deck with id: ${id}: ${err.message}`); + } } // ---------------------------------------------------------------- @@ -46,22 +73,63 @@ export function deleteDeck(deckId) { export function getAllCardsForDeck(deckId) { // TODO: return all card rows whose deckId matches - throw new Error('Not implemented'); + try { + const stmt = db.prepare(` + SELECT * FROM cards WHERE id = ?`); + const cards = stmt.all(deckId); + + return cards; + } catch (err) { + throw new Error(`Failed to load cards for deck ${deckId}: ${err.message}`); + } } export function addCard(question, answer, deckId) { // TODO: insert a new card and return the new row (including its id) - throw new Error('Not implemented'); + try { + const stmt = db.prepare(` + INSERT INTO cards (question, answer, learned, deckId) + VALUES (?, ?, 0, ?) + + `); + const result = stmt.run(question, answer, deckId); + const card = db.prepare('SELECT * FROM cards WHERE id = ?').get(result.lastInsertRowid); + return card; + } catch (err) { + throw new Error(`Failed to add card: ${err.message}`); + } } 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'); + try { + const stmt = db.prepare(` + UPDATE cards + SET learned = 1 + WHERE id = ? + `); + const result = stmt.run(cardId); + if (result.changes === 0) { + return null; + } + const card = db.prepare(`SELECT * FROM cards WHERE id = ?`).get(cardId); + return card; + } catch (err) { + throw new Error(`Failed to mark card learned: ${err.message}`); + } } - 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'); + try { + const stmt = db.prepare(` + DELETE FROM cards WHERE id = ? + + `); + const result = stmt.run(cardId); + return result.changes > 0; + } catch (err) { + throw new Error(`Failed to delete card with id: ${cardId}: ${err.message}`); + } } From 2c870ff184515fd045265c871d16fbd72e5dd475 Mon Sep 17 00:00:00 2001 From: Unlock7 Date: Wed, 1 Apr 2026 17:53:19 +0200 Subject: [PATCH 2/2] Update task-1/queries.sql Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- task-1/queries.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/task-1/queries.sql b/task-1/queries.sql index 01ebdc5..4b46d7b 100644 --- a/task-1/queries.sql +++ b/task-1/queries.sql @@ -45,7 +45,9 @@ WHERE b.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 books.title = 'Fikir Eske Mekabir'; -- or can be handled by using Id number too. +WHERE books.title = 'Fikir Eske Mekabir' + AND books.author_id = 25 + AND books.published_year = 1968; --- -- ### Bonus questions _(optional)_