-
Notifications
You must be signed in to change notification settings - Fork 0
Bugfix/list all books #5
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?
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 |
|---|---|---|
| @@ -1,44 +1,66 @@ | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
| let books = [] | ||
| import fs from 'fs'; | ||
|
|
||
| export const getBooks = (req, res) => { | ||
| res.send(books); | ||
| const books = JSON.parse(fs.readFileSync('./data/books.json')); | ||
|
Collaborator
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. use module imports to pull in the json. |
||
| res.send(books); | ||
| } | ||
|
|
||
| export const createBook = (req, res) => { | ||
| const book = req.body; | ||
| books.push({ ...book, id: uuidv4()}); | ||
| res.send(`Book with the name ${book.bookTitle} added to the database!`); | ||
| } | ||
| let books = [...JSON.parse(fs.readFileSync('./data/books.json'))] | ||
|
Collaborator
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. I see that this |
||
| const book = req.body; | ||
| books.push({ ...book, id: uuidv4()}); | ||
| fs.writeFileSync('./data/books.json', JSON.stringify(books), 'utf-8') | ||
| res.send(`Book with the name ${book.bookTitle} added to the database!`) | ||
| } | ||
|
|
||
| export const getBook = (req, res) => { | ||
| const { id } = req.params; | ||
| const foundBook = books.find(book => book.id ==id); | ||
| res.send(foundBook); | ||
| const books = JSON.parse(fs.readFileSync('./data/books.json')); | ||
| const { id } = req.params; | ||
| const foundBook = books.find(book => book.id ==id); | ||
| res.send(foundBook); | ||
| } | ||
|
|
||
| export const updateBook = (req, res) => { | ||
| const { id } = req.params; | ||
| const {bookTitle, authorName, numberOfPages, price } = req.body; | ||
| const book = books.find((book) => book.id == id); | ||
|
|
||
| if(bookTitle) { | ||
| book.bookTitle = bookTitle; | ||
| } | ||
| if(authorName){ | ||
| book.authorName = authorName; | ||
| } | ||
| if(numberOfPages){ | ||
| book.numberOfPages = numberOfPages; | ||
| } | ||
| if(price){ | ||
| book.price = price; | ||
| } | ||
| res.send(`Book with the id ${id} has been updated.`); | ||
| // get all books | ||
| let books = [...JSON.parse(fs.readFileSync('./data/books.json'))] | ||
| // get update id | ||
| const { id } = req.params; | ||
| // get update payload | ||
| const {bookTitle, authorName, numberOfPages, price } = req.body; | ||
|
|
||
| // find books | ||
| const book = books.find((book) => book.id == id); | ||
| console.log(book); | ||
| // update payload | ||
| bookTitle ? book.bookTitle = bookTitle: null; | ||
| authorName ? book.authorName = authorName: null; | ||
| numberOfPages ? book.numberOfPages = numberOfPages : null; | ||
| price ? book.price = price: null; | ||
|
|
||
| console.log(book); | ||
| // get new array | ||
| const newBooks = books.filter((book)=>book.id != id) | ||
| console.log(newBooks); | ||
| // write new file | ||
| const savedArray = [...newBooks,book] | ||
| console.log(savedArray); | ||
| fs.writeFileSync('./data/books.json', JSON.stringify(savedArray), 'utf-8') | ||
|
|
||
| res.send(`Book with the id ${id} has been updated.`); | ||
| } | ||
| export const deleteBook = (req, res) => { | ||
| const { id } = req.params; | ||
| books = books.filter((book) => book.id !== id); | ||
| res.send(`Book with the id ${id} deleted from the database.`); | ||
| // get all books | ||
| let books = [...JSON.parse(fs.readFileSync('./data/books.json'))] | ||
| // get update id | ||
| const { id } = req.params; | ||
| // get new array | ||
| const newBooks = books.filter((book) => book.id !== id); | ||
| console.log(newBooks); | ||
| fs.writeFileSync('./data/books.json', JSON.stringify(newBooks), 'utf-8') | ||
|
|
||
| res.send(`Book with the id ${id} deleted from the database.`); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,3 @@ | ||
| [{ | ||
| "bookTitle": "JS Fundamentals", | ||
| "authorName": "John Doe", | ||
| "numberOfPages": "300", | ||
| "price": "$20", | ||
| "ratings": "5 star" | ||
|
|
||
| }, | ||
| { | ||
| "bookTitle": "Modern JavaScript", | ||
| "authorName": "Jane Doe", | ||
| "numberOfPages": "400", | ||
| "price": "$30", | ||
| "ratings": "5 star" | ||
| }, | ||
| { | ||
| "bookTitle": "Advanced JavaScript", | ||
| "authorName": "Jonny Doe", | ||
| "numberOfPages": "400", | ||
| "price": "$35", | ||
| "ratings": "5 star" | ||
| }] | ||
| [{"bookTitle":"JS Fundamentals","authorName":"John Doe","numberOfPages":"300","price":"$20","ratings":"5 star","id":"df5fac54-6bf0-421c-a476-df5405fdac3f"}, | ||
| {"bookTitle":"Advanced JavaScript","authorName":"Jonny Doe","numberOfPages":"400","price":"$35","ratings":"5 star","id":"aad84636-c92c-4ee4-9f36-9e20103dc3d1"}, | ||
| {"bookTitle":"Modern JavaScript","authorName":"Jane Doe","numberOfPages":"500","price":"$30","ratings":"5 star","id":"e434d1f3-e2b3-4199-808e-bae71626d21d"}] |
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.
your tabs are still not 4 spaces