-
Notifications
You must be signed in to change notification settings - Fork 18
Yana P. #9
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?
Yana P. #9
Changes from all commits
6c60d01
0d4f481
c474a41
443dd2c
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,7 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "trailingComma": "es5", | ||
| "printWidth": 80, | ||
| "tabWidth": 2 | ||
| } |
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,16 @@ | ||
| { | ||
| "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": { | ||
| "start": "node reading-list-manager/app.js" | ||
| }, | ||
| "type": "module", | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
|
|
||
| import { | ||
| getBooksByGenre, | ||
| loadBooks, | ||
| markAsRead, | ||
| printAllBooks, | ||
| printSummary, | ||
| } from './readingList.js'; | ||
|
|
||
| // TODO: Implement the main application logic here | ||
| // 1. Load books on startup | ||
| // 2. Display all books | ||
| // 3. Show summary statistics | ||
| // 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'); | ||
|
|
||
| // Your implementation here | ||
| console.log(loadBooks()); | ||
| console.log(printAllBooks()); | ||
| console.log(printSummary()); | ||
| console.log('Get books by novel genre:', getBooksByGenre('Novel')); | ||
| console.log('After read update:', markAsRead(1)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,44 @@ | ||
| [] | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "title": "Kobzar", | ||
| "author": "Taras Shevchenko", | ||
| "genre": "Poetry", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "title": "Forest Song", | ||
| "author": "Lesya Ukrainka", | ||
| "genre": "Drama", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 3, | ||
| "title": "The Enchanted Desna", | ||
| "author": "Oleksandr Dovzhenko", | ||
| "genre": "Memoir", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 4, | ||
| "title": "The City", | ||
| "author": "Valerian Pidmohylny", | ||
| "genre": "Novel", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "title": "Marusia Churai", | ||
| "author": "Lina Kostenko", | ||
| "genre": "Historical Novel", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 6, | ||
| "title": "Death and the Penguin", | ||
| "author": "Andrey Kurkov", | ||
| "genre": "Novel", | ||
| "read": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,114 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| const dataDir = 'reading-list-manager'; | ||
| const filePath = path.join(dataDir, 'books.json'); | ||
|
|
||
| // Place here the file operation functions for loading and saving books | ||
|
|
||
| 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 | ||
| export function loadBooks() { | ||
| try { | ||
| if (!fs.existsSync(filePath)) { | ||
| fs.writeFileSync(filePath, JSON.stringify([])); | ||
| return []; | ||
| } | ||
|
|
||
| const raw = fs.readFileSync(filePath, 'utf-8'); | ||
|
|
||
| if (!raw.trim()) { | ||
| fs.writeFileSync(filePath, JSON.stringify([])); | ||
| return []; | ||
| } | ||
|
|
||
| const data = JSON.parse(raw); | ||
|
|
||
| if (!Array.isArray(data)) { | ||
| throw new Error('Books file contains invalid content'); | ||
| } | ||
|
|
||
| return data; | ||
| } catch (error) { | ||
| console.error('Error reading books.json:', error.message); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| function saveBooks(books) { | ||
| // TODO: Implement this function | ||
| // Write books array to books.json | ||
| // Use try-catch for error handling | ||
| export function saveBooks(books) { | ||
| try { | ||
| if (!Array.isArray(books)) { | ||
| throw new Error('Books file contains invalid content'); | ||
| } | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(books, null, 2)); | ||
| return 'Books saved'; | ||
| } catch (error) { | ||
| return `Cannot write: ${error.message}`; | ||
| } | ||
| } | ||
|
|
||
| function addBook(book) { | ||
| // TODO: Implement this function | ||
| export function addBook(book) { | ||
| try { | ||
| const books = loadBooks(); | ||
|
|
||
| const newId = | ||
| books.length > 0 ? Math.max(...books.map((book) => book.id || 0)) + 1 : 1; | ||
| const newBook = { id: newId, ...book }; | ||
|
|
||
| books.push(newBook); | ||
| saveBooks(books); | ||
| return 'Book successfully added'; | ||
| } catch (error) { | ||
| return `Cannot append: ${error.message}`; | ||
| } | ||
| } | ||
|
|
||
| function getUnreadBooks() { | ||
| // TODO: Implement this function using filter() | ||
| export function getUnreadBooks() { | ||
| return loadBooks().filter((book) => !book.read); | ||
| } | ||
|
|
||
| function getBooksByGenre(genre) { | ||
| // TODO: Implement this function using filter() | ||
| export function getBooksByGenre(genre) { | ||
|
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. Array methods can be chained. So this can be: getBooksByGenre() => |
||
| return loadBooks().filter((book) => book.genre === genre); | ||
| } | ||
|
|
||
| function markAsRead(id) { | ||
| // TODO: Implement this function using map() | ||
| export function markAsRead(id) { | ||
| const updateForRead = loadBooks().map((book) => | ||
| book.id === id ? { ...book, read: true } : book | ||
| ); | ||
| saveBooks(updateForRead); | ||
|
|
||
| return updateForRead; | ||
| } | ||
|
|
||
| function getTotalBooks() { | ||
| // TODO: Implement this function using length | ||
| export function getTotalBooks() { | ||
| return loadBooks().length; | ||
| } | ||
|
|
||
| function hasUnreadBooks() { | ||
| // TODO: Implement this function using some() | ||
| export function hasUnreadBooks() { | ||
|
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. can.be.chained: loadBooks().books.some((book) => !book.read); |
||
| return loadBooks().some((book) => !book.read); | ||
| } | ||
|
|
||
| function printAllBooks() { | ||
| // TODO: Implement this function | ||
| // Loop through and display with chalk | ||
| // Use green for read books, yellow for unread | ||
| // Use cyan for titles | ||
| export function printAllBooks() { | ||
| const books = loadBooks(); | ||
| let output = '\n📚 MY READING LIST 📚 \n\nAll books:\n\n'; | ||
|
|
||
| for (const book of books) { | ||
| const { id, title, author, genre, read } = book; | ||
| const readChalk = read ? chalk.green('📗 Read') : chalk.yellow('📕 Unread'); | ||
| const titleChalk = chalk.cyan(title); | ||
|
|
||
| output += `${id}. ${titleChalk} by ${author} (${genre}) ${readChalk}\n`; | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| function printSummary() { | ||
| // TODO: Implement this function | ||
| // Show statistics with chalk | ||
| // Display total books, read count, unread count | ||
| // Use bold for stats | ||
| } | ||
| export function printSummary() { | ||
| const totalBooks = getTotalBooks(); | ||
| const unreadBooks = getUnreadBooks().length; | ||
| const readBooks = totalBooks - unreadBooks; | ||
|
|
||
| const output = `\n📊 SUMMARY 📊\n\n${chalk.bold(`Total Books: ${totalBooks}\nRead: ${readBooks}\nUnread: ${unreadBooks}`)}`; | ||
|
|
||
| return output; | ||
| } | ||
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.
Nice... this helps the user of your app