Untitled

 avatar
unknown
plain_text
5 months ago
3.0 kB
3
Indexable
const express = require('express')
const app = express()
const port = 3000

// books = [{
//     id: 1,
//     name: 'harry potter',
//     author: 'author'
// }]
books = []
// reviews = [{
//     id: 1,
//     book_id: 1,
//     rating: 4,
//     comment: "good to read"
// }]
reviews = []

app.use(express.json());

app.get('/books', (req, res) => {
  res.send(JSON.stringify(books))
})

app.get('/books/:id', (req, res) => {
    requestedId = req?.params?.id
    let found = false
    for (let i=0 ; i<books.length ; i++) {
        if (books[i].id == requestedId) {
            found = true
            res.status(200).json(books[i])
        }
    }
    if (found === false) {
        res.status(404).json({error: 'book with id not found'})
    }
})

const bookValidator = (body, res) => {
    if (!body.name) {
        res.status(400).json({error: 'Please pass name for the book'})
    } else if (!body.author) {
        res.status(400).send({error: 'Please pass author of the book'})
    }
}

app.post('/book', (req, res) => {
    bookValidator(req?.body, res)
    console.log('printing request body ', req.body)

    id = books.length > 0 && books[books.length - 1].id
    books.push({
        id: id+1,
        name: req.body.name,
        author: req.body.name
    })
    res.status(201).json()
})

const checkForMatchingBook = (bookId) => {
    console.log('printing bookid ', bookId)
    for (let i=0 ; i<books.length ; i++) {
        if(Number(books[i].id) === Number(bookId)) {
            return true
        }
    }
    return false
}

const reviewValidator = (body, params, res) => {
    if (!params.book_id) {
        res.status(400).json({error: 'Please pass id of the book'})
    } else if (!body.rating) {
        res.status(400).send({error: 'Please pass rating for the review of the book'})
    } else if (!body.comment) {
        res.status(400).send({error: 'Please pass comment for the review of the book'})
    }
    let found = checkForMatchingBook(params.book_id)
    if(found === false) {
        res.status(400).send({error: 'No matching book found'})
    }
}

app.post('/books/:book_id/reviews', (req, res) => {
    reviewValidator(req?.body, req?.params, res)

    id = reviews.length > 0 && reviews[reviews.length - 1].id
    reviews.push({
        id: id+1,
        book_id: req.params.book_id,
        rating: req.body.rating,
        comment: req.body.comment
    })
    res.status(201).json(reviews[id])
})

app.get('/books/:book_id/reviews', (req, res) => {
    let found = checkForMatchingBook(req.params.book_id)
    if(found === false) {
        res.status(400).send({error: 'No matching book found'})
    }

    filteredReviews = []
    for (let i=0 ; i<reviews.length ; i++) {
        if (reviews[i].book_id === req.params.book_id) {
            filteredReviews.push(reviews[i])
        }
    }
    res.status(200).json({filteredReviews: filteredReviews, avg_rating: 10})
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
Editor is loading...
Leave a Comment