diff --git a/README.md b/README.md index 4ae76c8..1444654 100644 --- a/README.md +++ b/README.md @@ -1552,7 +1552,7 @@ Returns concept scheme suggestions. Currently the same as `/voc/suggest` with parameter `format=jskos`. ### GET /data -Returns data for a certain URI or URIs. Can return concept schemes, concepts, concordances, mappings, and annotations. +Returns data for a certain URI or URIs. Can return concept schemes, concepts, concordances, mappings, and annotations. This endpoint does not offer pagination via `limit` and `offset`. It will always return all results. Furthermore, there is no certain order to the result set (but it should be consistent across requests). If a certain type of data requires authentication and the user is not authenticated, that type of data will not be returned. **Note:** As of version 2.0, this endpoint was adjusted to return all types of items that are available in the database, instead of just concepts and concept schemes. The additional parameters, apart from `uri`, were also removed. For the previous behavior (only without returning concept schemes), see [GET /concepts](#get-concepts). @@ -1560,6 +1560,8 @@ Returns data for a certain URI or URIs. Can return concept schemes, concepts, co `uri=[uri]` URIs for concepts or concept schemes separated by `|` + `properties=[list]` with `[list]` being a comma-separated list of properties (currently supporting `ancestors`, `narrower`, and `annotations`) + * **Success Response** JSON array of [JSKOS Items] diff --git a/routes/data.js b/routes/data.js index 18656c2..9de41d2 100644 --- a/routes/data.js +++ b/routes/data.js @@ -1,5 +1,5 @@ import express from "express" -import { conceptService } from "../services/concepts.js" +import { dataService } from "../services/data.js" import * as utils from "../utils/index.js" import * as auth from "../utils/auth.js" @@ -11,9 +11,7 @@ router.get( auth.optional, utils.supportDownloadFormats([]), utils.wrappers.async(async (req) => { - return await conceptService.getDetails(req.query) + return await dataService.getData(req) }), - utils.addPaginationHeaders, - utils.adjust, utils.returnJSON, ) diff --git a/services/data.js b/services/data.js new file mode 100644 index 0000000..ee51041 --- /dev/null +++ b/services/data.js @@ -0,0 +1,23 @@ +import { adjust } from "../utils/index.js" +import { byType as models } from "../models/index.js" + +export class DataService { + async getData(req) { + const uris = req.query.uri?.split("|") ?? [] + return [].concat(...await Promise.all(Object.keys(models).map(async type => { + // Don't return data the user is not authorized to read + if (!req.isAuthorizedFor({ type: `${type}s`, action: "read" })) { + return [] + } + const model = models[type] + const results = await model.find({ $or: [ + { uri: { $in: uris } }, + { identifier: { $in: uris } }, + ] }).lean() + // Return adjusted data (needs to be done separately for each data type) + return adjust.data({ req, data: results, type: `${type}s` }) + }))) + } +} + +export const dataService = new DataService() diff --git a/services/index.js b/services/index.js index 75afb8c..eabc1a2 100644 --- a/services/index.js +++ b/services/index.js @@ -3,6 +3,7 @@ import { conceptService } from "./concepts.js" import { concordanceService } from "./concordances.js" import { mappingService } from "./mappings.js" import { schemeService } from "./schemes.js" +import { dataService } from "./data.js" import { statusService } from "./status.js" import { validateService } from "./validate.js" @@ -12,6 +13,7 @@ export { concordanceService, mappingService, schemeService, + dataService, statusService, validateService, }