1- const mongoose = require ( 'mongoose' ) ;
2- const db = require ( './dbModel' ) ;
1+ // const mongoose = require('mongoose');
2+ let db = [ ] ;
33
4- mongoose . set ( 'useFindAndModify' , false ) ;
4+ // mongoose.set('useFindAndModify', false);
55
66const bookController = { } ;
77
88bookController . clearDB = ( req , res , next ) => {
9- db . BookStore . deleteMany ( { } , ( err ) => {
10- if ( err ) next ( err ) ;
11- next ( )
12- } ) ;
9+ db = [ ] ;
10+ return next ( )
1311}
1412
1513bookController . getAll = ( req , res , next ) => {
16- db . BookStore . find ( { } , ( err , books ) => {
17- if ( err ) {
18- next ( err ) ;
19- }
20- res . locals . books = books ;
21- // console.log(books);
22- next ( ) ;
23- } )
14+ res . locals . books = db ;
15+ return next ( ) ;
2416}
2517
2618bookController . addBook = ( req , res , next ) => {
2719 const { title, author, pages } = req . body ;
28- db . BookStore . create ( { title, author, pages } , ( err , books ) => {
29- if ( err ) {
30- next ( err ) ;
31- }
32- res . locals . books = books ;
33- next ( ) ;
34- } )
20+ const book = { title, author, pages} ;
21+ db . push ( book ) ;
22+ res . locals . books = db ;
23+ return next ( ) ;
3524}
3625
3726bookController . updateEntireBook = ( req , res , next ) => {
3827 const { title } = req . params ;
3928 const { author, pages } = req . body ;
40- db . BookStore . replaceOne ( { title } , { title, author, pages } , ( err , books ) => {
41- if ( err ) {
42- next ( err ) ;
43- }
44- if ( books . n === 1 ) {
45- db . BookStore . findOne ( { title} , ( err , books ) => {
46- if ( err ) {
47- next ( err ) ;
48- }
49-
50- res . locals . books = books ;
51- next ( ) ;
52- } )
53- }
54- } )
29+ const book = db . filter ( book => book . title === title ) ;
30+ book [ 0 ] . title = title ;
31+ book [ 0 ] . author = author ;
32+ book [ 0 ] . pages = pages ;
33+ db = [ ...db , ...book ] ;
34+ res . locals . books = db ;
35+ return next ( ) ;
5536}
5637
5738bookController . patchBook = ( req , res , next ) => {
5839 const { title } = req . params ;
5940 const { author } = req . body ;
60- db . BookStore . findOneAndUpdate ( { title } , { title, author } , { new : true } , ( err , books ) => {
61- if ( err ) {
62- next ( err ) ;
63- }
64- res . locals . books = books ;
65- next ( ) ;
66- } )
41+ const book = db . filter ( book => book . title === title ) ;
42+ book [ 0 ] . title = title ;
43+ book [ 0 ] . author = author ;
44+ db = [ ...db , ...book ] ;
45+ res . locals . books = db ;
46+ return next ( ) ;
6747}
6848
6949bookController . deleteBook = ( req , res , next ) => {
7050 const { title } = req . params ;
71- db . BookStore . findOneAndDelete ( { title } , ( err , books ) => {
72- if ( err ) {
73- next ( err ) ;
74- }
75- res . locals . books = books ;
76- next ( ) ;
77- } )
51+ const book = db . filter ( book => book . title === title ) [ 0 ] ;
52+ db = db . filter ( book => book . title !== title ) ;
53+ res . locals . books = book ;
54+ return next ( ) ;
7855}
7956
8057module . exports = bookController ;
0 commit comments