-
Notifications
You must be signed in to change notification settings - Fork 19
Diana C. #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Diana C. #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
|
|
||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "tabWidth": 2, | ||
| "trailingComma": "es5", | ||
| "printWidth": 80 | ||
| } | ||
|
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "devDependencies": { | ||
| "prettier": "^3.8.1" | ||
| }, | ||
| "name": "c55-core-week-6", | ||
| "version": "1.0.0", | ||
| "description": "The week 6 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-6-assignment", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/dianadenwik/c55-core-week-6.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "module", | ||
| "bugs": { | ||
| "url": "https://github.com/dianadenwik/c55-core-week-6/issues" | ||
| }, | ||
| "homepage": "https://github.com/dianadenwik/c55-core-week-6#readme", | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,37 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
|
|
||
| // TODO: Implement the main application logic here | ||
| // Your implementation here | ||
|
|
||
| import chalk from 'chalk'; | ||
|
|
||
| import { | ||
| loadBooks, | ||
| printAllBooks, | ||
| printSummary, | ||
| getUnreadBooks, | ||
| getBooksByGenre, | ||
| markAsRead, | ||
| hasUnreadBooks, | ||
| addBook, | ||
| } from './readingList.js'; | ||
|
|
||
| // 1. Load books on startup | ||
| loadBooks(); | ||
| console.log('📚 MY READING LIST 📚\n'); | ||
|
|
||
| // 2. Display all books | ||
| printAllBooks(); | ||
|
|
||
| // 3. Show summary statistics | ||
| // 4. Add example of filtering by genre or read/unread status | ||
| // 5. Add example of marking a book as read | ||
| printSummary(); | ||
|
|
||
| console.log('📚 MY READING LIST 📚\n'); | ||
| // 4. 4. Add example of filtering by genre or read/unread status | ||
| console.log(chalk.bold('\n📖 Unread Books:')); | ||
| const unread = getUnreadBooks(); | ||
| console.log(unread); | ||
|
|
||
| console.log(chalk.bold('\n🔍 Sci-Fi Books:')); | ||
| const scifi = getBooksByGenre('Sci-Fi'); | ||
| console.log(scifi); | ||
|
|
||
| // Your implementation here | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,44 @@ | ||
| [] | ||
| [ | ||
| { | ||
| "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": "The Great Gatsby", | ||
| "author": "F. Scott Fitzgerald", | ||
| "genre": "Fiction", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "title": "Brave New World", | ||
| "author": "Aldous Huxley", | ||
| "genre": "Sci-Fi", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 6, | ||
| "title": "Harry Potter", | ||
| "author": "J.K. Rowling", | ||
| "genre": "Fantasy", | ||
| "read": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,123 @@ | ||
| // Place here the file operation functions for loading and saving books | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import chalk from 'chalk' | ||
|
|
||
| const dataDir = '.' | ||
| const BOOKS = path.join(dataDir, 'books.json') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A more helpful name would be |
||
|
|
||
|
|
||
|
|
||
|
|
||
| function loadBooks() { | ||
| // TODO: Implement this function | ||
| // Read from books.json | ||
| // Handle missing file (create empty array) | ||
| // Handle invalid JSON (notify user, use empty array) | ||
| // Use try-catch for error handling | ||
| try { | ||
| const data = fs.readFileSync(BOOKS, 'utf-8') | ||
| return JSON.parse(data) | ||
| } catch (error) { | ||
| if (error.code === 'ENOENT') { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job handling errors based on the type of error ⭐ |
||
| console.log('books.json not found. Starting with empty list.') | ||
| return [] | ||
| } | ||
| if (error.name === "SyntaxError") { | ||
| console.log('Invalid JSON in books.json. Starting with empty list.') | ||
| return [] | ||
| } | ||
| throw error | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For unhandled errors, it is better to log it and then return an empty error, than to just throw the error. |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| function saveBooks(books) { | ||
| // TODO: Implement this function | ||
| // Write books array to books.json | ||
| // Use try-catch for error handling | ||
| try { | ||
| fs.writeFileSync(BOOKS, JSON.stringify(books, null, 2)) | ||
| } catch (error) { | ||
| console.log('Error saving books:', error.message) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function addBook(book) { | ||
| // TODO: Implement this function | ||
| const books = loadBooks() | ||
| books.push(book) | ||
| saveBooks(books) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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() | ||
| return books.filter((book) => book.genre === genre) | ||
| } | ||
|
|
||
|
|
||
| function markAsRead(id) { | ||
| // TODO: Implement this function using map() | ||
| const books = loadBooks() | ||
| const updatedBooks = books.map((book) => { | ||
| if (book.id === id) { | ||
| return { ...book, read: true } | ||
| } | ||
| return book | ||
| }) | ||
|
|
||
| saveBooks(updatedBooks) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| function getTotalBooks() { | ||
| // TODO: Implement this function using length | ||
| const books = loadBooks() | ||
| return books.length | ||
| } | ||
|
|
||
| function hasUnreadBooks() { | ||
| // TODO: Implement this function using some() | ||
| const books = loadBooks() | ||
| return books.some((book) => book.read === false) | ||
| } | ||
|
|
||
|
|
||
| function printAllBooks() { | ||
| // TODO: Implement this function | ||
| // 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 title = chalk.cyan(book.title) | ||
| const info = `${book.id}. ${title} by ${book.author} (${book.genre})` | ||
|
|
||
| if (book.read === true) { | ||
| console.log(chalk.green(info + ' ✓ Read')) | ||
| } else { | ||
| console.log(chalk.yellow(info + ' ⚠ Unread')) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function printSummary() { | ||
| // TODO: Implement this function | ||
| // Show statistics with chalk | ||
| // Display total books, read count, unread count | ||
| // Use bold for stats | ||
| const total = getTotalBooks() | ||
| const unread = getUnreadBooks().length | ||
| const read = total - unread | ||
|
|
||
| console.log(chalk.bold('\n📊 SUMMARY 📊')) | ||
| console.log(chalk.bold('Total Books: ' + total)) | ||
| console.log(chalk.bold.green('Read: ' + read)) | ||
| console.log(chalk.bold.yellow('Unread: ' + unread)) | ||
| } | ||
|
|
||
|
|
||
| export { | ||
| loadBooks, | ||
| saveBooks, | ||
| addBook, | ||
| getUnreadBooks, | ||
| getBooksByGenre, | ||
| markAsRead, | ||
| getTotalBooks, | ||
| hasUnreadBooks, | ||
| printAllBooks, | ||
| printSummary | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done using the other functions and seeing them in action ⭐