diff --git a/ (2).gitignore b/ (2).gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/ (2).gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..e69de29 diff --git a/books.json b/books.json new file mode 100644 index 0000000..0641f9f --- /dev/null +++ b/books.json @@ -0,0 +1,51 @@ +[ + { + "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 Great Gatsby", + "author": "F. Scott Fitzgerald", + "genre": "Classic", + "read": false + }, + { + "id": 4, + "title": "To Kill a Mockingbird", + "author": "Harper Lee", + "genre": "Classic", + "read": true + }, + { + "id": 6, + "title": "Clean Code", + "author": "Robert C. Martin", + "genre": "Programming", + "read": false + }, + { + "id": 6, + "title": "Clean Code", + "author": "Robert C. Martin", + "genre": "Programming", + "read": false + }, + { + "id": 6, + "title": "Clean Code", + "author": "Robert C. Martin", + "genre": "Programming", + "read": false + } +] \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..5bd284f --- /dev/null +++ b/index.js @@ -0,0 +1,122 @@ +const fs = require('fs'); +const chalk = require('chalk'); + +function loadBooks() { + try{ + const dataBuffer = fs.readFileSync('books.json'); + const dataJSON = dataBuffer.toString(); + return JSON.parse(dataJSON); + } catch (error) { + console.log(chalk.red('Error loading books. starting with an empty list.')); + return []; + } +} + +function saveBooks(books) { + try { + const dataJSON = JSON.stringify(books, null, 2); + fs.writeFileSync('books.json', dataJSON); + } catch (error) { + console.log(chalk.red('Error saving books.')); + } +} +const books = loadBooks(); +console.log(books); + + +function addBook(book) { + const books = loadBooks(); + + books.push(book); + + saveBooks(books); + + console.log(chalk.green('Book added successfully!')); +} + +function getUnreadBooks() { + const books = loadBooks(); + + return books.filter((book) => book.read === false); +} + +function getBooksByGenre(genre) { + const books = loadBooks(); + + return books.filter( + (book) => book.genre.toLowerCase() === genre.toLowerCase() + ); +} + +function markAsRead(id) { + const books = loadBooks(); + + const updatedBooks = books.map((book) => { + if (book.id === id) { + return { ...book, read: true }; + } + return book; + }); + + saveBooks(updatedBooks); + + console.log(chalk.green('Book marked as read!')); +} + +function getTotalBooks() { + const books = loadBooks(); + return books.length; +} + +function hasUnreadBooks() { + const books = loadBooks(); + + return books.some((book) => book.read === false); +} +addBook({ + id: 6, + title: 'Clean Code', + author: 'Robert C. Martin', + genre: 'Programming', + read: false +}); + +function printAllBooks() { + const books = loadBooks(); + + console.log(chalk.bold('\nšŸ“š MY READING LIST šŸ“š\n')); + + if (books.length === 0) { + console.log(chalk.yellow('No books found.\n')); + return; + } + + books.forEach((book) => { + const title = chalk.cyan(book.title); + + const status = book.read + ? chalk.green('āœ“ Read') + : chalk.yellow('⚠ Unread'); + + console.log( + `${book.id}. ${title} by ${book.author} (${book.genre}) ${status}` + ); + }); + + console.log(); +} + +function printSummary() { + const books = loadBooks(); + + const total = books.length; + const readCount = books.filter((book) => book.read).length; + const unreadCount = books.filter((book) => !book.read).length; + + console.log(chalk.bold(' SUMMARY ')); + console.log(chalk.bold(`Total Books: ${total}`)); + console.log(chalk.bold(`Read: ${readCount}`)); + console.log(chalk.bold(`Unread: ${unreadCount}\n`)); +} +printAllBooks(); +printSummary(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9ee2974 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,82 @@ +{ + "name": "cc55-core-week-6", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "chalk": "^4.1.2" + } + }, + "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/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/package.json b/package.json new file mode 100644 index 0000000..b51678e --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "chalk": "^4.1.2" + } +} diff --git a/reading-list-manger/package-lock.json b/reading-list-manger/package-lock.json new file mode 100644 index 0000000..aa51220 --- /dev/null +++ b/reading-list-manger/package-lock.json @@ -0,0 +1,86 @@ +{ + "name": "reading-list-manger", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "reading-list-manger", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + } + }, + "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/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-manger/package.json b/reading-list-manger/package.json new file mode 100644 index 0000000..ae37b1c --- /dev/null +++ b/reading-list-manger/package.json @@ -0,0 +1,16 @@ +{ + "name": "reading-list-manger", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "chalk": "^4.1.2" + } +}