|
| 1 | +import type { BookRepositoryPort } from '$lib/server/application/ports/BookRepositoryPort'; |
| 2 | +import type { MetadataQuery } from '$lib/server/application/ports/MetadataProviderPort'; |
| 3 | +import { |
| 4 | + MetadataAggregatorService, |
| 5 | + type MetadataAggregatorResult |
| 6 | +} from '$lib/server/application/services/MetadataAggregatorService'; |
| 7 | +import { apiError, apiOk, type ApiResult } from '$lib/server/http/api'; |
| 8 | + |
| 9 | +export interface SearchMetadataCandidatesInput { |
| 10 | + bookId?: number; |
| 11 | + query?: MetadataQuery; |
| 12 | +} |
| 13 | + |
| 14 | +export type SearchMetadataCandidatesResult = MetadataAggregatorResult; |
| 15 | + |
| 16 | +export class SearchMetadataCandidatesUseCase { |
| 17 | + constructor( |
| 18 | + private readonly aggregator: MetadataAggregatorService, |
| 19 | + private readonly bookRepository: BookRepositoryPort |
| 20 | + ) {} |
| 21 | + |
| 22 | + async execute( |
| 23 | + input: SearchMetadataCandidatesInput |
| 24 | + ): Promise<ApiResult<SearchMetadataCandidatesResult>> { |
| 25 | + if (input.bookId == null && input.query == null) { |
| 26 | + return apiError('Either bookId or query must be provided', 400); |
| 27 | + } |
| 28 | + |
| 29 | + let query: MetadataQuery; |
| 30 | + |
| 31 | + if (input.bookId != null) { |
| 32 | + const book = await this.bookRepository.getById(input.bookId); |
| 33 | + if (!book) { |
| 34 | + return apiError('Book not found', 404); |
| 35 | + } |
| 36 | + query = { |
| 37 | + title: book.title, |
| 38 | + author: book.author ?? null, |
| 39 | + isbn: book.identifier ?? null, |
| 40 | + language: book.language ?? null, |
| 41 | + googleBooksId: book.google_books_id ?? null, |
| 42 | + openLibraryKey: book.open_library_key ?? null, |
| 43 | + ...input.query |
| 44 | + }; |
| 45 | + } else { |
| 46 | + query = input.query!; |
| 47 | + } |
| 48 | + |
| 49 | + const result = await this.aggregator.lookup(query); |
| 50 | + return apiOk(result); |
| 51 | + } |
| 52 | +} |
0 commit comments