1+ const Book = require ( "../models/BookModel" ) ;
2+ const { body, validationResult } = require ( 'express-validator' ) ;
3+ const { sanitizeBody } = require ( 'express-validator' ) ;
4+ const apiResponse = require ( '../helpers/apiResponse' ) ;
5+ const auth = require ( '../middlewares/jwt' ) ;
6+ var moment = require ( 'moment' ) ;
7+ var mongoose = require ( 'mongoose' ) ;
8+
9+ function BookData ( data ) {
10+ this . title = data . title ;
11+ this . description = data . description ;
12+ this . isbn = data . isbn ;
13+ this . createdAt = data . createdAt ;
14+ }
15+
16+ /**
17+ * Book List.
18+ *
19+ * @returns {Object }
20+ */
21+ exports . bookList = [
22+ auth ,
23+ function ( req , res ) {
24+ try {
25+ Book . find ( { user : req . user . _id } , 'title description isbn createdAt' ) . then ( ( books ) => {
26+ if ( books . length > 0 ) {
27+ return apiResponse . successResponseWithData ( res , 'Operation success' , books ) ;
28+ } else {
29+ return apiResponse . successResponseWithData ( res , "Operation success" , { } ) ;
30+ }
31+ } ) ;
32+ } catch ( err ) {
33+ //throw error in json response with status 500.
34+ return apiResponse . ErrorResponse ( res , err ) ;
35+ }
36+ }
37+ ] ;
38+
39+ /**
40+ * Book Detail.
41+ *
42+ * @returns {Object }
43+ */
44+ exports . bookDetail = [
45+ auth ,
46+ function ( req , res ) {
47+ if ( ! mongoose . Types . ObjectId . isValid ( req . params . id ) ) {
48+ return apiResponse . successResponseWithData ( res , "Operation success" , { } ) ;
49+ }
50+ try {
51+ Book . findOne ( { _id : req . params . id , user : req . user . _id } , 'title description isbn createdAt' ) . then ( ( book ) => {
52+ if ( book !== null ) {
53+ let bookData = new BookData ( book ) ;
54+ return apiResponse . successResponseWithData ( res , 'Operation success' , bookData ) ;
55+ } else {
56+ return apiResponse . successResponseWithData ( res , "Operation success" , { } ) ;
57+ }
58+ } ) ;
59+ } catch ( err ) {
60+ //throw error in json response with status 500.
61+ return apiResponse . ErrorResponse ( res , err ) ;
62+ }
63+ }
64+ ] ;
65+
66+ /**
67+ * Store book.
68+ *
69+ * @param {string } title
70+ * @param {string } description
71+ * @param {string } isbn
72+ *
73+ * @returns {Object }
74+ */
75+ exports . bookStore = [
76+ auth ,
77+ body ( 'title' , 'Title must not be empty.' ) . isLength ( { min : 1 } ) . trim ( ) ,
78+ body ( 'description' , 'Description must not be empty.' ) . isLength ( { min : 1 } ) . trim ( ) ,
79+ body ( 'isbn' , 'ISBN must not be empty' ) . isLength ( { min : 1 } ) . trim ( ) . custom ( value => {
80+ return Book . findOne ( { isbn : value , user : req . user . _id } ) . then ( book => {
81+ if ( book ) {
82+ return Promise . reject ( 'Book already exist with this ISBN no.' ) ;
83+ }
84+ } ) ;
85+ } ) ,
86+ sanitizeBody ( '*' ) . escape ( ) ,
87+ ( req , res , next ) => {
88+ try {
89+ const errors = validationResult ( req ) ;
90+ var book = new Book (
91+ { title : req . body . title ,
92+ user : req . user ,
93+ description : req . body . description ,
94+ isbn : req . body . isbn
95+ } ) ;
96+
97+ if ( ! errors . isEmpty ( ) ) {
98+ return apiResponse . validationErrorWithData ( res , 'Validation Error.' , errors . array ( ) ) ;
99+ }
100+ else {
101+ //Save book.
102+ book . save ( function ( err ) {
103+ if ( err ) { return apiResponse . ErrorResponse ( res , err ) ; }
104+ let bookData = new BookData ( book ) ;
105+ return apiResponse . successResponseWithData ( res , 'Book add Success.' , bookData ) ;
106+ } ) ;
107+ }
108+ } catch ( err ) {
109+ //throw error in json response with status 500.
110+ return apiResponse . ErrorResponse ( res , err ) ;
111+ }
112+ }
113+ ] ;
114+
115+ /**
116+ * Store book.
117+ *
118+ * @param {string } title
119+ * @param {string } description
120+ * @param {string } isbn
121+ *
122+ * @returns {Object }
123+ */
124+ exports . bookUpdate = [
125+ auth ,
126+ body ( 'title' , 'Title must not be empty.' ) . isLength ( { min : 1 } ) . trim ( ) ,
127+ body ( 'description' , 'Description must not be empty.' ) . isLength ( { min : 1 } ) . trim ( ) ,
128+ body ( 'isbn' , 'ISBN must not be empty' ) . isLength ( { min : 1 } ) . trim ( ) ,
129+ sanitizeBody ( '*' ) . escape ( ) ,
130+ ( req , res , next ) => {
131+ try {
132+ const errors = validationResult ( req ) ;
133+ var book = new Book (
134+ { title : req . body . title ,
135+ description : req . body . description ,
136+ isbn : req . body . isbn ,
137+ _id :req . params . id
138+ } ) ;
139+
140+ if ( ! errors . isEmpty ( ) ) {
141+ return apiResponse . validationErrorWithData ( res , 'Validation Error.' , errors . array ( ) ) ;
142+ }
143+ else {
144+ if ( ! mongoose . Types . ObjectId . isValid ( req . params . id ) ) {
145+ return apiResponse . validationErrorWithData ( res , 'Invalid Error.' , "Invalid ID" ) ;
146+ }
147+ Book . findOne ( { isbn : req . body . isbn , user : req . user . _id , _id : { "$ne" : req . params . id } } ) . then ( book => {
148+ if ( book ) {
149+ return apiResponse . validationErrorWithData ( res , 'Validation Error.' , "Book already exist with this ISBN no." ) ;
150+ }
151+ } ) ;
152+ //Check authorized user
153+ Book . findById ( req . params . id , function ( err , book ) {
154+ if ( book . user . toString ( ) !== req . user . _id ) {
155+ return apiResponse . unauthorizedResponse ( res , 'You are not authorized to do this operation.' ) ;
156+ }
157+ } ) ;
158+ //Save book.
159+ Book . findByIdAndUpdate ( req . params . id , book , { } , function ( err ) {
160+ if ( err ) { return apiResponse . ErrorResponse ( res , err ) ; }
161+ let bookData = new BookData ( book ) ;
162+ return apiResponse . successResponseWithData ( res , 'Book update Success.' , bookData ) ;
163+ } ) ;
164+ }
165+ } catch ( err ) {
166+ //throw error in json response with status 500.
167+ return apiResponse . ErrorResponse ( res , err ) ;
168+ }
169+ }
170+ ] ;
0 commit comments