Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added pagination to the GET /search endpoint ✨ #632

Merged
merged 2 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions backend/controllers/search.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,54 @@ const appResponse = require('../utils/appResponse');
const { BadRequestError } = require('../utils/appError');


const formatSearchData = data => {
return data.map(_ => {
return {
title: _.fileName || _.folderName,
description: _.type || _.description,
imageUrl: _.url ? _.url : null,
createdAt: _.description,
url: _.fileName ? `/companyfiles/previewFile/${_._id}` : `/companyfiles/folders/${_._id}`
}
})
}


const searchAndFilterFiles = async (req, res) => {
const { fileName, fileType } = req.query;

if (fileName && fileName.trim()) {
const page = parseInt(req.query.page, 10) || 1;
const limit = parseInt(req.query.limit, 10) || 10;
const startIndex = (page - 1) * limit;
const endIndex = page * limit;

let response;

if (fileType && fileType.trim()) {
response = await File.fetchByFilter({
fileName: { '$regex': fileName, '$options': 'i' }, type: { '$in': fileType.split(',') }
});
}, { skip: startIndex, limit });
} else {
response = await File.fetchByFilter({ fileName: { '$regex': fileName, '$options': 'i' } });
}

if (!response.length) {
return res.status(404).send(appResponse('File not found!', null, true));
response = await File.fetchByFilter({
fileName: { '$regex': fileName, '$options': 'i' }
}, { skip: startIndex, limit });
}

return res.status(200).send(appResponse('File results', response, true));
// Pagination
const total = response.length;
const next = (endIndex < total && limit <= total) ? { page: page + 1, limit } : {};
const previous = (startIndex > 0) ? { page: page - 1, limit } : {};

return res.status(200).send(appResponse('File search result', undefined, true, {
count: total,
page,
next,
previous,
plugin: 'Company Files',
query: fileName,
result: formatSearchData(response)
}));
} else {
return res.status(400).send(appResponse('Invalid fileName provided!', null));
}
Expand Down
4 changes: 3 additions & 1 deletion backend/utils/database.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DatabaseOps {
bulk_write: false,
object_id: '',
filter: {},
options: {},
payload: {}
}

Expand Down Expand Up @@ -61,8 +62,9 @@ class DatabaseOps {
return data;
}

fetchByFilter = async (filter = {}) => {
fetchByFilter = async (filter = {}, options = {}) => {
this.data.filter = filter;
this.data.options = options;

const { data } = await axios.post(`${databaseReadUrl}`, this.data);

Expand Down