Skip to content

feat: add search controller + search and reverseSearch methods #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/core/server/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import urlAliasController from './url-alias';
import urlPatternController from './url-pattern';
import infoController from './info';
import coreController from './core';
import searchController from './search';

export default {
'url-alias': urlAliasController,
'url-pattern': urlPatternController,
info: infoController,
core: coreController,
search: searchController,
};
80 changes: 80 additions & 0 deletions packages/core/server/controllers/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Context } from 'koa';
import { UID } from '@strapi/strapi';
import { isContentTypeEnabled } from '../util/enabledContentTypes';

/**
* Search controller
*/

export default {
search: async (ctx: Context & { params: { id: number } }) => {
const { q } = ctx.query;
const { id } = ctx.params;

Check warning on line 12 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'id' is assigned a value but never used

Check warning on line 12 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

'id' is assigned a value but never used
let results = [];

await Promise.all(Object.entries(strapi.contentTypes)
.map(async ([uid, config]: [UID.CollectionType, any]) => {

Check warning on line 16 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'config' is defined but never used

Check warning on line 16 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

'config' is defined but never used
const hasWT = isContentTypeEnabled(uid);

if (!hasWT) return;

const coreStoreSettings = await strapi.query('strapi::core-store').findMany({
where: {
key: `plugin_content_manager_configuration_content_types::${uid}`,
},
});

if (!coreStoreSettings[0]) return;

const value = JSON.parse(coreStoreSettings[0].value);

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .value on an `any` value

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe member access .value on an `any` value

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 29 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe assignment of an `any` value
const { mainField } = value.settings;

Check warning on line 30 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .settings on an `any` value

Check warning on line 30 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 30 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe member access .settings on an `any` value

Check warning on line 30 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe assignment of an `any` value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm thinking about it, it would be nice to make a dedicated service for finding the main field of content type. That should make it easy to reuse the code in both the endpoints.


const entries = await strapi.entityService.findMany(uid, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use the document service here.

filters: {
[mainField]: {
$containsi: q,
},
},
fields: [mainField],
populate: {
url_alias: {
fields: ['id'],
},
},
});

const entriesWithContentType = entries?.map((entry) => ({
...entry,
contentType: uid,
}));

// eslint-disable-next-line max-len
results = [...results, ...(Array.isArray(entriesWithContentType) ? entriesWithContentType : [])];

Check warning on line 52 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe spread of an `any` value in an array

Check warning on line 52 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe spread of an `any` value in an array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maby it is nicer if we do an early exit when there are no entries after fetching them. Then we don't have to check if entriesWithContentType is an array, we know it is.

}));

// @ts-ignore
ctx.body = results;
},
reverseSearch: async (ctx: Context & { params: { contentType: string; documentId: string } }) => {
const { contentType, documentId } = ctx.params;

// Zoek met filters via findMany omdat documentId geen primaire ID is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use English comments.

const [entry] = await strapi.entityService.findMany(contentType as UID, {

Check warning on line 62 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 62 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe assignment of an `any` value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use the document service here with a findOne.

filters: { documentId },
fields: ['id', 'title', 'documentId'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't assume the content type you're fetching here will always have a title field. You should use the same technique for finding the 'mainField' as it's done in the search controller.

limit: 1,
});

if (!entry) {
ctx.throw(404, 'Entry not found');
return;
}

ctx.body = {
id: entry.id,

Check warning on line 74 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 74 in packages/core/server/controllers/search.ts

View workflow job for this annotation

GitHub Actions / lint (22)

Unsafe assignment of an `any` value
title: entry.title,
slug: entry.slug,
documentId: entry.documentId,
};
},
};
22 changes: 22 additions & 0 deletions packages/core/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ export default {
policies: [],
},
},
/**
* Search routes
*/
{
method: 'GET',
path: '/search',
handler: 'search.search',
config: {
policies: [],
},
},
/**
* Reverse Search routes for a title or slug
*/
{
method: 'GET',
path: '/search/reverse/:contentType/:documentId',
handler: 'search.reverseSearch',
config: {
policies: [],
},
},
],
},
};
Loading