-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c22de77
commit 614d768
Showing
5 changed files
with
65 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
import { type NextFunction, type Request, type Response, Router } from 'express'; | ||
import { type NextFunction, type Request, RequestHandler, type Response, Router } from 'express'; | ||
import { asyncWrapperMiddleware } from '@myrotvorets/express-async-middleware-wrapper'; | ||
import { SearchService } from '../services/search.mjs'; | ||
import { type SearchItem, SearchService } from '../services/search.mjs'; | ||
import { environment } from '../lib/environment.mjs'; | ||
|
||
async function searchHandler(req: Request, res: Response, next: NextFunction): Promise<void> { | ||
const result = await SearchService.search(req.query.s as string); | ||
if (result === null) { | ||
next({ | ||
success: false, | ||
status: 400, | ||
code: 'BAD_SEARCH_TERM', | ||
message: 'Both surname and name are required', | ||
}); | ||
} else { | ||
res.json({ | ||
success: true, | ||
items: result, | ||
}); | ||
} | ||
type DefaultParams = Record<string, unknown>; | ||
|
||
interface SearchRequestParams { | ||
s: string; | ||
} | ||
|
||
interface SearchResponse { | ||
success: true; | ||
items: SearchItem[]; | ||
} | ||
|
||
function searchHandler( | ||
service: SearchService, | ||
): RequestHandler<DefaultParams, SearchResponse, never, SearchRequestParams> { | ||
return asyncWrapperMiddleware(async function _searchHandler( | ||
req: Request<DefaultParams, SearchResponse, never, SearchRequestParams>, | ||
res: Response<SearchResponse>, | ||
next: NextFunction, | ||
): Promise<void> { | ||
const result = await service.search(req.query.s); | ||
if (result === null) { | ||
next({ | ||
success: false, | ||
status: 400, | ||
code: 'BAD_SEARCH_TERM', | ||
message: 'Both surname and name are required', | ||
}); | ||
} else { | ||
res.json({ | ||
success: true, | ||
items: result, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export function searchController(): Router { | ||
const router = Router(); | ||
router.get('/search', asyncWrapperMiddleware(searchHandler)); | ||
const env = environment(); | ||
const service = new SearchService(env.IMAGE_CDN_PREFIX); | ||
|
||
router.get('/search', searchHandler(service)); | ||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters