'use strict'
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
const Book = use('App/Models/Book');
class BookContext {
  /**
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Function} next
   */
  async handle(ctx, next) {
    const {request, auth, response} = ctx;
    const bookId = request.params.bookId;
    const book = await Book.find(bookId);
    if (!book) {
      response.status(404);
      response.send({code: 404, message: 'Book not found'});
      return;
    }
    ctx.book = book;
    // call next to advance the request
    await next()
  }
}

module.exports = BookContext
