/** * @swagger * components: * schemas: * Book: * type: object * required: * - title * - author * properties: * id: * type: string * description: The auto-generated id of the book * title: * type: string * description: The book title * author: * type: string * description: The book author * example: * id: d5fE_asz * title: The New Turing Omnibus * author: Alexander K. Dewdney */
/** * @swagger * tags: * name: Books * description: The books managing API */
/** * @swagger * /books: * get: * summary: Returns the list of all the books * tags: [Books] * responses: * 200: * description: The list of the books * content: * application/json: * schema: * type: array * items: * $ref: '#/components/schemas/Book' */
/** * @swagger * /books/{id}: * get: * summary: Get the book by id * tags: [Books] * parameters: * - in: path * name: id * schema: * type: string * required: true * description: The book id * responses: * 200: * description: The book description by id * contents: * application/json: * schema: * $ref: '#/components/schemas/Book' * 404: * description: The book was not found */ router.get('/:id', (req, res) => { const book = req.app.db.get('books').find({ id: req.params.id }).value() if (!book) res.send(404) res.send(book) })
/** * @swagger * /books: * post: * summary: Create a new book * tags: [Books] * requestBody: * required: true * content: * application/json: * schema: * $ref: '#/components/schemas/Book' * responses: * 200: * description: The book was successfully created * content: * application/json: * schema: * $ref: '#/components/schemas/Book' * 500: * description: Server Error */ router.post('/', (req, res) => { try { const book = { id: nanoid(idLength), ...req.body } req.app.db.get('books').push(book).write() res.send(book) }catch (error) { return res.status(500).send(error) } })
/** * @swagger * /books/{id}: * put: * summary: Update the book by the id * tags: [Books] * parameters: * - in: path * name: id * schema: * type: string * required: true * description: The book id * requestBody: * required: true * content: * application/json: * schema: * $ref: '#/components/schemas/Book' * responses: * 200: * description: The Book was updated * content: * application/json: * schema: * $ref: '#/components/schemas/Book' */ router.put('/:id', (req, res) => { try { req.app.db.get('books').find({ id: req.params.id }).assign(req.body).write() res.send(req.app.db.get('books')).find({id: req.params.id}) }catch (error) { return res.status(500).send(error) } })
/** * @swagger * /books/{id}: * delete: * summary: Update the book by the id * tags: [Books] * parameters: * - in: path * name: id * schema: * type: string * required: true * description: The book id * responses: * 200: * description: The Book was deleted * 404: * description: The Book was not found */