diff --git a/reading-list-manager/.prettierrc.json b/reading-list-manager/.prettierrc.json new file mode 100644 index 0000000..ce5dea0 --- /dev/null +++ b/reading-list-manager/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "semi": true, + "printWidth": 80 +} diff --git a/reading-list-manager/app.js b/reading-list-manager/app.js index b0365ef..36945e4 100644 --- a/reading-list-manager/app.js +++ b/reading-list-manager/app.js @@ -1,5 +1,16 @@ // This is the entrypoint for your application. // node app.js +const chalk = require('chalk'); +const { + printAllBooks, + printSummary, + getUnreadBooks, + getBooksByGenre, + addBook, + markAsRead, +} = require('./readingList'); +const command = process.argv[2]; + // TODO: Implement the main application logic here // 1. Load books on startup @@ -8,6 +19,30 @@ // 4. Add example of filtering by genre or read/unread status // 5. Add example of marking a book as read -console.log('šŸ“š MY READING LIST šŸ“š\n'); +printAllBooks(); +printSummary(); + +// Example: filter unread +const unread = getUnreadBooks(); +console.log('\nUnread Books:'); +unread.forEach((book) => console.log(`${book.id}. ${book.title}`)); + +// Example: filter by genre +const fiction = getBooksByGenre('Fiction'); +console.log('\nFiction Books:'); +fiction.forEach((book) => console.log(`${book.id}. ${book.title}`)); + +// Example: add new book +if (command === 'add') { + const title = process.argv[3]; + const author = process.argv[4]; + const genre = process.argv[5]; +addBook({ title: 'Brave New World', author: 'Aldous Huxley', genre: 'Fiction' }); +} + +// Example: mark a book as read +markAsRead(2); -// Your implementation here +console.log('\nAfter updates:'); +printAllBooks(); +printSummary(); \ No newline at end of file diff --git a/reading-list-manager/books.json b/reading-list-manager/books.json index 0637a08..61cb1bf 100644 --- a/reading-list-manager/books.json +++ b/reading-list-manager/books.json @@ -1 +1,37 @@ -[] \ No newline at end of file +[ + { + "id": 1, + "title": "1984", + "author": "George Orwell", + "genre": "Fiction", + "read": false + }, + { + "id": 2, + "title": "Dune", + "author": "Frank Herbert", + "genre": "Sci-Fi", + "read": true + }, + { + "id": 3, + "title": "The Hobbit", + "author": "J.R.R. Tolkien", + "genre": "Fantasy", + "read": true + }, + { + "id": 4, + "title": "Sapiens", + "author": "Yuval Noah Harari", + "genre": "Non-fiction", + "read": false + }, + { + "id": 5, + "title": "Clean Code", + "author": "Robert C. Martin", + "genre": "Programming", + "read": true + } +] diff --git a/reading-list-manager/package-lock.json b/reading-list-manager/package-lock.json new file mode 100644 index 0000000..b0215d4 --- /dev/null +++ b/reading-list-manager/package-lock.json @@ -0,0 +1,105 @@ +{ + "name": "reading-list-manager", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "reading-list-manager", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "license": "MIT" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/prettier": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", + "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + } + } +} diff --git a/reading-list-manager/package.json b/reading-list-manager/package.json new file mode 100644 index 0000000..dc7b4a1 --- /dev/null +++ b/reading-list-manager/package.json @@ -0,0 +1,19 @@ +{ + "name": "reading-list-manager", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } +} diff --git a/reading-list-manager/readingList.js b/reading-list-manager/readingList.js index 84febab..b42b746 100644 --- a/reading-list-manager/readingList.js +++ b/reading-list-manager/readingList.js @@ -1,4 +1,7 @@ -// Place here the file operation functions for loading and saving books +const fs = require('fs'); +const chalk = require('chalk'); + +const FILE = 'books.json'; function loadBooks() { // TODO: Implement this function @@ -6,36 +9,94 @@ function loadBooks() { // Handle missing file (create empty array) // Handle invalid JSON (notify user, use empty array) // Use try-catch for error handling + try { + const text = fs.readFileSync(FILE, 'utf8'); + const books = JSON.parse(text); + if (!Array.isArray(books)) { + console.log(chalk.yellow('⚠ books.json is not an array. Using empty list.')); + return []; + } + return books; + } catch (error) { + if (error.code === 'ENOENT') { + // missing file -> create empty array + fs.writeFileSync(FILE, '[]\n', 'utf8'); + return []; + } + if (error.name === 'SyntaxError') { + console.log(chalk.yellow('⚠ Invalid JSON in books.json. Using empty list.')); + return []; + } + throw error; + } } function saveBooks(books) { // TODO: Implement this function // Write books array to books.json // Use try-catch for error handling + try { + fs.writeFileSync(FILE, JSON.stringify(books, null, 2) + '\n', 'utf8'); + } catch (error) { + console.log(chalk.red('āŒ Could not save books.json')); + throw error; + } } function addBook(book) { // TODO: Implement this function + const books = loadBooks(); + // find max id + const maxId = books.reduce((max, book) => Math.max(max, book.id), 0) + const newBook = { + id: maxId + 1, + title: book.title, + author: book.author, + genre: book.genre, + read: false, + }; + books.push(newBook); + saveBooks(books); + return newBook; } function getUnreadBooks() { // TODO: Implement this function using filter() + const books = loadBooks(); + return books.filter((book) => book.read === false); } function getBooksByGenre(genre) { // TODO: Implement this function using filter() + const books = loadBooks(); + const genreLowCase = String(genre).toLowerCase(); + return books.filter((book) => String(book.genre).toLowerCase() === genreLowCase); } function markAsRead(id) { // TODO: Implement this function using map() + const books = loadBooks(); + const targetId = Number(id); + let found = false; + const updated = books.map((book) => { + if (Number(book.id) === targetId) { + found = true; + return { ...book, read: true }; + } + return book; + }); + if (found) saveBooks(updated); + return found; } function getTotalBooks() { // TODO: Implement this function using length + return loadBooks().length; } function hasUnreadBooks() { // TODO: Implement this function using some() + return loadBooks().some((book) => book.read === false); } function printAllBooks() { @@ -43,6 +104,17 @@ function printAllBooks() { // Loop through and display with chalk // Use green for read books, yellow for unread // Use cyan for titles + const books = loadBooks(); + console.log('\nAll Books:'); + books.forEach((book) => { + const color = book.read ? chalk.green : chalk.yellow; + const status = book.read ? 'āœ“ Read' : '⚠ Unread'; + console.log( + color(`${book.id}. `) + + chalk.cyan(book.title) + + color(` by ${book.author} (${book.genre}) ${status}`) + ); + }); } function printSummary() { @@ -50,4 +122,76 @@ function printSummary() { // Show statistics with chalk // Display total books, read count, unread count // Use bold for stats -} \ No newline at end of file + const books = loadBooks(); + const total = books.length; + const readCount = books.filter((book) => book.read === true).length; + const unreadCount = total - readCount; + console.log(chalk.bold('\nšŸ“Š SUMMARY šŸ“Š')); + console.log(chalk.bold(`Total Books: ${total}`)); + console.log(chalk.bold(`Read: ${readCount}`)); + console.log(chalk.bold(`Unread: ${unreadCount}`)); +} +module.exports = { + loadBooks, + saveBooks, + addBook, + getUnreadBooks, + getBooksByGenre, + markAsRead, + getTotalBooks, + hasUnreadBooks, + printAllBooks, + printSummary, +}; + + + + + + + + + + + + +function printAllBooks() { + const books = loadBooks(); + + console.log('\nAll Books:'); + books.forEach((b) => { + const color = b.read ? chalk.green : chalk.yellow; + const status = b.read ? 'āœ“ Read' : '⚠ Unread'; + + console.log( + color(`${b.id}. `) + + chalk.cyan(b.title) + + color(` by ${b.author} (${b.genre}) ${status}`) + ); + }); +} + +function printSummary() { + const books = loadBooks(); + const total = books.length; + const readCount = books.filter((b) => b.read === true).length; + const unreadCount = total - readCount; + + console.log(chalk.bold('\nšŸ“Š SUMMARY šŸ“Š')); + console.log(chalk.bold(`Total Books: ${total}`)); + console.log(chalk.bold(`Read: ${readCount}`)); + console.log(chalk.bold(`Unread: ${unreadCount}`)); +} + +module.exports = { + loadBooks, + saveBooks, + addBook, + getUnreadBooks, + getBooksByGenre, + markAsRead, + getTotalBooks, + hasUnreadBooks, + printAllBooks, + printSummary, +};