feat: add public search service and routes
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
function coerceNumber(value, fallback) {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = Number.parseInt(String(value), 10);
|
||||
return Number.isNaN(parsed) ? fallback : parsed;
|
||||
}
|
||||
|
||||
export async function registerPublicSearchRoutes(app, options) {
|
||||
const { searchService } = options;
|
||||
|
||||
app.get('/public/search', async (request) => {
|
||||
if (!searchService || typeof searchService.searchPublic !== 'function') {
|
||||
throw createApplicationError(
|
||||
'SEARCH_SERVICE_UNAVAILABLE',
|
||||
'Public search is currently unavailable.',
|
||||
503,
|
||||
);
|
||||
}
|
||||
|
||||
const filters = {
|
||||
query: request.query.query,
|
||||
category: request.query.category,
|
||||
minPriceMinor: coerceNumber(request.query.minPriceMinor, undefined),
|
||||
maxPriceMinor: coerceNumber(request.query.maxPriceMinor, undefined),
|
||||
page: coerceNumber(request.query.page, 1),
|
||||
limit: coerceNumber(request.query.limit, 20),
|
||||
includeSold: request.query.includeSold === 'true' || request.query.includeSold === true,
|
||||
};
|
||||
|
||||
return { data: await searchService.searchPublic(filters) };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user