Skip to content

Commit e85fa45

Browse files
Filter
1 parent eea5b2a commit e85fa45

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/controllers/AuthoresController.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import notFound from "../Err/notfound.js";
2-
import {authors} from "../models/index.js";
2+
import {authors, books} from "../models/index.js";
33

44

55
class AuthorController{
@@ -23,6 +23,7 @@ class AuthorController{
2323
next(err);
2424
}
2525
};
26+
2627

2728
static listAuthorById = async (req, res, next) => {
2829
try {
@@ -63,6 +64,8 @@ class AuthorController{
6364
next(new notFound("ID author not found"));
6465
}
6566
};
67+
68+
6669
}
6770

6871
export default AuthorController;

src/controllers/booksController.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ class BookController{
1616
next(err);
1717
}
1818
};
19+
20+
static byfilter = async (req, res, next)=>{
21+
try {
22+
let {publisher, title} = req.query;
23+
24+
const regex = new RegExp(title, "i");
25+
26+
const search = {}
27+
28+
if (publisher) search.publisher = publisher;
29+
if (title) search.title = { $regex: title, $options: "i" };
30+
31+
const result = await books.find({
32+
"publisher":publisher,
33+
"title": title
34+
})
35+
res.staus(200).send(result)
36+
}
37+
catch(err){
38+
next(err)
39+
}
40+
}
1941

2042
//GET BY ID
2143
static listarLivroPorId = async (req, res, next) => {

src/models/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import globalValidator from "./globalValidator.js";
1+
//import globalValidator from "./globalValidator.js";
22
import books from "./Books.js";
33
import authors from "./Author.js";
44

src/routes/booksRoutes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const router = express.Router();
55

66
router
77
.get("/books",BookController.listBooks)
8+
.get("/books/serach", BookController.byfilter)
89
.get("/books/:id", BookController.listBookById)
910
.post("/books",BookController.registerBook)
1011
.put("/books/:id", BookController.updateBook)

0 commit comments

Comments
 (0)