Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions src/api/dataset/controllers/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,14 @@ const _ = require('lodash');
module.exports = createCoreController('api::dataset.dataset', ({ strapi }) => ({
async find(ctx) {

// Check if 'collection' query parameter is present
const { collection } = ctx.query;
if (collection) {
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
where: { name: collection },
populate: { studies: { populate: { datasets: { select: ['id'] } } } }
});

const ids = _.flatMap(collectionEntry?.studies, s => s.datasets.map(d => d.id)) || [];
if (!ids?.length) { return this.transformResponse([], {
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
}); }

ctx.query.filters = {
...ctx.query.filters,
id: { $in: ids },
};
}

// If not providing a study id, return only datasets from studies that are listed
if (!ctx.query.filters?.study?.id?.$eq) {
ctx.query.filters = {
...ctx.query.filters,
study: {
is_listed: true },
...(ctx.query.filters?.study || {}),
is_listed: true,
},
};
}

Expand All @@ -57,6 +40,27 @@ module.exports = createCoreController('api::dataset.dataset', ({ strapi }) => ({
}
}
};

// Check if 'collection' query parameter is present
// Add to query filters last to avoid spreading the ids array into an object
const { collection } = ctx.query;
if (collection) {
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
where: { name: collection },
populate: { studies: { populate: { datasets: { select: ['id'] } } } }
});

const ids = _.flatMap(collectionEntry?.studies, s => s.datasets.map(d => d.id)) || [];
if (!ids?.length) { return this.transformResponse([], {
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
}); }

ctx.query.filters = {
...ctx.query.filters,
id: { $in: ids },
};
}

return await super.find(ctx);
},
async findOne(ctx) {
Expand Down
40 changes: 21 additions & 19 deletions src/api/study/controllers/study.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@ const { NotFoundError } = require('@strapi/utils').errors;
module.exports = createCoreController('api::study.study', ({ strapi }) => ({
async find(ctx) {

// Check if 'collection' query parameter is present
const { collection } = ctx.query;
if (collection) {
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
where: { name: collection },
populate: { studies: { select: ['id'] } },
});

const ids = collectionEntry?.studies.map(({ id }) => id) || [];
if (!ids?.length) { return this.transformResponse([], {
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
}); }

ctx.query.filters = {
...ctx.query.filters,
id: { $in: ids },
};
}

ctx.query.filters = {
...ctx.query.filters,
is_listed: true,
Expand Down Expand Up @@ -87,6 +68,27 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({
}
},
};

// Check if 'collection' query parameter is present
// Add to query filters last to avoid spreading the ids array into an object
const { collection } = ctx.query;
if (collection) {
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
where: { name: collection },
populate: { studies: { select: ['id'] } },
});

const ids = collectionEntry?.studies.map(({ id }) => id) || [];
if (!ids?.length) { return this.transformResponse([], {
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
}); }

ctx.query.filters = {
...ctx.query.filters,
id: { $in: ids },
};
}

return await super.find(ctx);
},
async findOne(ctx) {
Expand Down