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
82 changes: 52 additions & 30 deletions controllers/books.js
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) => {
Copy link
Copy Markdown
Collaborator

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

res.send(books);
const books = JSON.parse(fs.readFileSync('./data/books.json'));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use module imports to pull in the json.
import books from './data/books.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'))]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see that this JSON.parse(fs.readFileSync('./data/books.json')) is being used many places. You will probably not need this after implementing the module import suggestion. However, such reused code pieces can be declared at the top of the file and used in all the functions.

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.`);
}




25 changes: 3 additions & 22 deletions data/books.json
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"}]
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import bodyParser from 'body-parser';
import routes from './routes/books.js'


const app = express();
const PORT = 5000;

Expand Down