Added mocks for all model types, added CollectionDataService

This commit is contained in:
Art Lowel
2017-02-14 14:40:57 +01:00
parent c3214595ef
commit c8fb98760d
30 changed files with 1129 additions and 162 deletions

View File

@@ -6,6 +6,8 @@ import { fakeDataBase } from './db';
import { fakeDemoRedisCache } from './cache';
import { COLLECTIONS } from "./collections";
import { ITEMS } from "./items";
import { BUNDLES } from "./bundles";
import { BITSTREAMS } from "./bitstreams";
import { METADATA } from "./metadata";
// you would use cookies/token etc
@@ -30,14 +32,10 @@ export function serverApi(req, res) {
}
let COLLECTION_COUNT = 2;
let ITEM_COUNT = 2;
function toJSONAPIResponse(req, data, included?) {
function toHALResponse(req, data, included?) {
let result = {
"data": data,
"links": {
"_embedded": data,
"_links": {
"self": req.protocol + '://' + req.get('host') + req.originalUrl
}
};
@@ -58,7 +56,7 @@ export function createMockApi() {
console.log('GET');
// 70ms latency
setTimeout(function() {
res.json(toJSONAPIResponse(req, COLLECTIONS));
res.json(toHALResponse(req, COLLECTIONS));
}, 0);
// })
@@ -84,7 +82,7 @@ export function createMockApi() {
try {
req.collection_id = id;
req.collection = COLLECTIONS.find((collection) => {
return collection.id = id;
return collection.id === id;
});
next();
} catch (e) {
@@ -95,7 +93,7 @@ export function createMockApi() {
router.route('/collections/:collection_id')
.get(function(req, res) {
console.log('GET', util.inspect(req.collection, { colors: true }));
res.json(toJSONAPIResponse(req, req.collection));
res.json(toHALResponse(req, req.collection));
// })
// .put(function(req, res) {
// console.log('PUT', util.inspect(req.body, { colors: true }));
@@ -120,7 +118,7 @@ export function createMockApi() {
console.log('GET');
// 70ms latency
setTimeout(function() {
res.json(toJSONAPIResponse(req, ITEMS));
res.json(toHALResponse(req, ITEMS));
}, 0);
// })
@@ -161,7 +159,7 @@ export function createMockApi() {
const itemMetadata: any[] = METADATA.filter((metadatum) => {
return metadataIds.indexOf(metadatum.id) >= 0
});
res.json(toJSONAPIResponse(req, req.item, itemMetadata));
res.json(toHALResponse(req, req.item, itemMetadata));
// })
// .put(function(req, res) {
// console.log('PUT', util.inspect(req.body, { colors: true }));
@@ -180,5 +178,72 @@ export function createMockApi() {
// res.json(req.item);
});
router.route('/bundles')
.get(function(req, res) {
console.log('GET');
// 70ms latency
setTimeout(function() {
res.json(toHALResponse(req, BUNDLES));
}, 0);
});
router.param('bundle_id', function(req, res, next, bundle_id) {
// ensure correct prop type
let id = req.params.bundle_id;
try {
req.bundle_id = id;
req.bundle = BUNDLES.find((bundle) => {
return bundle.id === id;
});
next();
} catch (e) {
next(new Error('failed to load item'));
}
});
router.route('/bundles/:bundle_id')
.get(function(req, res) {
console.log('GET', util.inspect(req.bundle, { colors: true }));
const metadataIds: string[] = req.bundle.relationships.metadata.data.map(obj => obj.id);
const bundleMetadata: any[] = METADATA.filter((metadatum) => {
return metadataIds.indexOf(metadatum.id) >= 0
});
res.json(toHALResponse(req, req.bundle, bundleMetadata));
});
router.route('/bitstreams')
.get(function(req, res) {
console.log('GET');
// 70ms latency
setTimeout(function() {
res.json(toHALResponse(req, BITSTREAMS));
}, 0);
});
router.param('bitstream_id', function(req, res, next, bitstream_id) {
// ensure correct prop type
let id = req.params.bitstream_id;
try {
req.bitstream_id = id;
req.bitstream = BITSTREAMS.find((bitstream) => {
return bitstream.id === id;
});
next();
} catch (e) {
next(new Error('failed to load item'));
}
});
router.route('/bitstreams/:bitstream_id')
.get(function(req, res) {
console.log('GET', util.inspect(req.bitstream, { colors: true }));
const metadataIds: string[] = req.bitstream.relationships.metadata.data.map(obj => obj.id);
const bitstreamMetadata: any[] = METADATA.filter((metadatum) => {
return metadataIds.indexOf(metadatum.id) >= 0
});
res.json(toHALResponse(req, req.bitstream, bitstreamMetadata));
});
return router;
}