@@ -2,6 +2,19 @@ const express = require('express');
22const router = express . Router ( ) ;
33const Book = require ( '../models/book' ) ;
44const auth = require ( '../middleware/auth' ) ;
5+ const Joi = require ( 'joi' ) ;
6+
7+ const bookSchema = Joi . object ( {
8+ title : Joi . string ( ) . required ( ) ,
9+ author : Joi . string ( ) . required ( ) ,
10+ publicationYear : Joi . number ( ) . integer ( ) . required ( ) ,
11+ isbn : Joi . string ( ) . required ( ) ,
12+ summary : Joi . string ( ) . required ( ) ,
13+ coverImage : Joi . string ( ) . uri ( ) . required ( ) ,
14+ category : Joi . string ( ) . required ( ) ,
15+ pageCount : Joi . number ( ) . integer ( ) . required ( ) ,
16+ language : Joi . string ( ) . required ( )
17+ } ) ;
518
619router . get ( '/' , auth , async ( req , res ) => {
720 try {
@@ -25,6 +38,10 @@ router.get('/:id', auth, async (req, res) => {
2538} ) ;
2639
2740router . post ( '/' , auth , async ( req , res ) => {
41+ const { error } = bookSchema . validate ( req . body ) ;
42+ if ( error ) {
43+ return res . status ( 400 ) . json ( { message : error . details [ 0 ] . message } ) ;
44+ }
2845 const book = new Book ( {
2946 title : req . body . title ,
3047 author : req . body . author ,
@@ -46,6 +63,10 @@ router.post('/', auth, async (req, res) => {
4663} ) ;
4764
4865router . put ( '/:id' , auth , async ( req , res ) => {
66+ const { error } = bookSchema . validate ( req . body ) ;
67+ if ( error ) {
68+ return res . status ( 400 ) . json ( { message : error . details [ 0 ] . message } ) ;
69+ }
4970 try {
5071 const book = await Book . findByIdAndUpdate ( req . params . id , req . body , { new : true } ) ;
5172 if ( ! book ) {
0 commit comments