Skip to content

Commit f9330d6

Browse files
Merge pull request #211 from learnsoftwaredevelopment/improved-search-software-api-endpoint
Improved `search` software API endpoint
2 parents 0ac7ec2 + dec51f8 commit f9330d6

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

controllers/api/searchController.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
const Software = require('../../models/software');
22

33
const searchSoftware = async (req, res) => {
4-
const { name } = req.query;
5-
const response = await Software.find({
4+
const page = parseInt(req.query.page, 10) || 0;
5+
const perPage = parseInt(req.query.per_page, 10) || 30;
6+
const query = {
67
name: {
7-
$regex: name,
8+
$regex: req.query.q,
89
$options: 'i',
910
},
11+
};
12+
13+
const queryResponse = await Software.find(query)
14+
.sort({ name: 'asc' })
15+
.skip(page * perPage)
16+
.limit(perPage);
17+
18+
const totalQueryResultCount = await Software.find(query).countDocuments();
19+
20+
return res.status(200).json({
21+
totalQueryResultCount,
22+
queryResponse,
1023
});
11-
return res.status(200).json(response);
1224
};
1325

1426
module.exports = {

models/software.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const softwareSchema = new mongoose.Schema(
3434
homePage: {
3535
type: String,
3636
validate: [
37-
(value) => isURL(value, {
38-
protocols: ['http', 'https'],
39-
}),
37+
(value) =>
38+
isURL(value, {
39+
protocols: ['http', 'https'],
40+
}),
4041
'A valid url is required',
4142
],
4243
required: [true, 'Software homepage url is required'],
@@ -64,17 +65,19 @@ const softwareSchema = new mongoose.Schema(
6465
developedBy: {
6566
type: [
6667
{
67-
type: mongoose.Schema.Types.ObjectId,
68-
ref: 'User',
68+
type: String,
69+
trim: true,
70+
lowercase: true,
6971
},
7072
],
7173
default: [],
7274
},
7375
maintainedBy: {
7476
type: [
7577
{
76-
type: mongoose.Schema.Types.ObjectId,
77-
ref: 'User',
78+
type: String,
79+
trim: true,
80+
lowercase: true,
7881
},
7982
],
8083
default: [],
@@ -87,19 +90,21 @@ const softwareSchema = new mongoose.Schema(
8790
updateUrl: {
8891
type: String,
8992
validate: [
90-
(value) => isURL(value, {
91-
protocols: ['http', 'https'],
92-
}) || value === '',
93+
(value) =>
94+
isURL(value, {
95+
protocols: ['http', 'https'],
96+
}) || value === '',
9397
'A valid update url is required',
9498
],
9599
default: '',
96100
},
97101
downloadUrl: {
98102
type: String,
99103
validate: [
100-
(value) => isURL(value, {
101-
protocols: ['http', 'https', 'ftp'],
102-
}) || value === '',
104+
(value) =>
105+
isURL(value, {
106+
protocols: ['http', 'https', 'ftp'],
107+
}) || value === '',
103108
'A valid download url is required',
104109
],
105110
default: '',
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
GET http://localhost:3001/api/search/software?name=sample
1+
GET http://localhost:3001/api/search/software?q=sample&per_page=1&page=0

0 commit comments

Comments
 (0)