@@ -3,26 +3,65 @@ const Book = require("../models/book.model");
33async function getAllBooks ( req , res , next ) {
44 try {
55 const books = await Book . find ( { } ) ;
6- res . json ( { data : books } ) ;
6+ res . json ( { data : books } ) ;
77 } catch ( e ) {
88 next ( e ) ;
99 }
1010}
1111
1212async function createBook ( req , res , next ) {
1313 try {
14- const { title, description} = req . body ;
15- const result = await Book . create ( {
16- title, description
17- } ) ;
14+ const { ...rest } = req . body ;
15+ const result = await Book . create ( rest ) ;
1816
19- res . json ( { success : true , data : result } ) ;
17+ res . json ( { success : true , data : result } ) ;
2018 } catch ( e ) {
2119 next ( e ) ;
2220 }
23- } ;
21+ }
22+
23+ async function deleteBook ( req , res , next ) {
24+ try {
25+ const { book_id : bookID } = req . params ;
26+
27+ const result = await Book . deleteOne ( { _id : bookID } )
28+
29+ res . json ( { success : true , data : result } ) ;
30+ } catch ( e ) {
31+ next ( e ) ;
32+ }
33+ }
34+
35+ async function updateBook ( req , res , next ) {
36+ try {
37+ const { book_id : bookID } = req . params ;
38+ const { ...rest } = req . body ;
39+
40+ const result = await Book . updateOne ( { _id : bookID } , {
41+ ...rest
42+ } )
43+
44+ res . json ( { success : true , data : result } ) ;
45+ } catch ( e ) {
46+ next ( e ) ;
47+ }
48+ }
49+ async function getBookById ( req , res , next ) {
50+ try {
51+ const { book_id : bookID } = req . params ;
52+
53+ const result = await Book . findOne ( { _id : bookID } ) ;
54+
55+ res . json ( { success : true , data : result } ) ;
56+ } catch ( e ) {
57+ next ( e ) ;
58+ }
59+ }
2460
2561module . exports = {
2662 getAllBooks,
27- createBook
63+ createBook,
64+ deleteBook,
65+ updateBook,
66+ getBookById,
2867}
0 commit comments