- auth: actionDeleteAccount 改用 service+repo,forgot-password 完整三层实现 - card: serviceCheckCardOwnership 替代直接调用 repository - deck: 移除 service 层的 use server 指令 - dictionary: 数据转换逻辑从 repository 移到 service - ocr: 认证移到 action 层,跨模块调用改用 service - translator: genIPA/genLanguage 改用 service 层
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { executeDictionaryLookup } from "@/lib/bigmodel/dictionary/orchestrator";
|
|
import { repoCreateLookUp, repoCreateLookUpWithItemAndEntries, repoSelectLastLookUpResult } from "./dictionary-repository";
|
|
import { ServiceInputLookUp } from "./dictionary-service-dto";
|
|
import { createLogger } from "@/lib/logger";
|
|
import { RepoOutputSelectLastLookUpResultItem } from "./dictionary-repository-dto";
|
|
|
|
const log = createLogger("dictionary-service");
|
|
|
|
function transformRawItemToSharedItem(rawItem: RepoOutputSelectLastLookUpResultItem) {
|
|
return {
|
|
id: rawItem.id,
|
|
standardForm: rawItem.standardForm,
|
|
entries: rawItem.entries.map(entry => ({
|
|
ipa: entry.ipa ?? undefined,
|
|
definition: entry.definition,
|
|
partOfSpeech: entry.partOfSpeech ?? undefined,
|
|
example: entry.example
|
|
}))
|
|
};
|
|
}
|
|
|
|
export const serviceLookUp = async (dto: ServiceInputLookUp) => {
|
|
const {
|
|
text,
|
|
queryLang,
|
|
userId,
|
|
definitionLang,
|
|
forceRelook
|
|
} = dto;
|
|
|
|
const lastLookUpResult = await repoSelectLastLookUpResult({
|
|
text,
|
|
queryLang,
|
|
definitionLang,
|
|
});
|
|
|
|
if (forceRelook || !lastLookUpResult) {
|
|
const response = await executeDictionaryLookup(
|
|
text,
|
|
queryLang,
|
|
definitionLang
|
|
);
|
|
|
|
repoCreateLookUpWithItemAndEntries(
|
|
{
|
|
standardForm: response.standardForm,
|
|
queryLang,
|
|
definitionLang
|
|
},
|
|
{
|
|
userId,
|
|
text,
|
|
queryLang,
|
|
definitionLang,
|
|
},
|
|
response.entries
|
|
).catch(error => {
|
|
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
|
|
});
|
|
|
|
return response;
|
|
} else {
|
|
const transformedResult = transformRawItemToSharedItem(lastLookUpResult);
|
|
|
|
repoCreateLookUp({
|
|
userId: userId,
|
|
text: text,
|
|
queryLang: queryLang,
|
|
definitionLang: definitionLang,
|
|
dictionaryItemId: transformedResult.id
|
|
}).catch(error => {
|
|
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
|
|
});
|
|
return {
|
|
standardForm: transformedResult.standardForm,
|
|
entries: transformedResult.entries
|
|
};
|
|
}
|
|
}; |