diff --git a/modules/hudozka-api/src/controllers/page.ts b/modules/hudozka-api/src/controllers/page.ts index 33ea8155..6544620c 100644 --- a/modules/hudozka-api/src/controllers/page.ts +++ b/modules/hudozka-api/src/controllers/page.ts @@ -1,9 +1,25 @@ import { Request, Response } from 'express' import Page from '../core/Page' -import { dropTrailingSlash, getPathWaterfall, readQueryArray, readQueryInt } from '../lib/url' +import { dropTrailingSlash, getPathWaterfall, readQueryArray, readQueryInt, readQueryString } from '../lib/url' import { encodePage } from '../factory/page' import * as breadcrumb from './breadcrumb' +function getSort(sortBy: string) { + if (!sortBy) { + return null + } + + if (sortBy.charAt(0) === '-') { + return { + [sortBy.substr(1, sortBy.length)]: -1, + } + } + + return { + [sortBy]: 1, + } +} + async function resolveItem(page: Page) { const breadcrumbs = await breadcrumb.getItems(page.getUrl()) page.setBreadcrumb(breadcrumbs) @@ -44,6 +60,8 @@ export async function getAll(req: Request, res: Response) { export async function getByTags(req: Request, res: Response) { const skip = readQueryInt(req, 'skip') const limit = readQueryInt(req, 'limit') + const sortBy = readQueryString(req, 'sortBy') + const sort = getSort(sortBy) const tags = readQueryArray(req, 'tag') if (tags.length === 0) { @@ -53,7 +71,7 @@ export async function getByTags(req: Request, res: Response) { }) } - const options = { skip, limit } + const options = { skip, limit, sort } const models = await Page.find({ tags: { $in: ['event', 'album'] }, }, options) diff --git a/modules/hudozka-api/src/lib/url.ts b/modules/hudozka-api/src/lib/url.ts index 4ac3bb88..f7e0ea79 100644 --- a/modules/hudozka-api/src/lib/url.ts +++ b/modules/hudozka-api/src/lib/url.ts @@ -39,6 +39,10 @@ export function readQueryInt(req: Request, key: string) { return int(req.query[key]) || 0 } +export function readQueryString(req: Request, key: string) { + return `${req.query[key]}` +} + export function readQueryArray(req: Request, key: string) { return ensureArray(req.query[key]) }