Skip to content

Commit

Permalink
Implememnt fragment references to AfO Reg. endpoint & injection
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Dec 10, 2023
1 parent 07378b1 commit 0a8e072
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
34 changes: 17 additions & 17 deletions src/afo-register/infrastructure/AfoRegisterRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ function createAfoRegisterRecordSuggestion(data): AfoRegisterRecordSuggestion {
return new AfoRegisterRecordSuggestion(data)
}

function injectFragmentReferenceToRecord(
record: AfoRegisterRecord,
fragmentService: FragmentService
): Promise<AfoRegisterRecord> {
const { text, textNumber } = record
return fragmentService
.query({ traditionalReferences: text + ' ' + textNumber })
.then((queryResult) => {
return record.setFragmentNumbers(
queryResult.items.map((item) => item.museumNumber)
)
})
}

function injectFragmentReferencesToRecords(
records: AfoRegisterRecord[],
fragmentService: FragmentService
): Promise<AfoRegisterRecord>[] {
return records.map((record) =>
injectFragmentReferenceToRecord(record, fragmentService)
): Promise<AfoRegisterRecord[]> {
const traditionalReferences = records.map(
({ text, textNumber }: AfoRegisterRecord) => text + ' ' + textNumber
)
return fragmentService
.queryByTraditionalReferences(traditionalReferences)
.then((result) =>
records.map((record) => {
const match = result.items.find(
(item) =>
item.traditionalReference === record.text + ' ' + record.textNumber
)
if (match) {
return record.setFragmentNumbers(match.fragmentNumbers)
}
return record
})
)
}

export default class AfoRegisterRepository {
Expand Down
8 changes: 2 additions & 6 deletions src/afo-register/ui/AfoRegisterSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ export default withData<
AfoRegisterSearch,
(props) =>
props.afoRegisterService.search(
stringify(props.query)
// This is temporarly disabled.
// ToDo:
// Pass here `props.fragmentService` as the second parameter again
// when the new, fast batch endpoint for `traditionalReferences`
// is in service.
stringify(props.query),
props.fragmentService
),
{
watch: (props) => [props.query],
Expand Down
10 changes: 10 additions & 0 deletions src/fragmentarium/application/FragmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export interface FragmentRepository {
fetchCdliInfo(cdliNumber: string): Bluebird<CdliInfo>
lineToVecRanking(number: string): Bluebird<LineToVecRanking>
query(fragmentQuery: FragmentQuery): Bluebird<QueryResult>
queryByTraditionalReferences(
traditionalReferences: string[]
): Bluebird<QueryResult>
listAllFragments(): Bluebird<string[]>
}

Expand Down Expand Up @@ -333,6 +336,13 @@ export class FragmentService {
return this.fragmentRepository.query(fragmentQuery)
}

queryByTraditionalReferences(traditionalReferences: string[]): Bluebird<any> {
// ToDo: Add type
return this.fragmentRepository.queryByTraditionalReferences(
traditionalReferences
)
}

private injectReferences(fragment: Fragment): Bluebird<Fragment> {
return this.referenceInjector
.injectReferencesToText(fragment.text)
Expand Down
11 changes: 11 additions & 0 deletions src/fragmentarium/infrastructure/FragmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ class ApiFragmentRepository
}))
}

queryByTraditionalReferences(traditionalReferences: string[]): Promise<any> {
// ToDo: Add type
return this.apiClient.postJson(
`/fragments/query-by-traditional-references`,
{
traditionalReferences,
},
false
)
}

listAllFragments(): Promise<string[]> {
return this.apiClient.fetchJson(`/fragments/all`, false)
}
Expand Down
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ const errorReporter = new SentryErrorReporter()

export type JsonApiClient = {
fetchJson: (url: string, authorize: boolean) => Promise<any>
postJson: (url: string, body: Record<string, unknown>) => Promise<any>
postJson: (
url: string,
body: Record<string, unknown>,
authorize?: boolean
) => Promise<any>
}

function InjectedApp(): JSX.Element {
Expand Down

0 comments on commit 0a8e072

Please sign in to comment.