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

@ -12,6 +12,7 @@
"@types/jest": "^26.0.4", "@types/jest": "^26.0.4",
"@types/moment": "^2.13.0", "@types/moment": "^2.13.0",
"@types/mongoose": "^5.7.28", "@types/mongoose": "^5.7.28",
"@types/mongoose-paginate-v2": "^1.3.8",
"@types/multer": "^1.4.3", "@types/multer": "^1.4.3",
"@types/multer-s3": "^2.7.7", "@types/multer-s3": "^2.7.7",
"@types/mustache": "^4.0.1", "@types/mustache": "^4.0.1",
@ -32,6 +33,7 @@
"jest": "^26.1.0", "jest": "^26.1.0",
"moment": "^2.27.0", "moment": "^2.27.0",
"mongoose": "^5.9.20", "mongoose": "^5.9.20",
"mongoose-paginate-v2": "^1.3.9",
"multer": "^1.4.2", "multer": "^1.4.2",
"multer-s3": "^2.9.0", "multer-s3": "^2.9.0",
"multer-s3-v2": "^2.2.1", "multer-s3-v2": "^2.2.1",

View file

@ -10,7 +10,33 @@ import Papa from 'papaparse'
export default class AdminOrganizationController { export default class AdminOrganizationController {
static getMany(req: express.Request, res: express.Response) { 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) { static getOne(req: express.Request, res: express.Response) {
@ -204,6 +230,7 @@ export default class AdminOrganizationController {
* @param res * @param res
*/ */
static approve(req: express.Request, res: express.Response) { static approve(req: express.Request, res: express.Response) {
// (data.get('validationState') !== 'pending' && req.body.force == null)
Organization.findById(req.params.id).then(data => { Organization.findById(req.params.id).then(data => {
if ( if (
data === null || data === null ||

View file

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

View file

@ -698,6 +698,21 @@
"@types/bson" "*" "@types/bson" "*"
"@types/node" "*" "@types/node" "*"
"@types/mongoose-paginate-v2@^1.3.8":
version "1.3.8"
resolved "https://registry.yarnpkg.com/@types/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.8.tgz#e3eeb9817b5663bc62e5fec1aea9bb8df5482acc"
integrity sha512-76dmeOZ4//m0KvNINamY5zkYfEJPIVY8zK0HLXU+5E/h+v4ynQMA5z4hOJiCFUVIO50vENBrnQDQ2oC2RXqDEg==
dependencies:
"@types/mongoose" "*"
"@types/mongoose@*":
version "5.7.36"
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.36.tgz#2dae28c63041c6afba8a83ea02969f463b3f1021"
integrity sha512-ggFXgvkHgCNlT35B9d/heDYfSqOSwTmQjkRoR32sObGV5Xjd0N0WWuYlLzqeCg94j4hYN/OZxZ1VNNLltX/IVQ==
dependencies:
"@types/mongodb" "*"
"@types/node" "*"
"@types/mongoose@^5.7.28": "@types/mongoose@^5.7.28":
version "5.7.28" version "5.7.28"
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.28.tgz#99385892962e51649d3a40912f2a6f2629558d89" resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.28.tgz#99385892962e51649d3a40912f2a6f2629558d89"
@ -4530,6 +4545,11 @@ mongoose-legacy-pluralize@1.0.2:
resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz#3ba9f91fa507b5186d399fb40854bff18fb563e4" resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz#3ba9f91fa507b5186d399fb40854bff18fb563e4"
integrity sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ== integrity sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==
mongoose-paginate-v2@^1.3.9:
version "1.3.9"
resolved "https://registry.yarnpkg.com/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.9.tgz#dc0f58c22e061d78fc3a898195b884870a737c54"
integrity sha512-KXLmsTYDaS7zHqT45B2MZcCGzJtBySGANor5Xf6c0nU3y34xkRMqcDiVTizLd27KGqy5smqLe6LVNkTK994XGA==
mongoose@^5.9.20: mongoose@^5.9.20:
version "5.9.20" version "5.9.20"
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.9.20.tgz#975255196104ce760386ab439b1dad43caae23b5" resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.9.20.tgz#975255196104ce760386ab439b1dad43caae23b5"