Skip to content

Commit e936820

Browse files
-searchpublisher
1 parent 4e403b5 commit e936820

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

src/controllers/AuthoresController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class AuthorController{
2323
});
2424
};
2525

26-
2726
static registerAuthor = (req, res) => {
2827
let author = new authors(req.body);
2928
author.save()
@@ -76,6 +75,8 @@ class AuthorController{
7675
res.status(500).send({ message: err.message });
7776
}
7877
};
78+
79+
7980
}
8081

8182
export default AuthorController

src/controllers/booksController.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class BookController{
44

55
static listBooks = (req, res) => {
66
books.find()
7-
.populate('authors')
7+
.populate('author')
88
.exec()
99
.then((books) => {
1010
res.status(200).json(books);
@@ -78,6 +78,18 @@ class BookController{
7878
res.status(500).send({ message: err.message });
7979
}
8080
};
81+
82+
static listBooksByPublisher = async (req, res) => {
83+
try {
84+
const publisher = req.query.publisher;
85+
86+
const books = await books.find({'publisher': publisher});
87+
88+
res.status(200).send(books);
89+
} catch (err) {
90+
res.status(500).send(err.message);
91+
}
92+
}
8193
}
8294

8395
export default BookController

src/models/Author.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const authorSchema = new mongoose.Schema(
1111
}
1212
)
1313

14-
const authors = mongoose.model("authors", authorSchema);
14+
const author = mongoose.model("author", authorSchema);
1515

16-
export default authors
16+
export default author

src/models/Books.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import mongoose from 'mongoose'
33
const bookSchema = new mongoose.Schema({
44
id:{type:String},
55
title:{type:String, required:true},
6-
author:{type:mongoose.Schema.Types.ObjectId,ref:"authors", required:true},
6+
author:{type:mongoose.Schema.Types.ObjectId,ref:"author", required:true},
77
publisher:{type:String, require:true},
88
pageCount:{type:Number}
99
})

src/routes/booksRoutes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const router = express.Router()
66
router
77
.get('/books',BookController.listBooks)
88
.get('/books/:id', BookController.listBookById)
9+
.get("/books/searh",BookController.listBooksByPublisher)
910
.post('/books',BookController.registerBook)
1011
.put('/books/:id', BookController.updateBook)
1112
.delete('/books/:id',BookController.deleteBook)

0 commit comments

Comments
 (0)