-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
|
||
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
|
||
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
|
||
const { mainField } = value.settings; | ||
Check warning on line 30 in packages/core/server/controllers/search.ts
|
||
|
||
const entries = await strapi.entityService.findMany(uid, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
})); | ||
|
||
// @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably use the document service here with a |
||
filters: { documentId }, | ||
fields: ['id', 'title', 'documentId'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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
|
||
title: entry.title, | ||
slug: entry.slug, | ||
documentId: entry.documentId, | ||
}; | ||
}, | ||
}; |
There was a problem hiding this comment.
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.