feat(Admin): add pagination support for getMany organization route

This commit is contained in:
Matthieu Bessat 2020-08-16 11:29:22 +02:00
parent db2414b8b8
commit 3670a53f51
4 changed files with 53 additions and 2 deletions

View file

@ -10,7 +10,33 @@ import Papa from 'papaparse'
export default class AdminOrganizationController {
static getMany(req: express.Request, res: express.Response) {
Organization.find().then(data => res.json({ success: true, data }))
//const limit: number = req.query.limit == null ? '1' : req.query.limit
let limit: any = req.query.limit == null ? '10' : req.query.limit
limit = parseInt(limit)
let page: any = req.query.page == null ? '1' : req.query.page
page = parseInt(page)
let paginationOptions: any = {}
console.log(page, limit)
if (limit !== -1) {
paginationOptions = {
page, limit
}
} else {
paginationOptions = { pagination: false }
}
let sortBy: any = req.query.sortBy == null ? 'validationState' : req.query.sortBy
let sortWay: any = req.query.sortDesc == null ? 1 : (req.query.sortDesc == 'true' ? -1 : 1)
let sort: any = {}
sort[sortBy] = sortWay
console.log(sort, paginationOptions)
Organization.paginate({}, {
sort,
...paginationOptions
}).then(data => res.json({
success: true,
data
})
)
}
static getOne(req: express.Request, res: express.Response) {
@ -204,6 +230,7 @@ export default class AdminOrganizationController {
* @param res
*/
static approve(req: express.Request, res: express.Response) {
// (data.get('validationState') !== 'pending' && req.body.force == null)
Organization.findById(req.params.id).then(data => {
if (
data === null ||

View file

@ -1,5 +1,5 @@
import mongoose, { Schema } from 'mongoose'
import { Tag } from './Tag'
import mongoosePaginate from 'mongoose-paginate-v2'
interface AllowedStringOptions {
name: string;
@ -111,4 +111,6 @@ const Organization = new Schema({
publishedAt: { type: Date },
})
Organization.plugin(mongoosePaginate)
export default mongoose.model('Organization', Organization)