Skip to content

Commit

Permalink
Merge pull request #40 from Turistforeningen/filters-and-paging
Browse files Browse the repository at this point in the history
Filters and paging
  • Loading branch information
Håvard Ranum authored Oct 7, 2016
2 parents 1daf911 + a1f1a8e commit 5ef4ebd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ router.get('/albums/:album/photos', (req, res, next) => {
},
};

if (req.query.page) {
opts.url = `${opts.url};p=${req.query.page}`;
}

const qs = [];

Object.keys(req.query).forEach(tag => {
Expand All @@ -189,9 +185,19 @@ router.get('/albums/:album/photos', (req, res, next) => {
}
});

/*
// If query has filtering queries, paging must be set a reqular query param.
// If not, paging is set as a semi colon param (second conditional).
*/
if (qs.length) {
opts.url = `${fotoweb.API_URL}/archives/${albumId.split('.')[0]}/`;
opts.url = `${opts.url}?${qs.join('&')}`;

if (req.query.page) {
opts.url = `${opts.url}&p=${req.query.page}`;
}
} else if (req.query.page) {
opts.url = `${opts.url};p=${req.query.page}`;
}

request.get(opts, (err, resp, body) => {
Expand Down
35 changes: 29 additions & 6 deletions lib/fotoweb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const PHOTO_MEDADATA = [
const PHOTO_METADATA = [
['q', 'query'],
['5', 'title'],
['20', 'albums'],
Expand All @@ -13,21 +13,42 @@ const PHOTO_MEDADATA = [
['222', 'area'],
];

const PHOTO_METADATA_KEYS = new Map(PHOTO_MEDADATA);
const PHOTO_METADATA_IDS = new Map(PHOTO_MEDADATA.map(key => key.reverse()));
const PHOTO_METADATA_KEYS = new Map(PHOTO_METADATA);
const PHOTO_METADATA_IDS = new Map(PHOTO_METADATA.map(key => [...key].reverse()));

const QUERY_PARAMS = [
['p', 'page'],
...PHOTO_METADATA,
];

const QUERY_PARAMS_KEYS = new Map(QUERY_PARAMS);
const QUERY_PARAMS_IDS = new Map(QUERY_PARAMS.map(key => [...key].reverse()));

const BASE_URL = process.env.FOTOWEB_BASE_URL;
const API_PATH = process.env.FOTOWEB_API_PATH;
const API_URL = [BASE_URL, API_PATH].join('');
const API_TOKEN = process.env.FOTOWEB_API_TOKEN;

const PAGING_REGEX = /p=([0-9]+)/;
const QUERY_PARAMS_REGEX = new RegExp(
`((${QUERY_PARAMS.map(key => key[0]).join('|')})=([^&]+))`, 'gi'
);

// Rebuild paging query string to use ID's from PHOTO_METADATA map
const parsePagingLinks = (endpoint, pages) => {
for (const key in pages) { // eslint-disable-line no-restricted-syntax
if (pages[key]) {
if (PAGING_REGEX.test(pages[key])) {
pages[key] = `${endpoint}?page=${pages[key].match(PAGING_REGEX)[1]}`;
if (QUERY_PARAMS_REGEX.test(pages[key])) {
// Find all FotoWeb query keys in paging URL
const keyQueryParams = pages[key].match(QUERY_PARAMS_REGEX) || [];
const idQueryParams = keyQueryParams.map(item => {
const arr = item.split('=');
// Replace FotoWeb key with Skadi ID
// Example: ['25', 'Menneske'] => ['tags', 'Menneske']
arr[0] = QUERY_PARAMS_KEYS.get(arr[0]);
return arr.join('=');
});

pages[key] = `${endpoint}?${idQueryParams.join('&')}`;
} else {
pages[key] = endpoint;
}
Expand All @@ -42,6 +63,8 @@ const parsePagingLinks = (endpoint, pages) => {
module.exports = {
PHOTO_METADATA_KEYS,
PHOTO_METADATA_IDS,
QUERY_PARAMS_KEYS,
QUERY_PARAMS_IDS,
BASE_URL,
API_PATH,
API_URL,
Expand Down
11 changes: 8 additions & 3 deletions test/integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,21 @@ describe('server', () => {

it('?query', function it(done) {
this.timeout(10000);
const queryStr = 'Flaatten';
const queryRe = new RegExp(queryStr, 'i');

const url = `/latest/albums/${albumId}/photos?query=Flaatten`;
const url = `/latest/albums/${albumId}/photos?query=${queryStr}`;

app.get(url)
.set('Origin', 'https://example1.com')
.expect(200)
.expect(res => res.body.data.forEach(data => {
assert(
/flaatten/i.test((data.metadata.photographers || []).join(' ')) ||
/flaatten/i.test((data.metadata.persons || ''))
queryRe.test((data.metadata.photographers || []).join(' ')) ||
queryRe.test((data.metadata.persons || '')) ||
queryRe.test((data.metadata.tags || []).join(' ')) ||
queryRe.test((data.metadata.description || '')) ||
queryRe.test((data.filename || ''))
);
}))
.end(done);
Expand Down

0 comments on commit 5ef4ebd

Please sign in to comment.