Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}

105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
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"
}
}
34 changes: 29 additions & 5 deletions reading-list-manager/app.js
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();
Copy link

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 ⭐

console.log(unread);

console.log(chalk.bold('\n🔍 Sci-Fi Books:'));
const scifi = getBooksByGenre('Sci-Fi');
console.log(scifi);

// Your implementation here
45 changes: 44 additions & 1 deletion reading-list-manager/books.json
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
}
]
114 changes: 92 additions & 22 deletions reading-list-manager/readingList.js
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')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more helpful name would be booksPath. Currently with this name it looks like this variable holds the array of books. This can be a little misleading.





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') {
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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
}