This commit is contained in:
2026-01-14 16:57:35 +08:00
parent 804baa64b2
commit ec265be26b
38 changed files with 585 additions and 294 deletions

View File

@@ -1,29 +1,21 @@
import { ValidateError } from "@/lib/errors";
import { TSharedItem } from "@/shared";
import { LENGTH_MAX_DICTIONARY_TEXT, LENGTH_MAX_LANGUAGE, LENGTH_MIN_DICTIONARY_TEXT, LENGTH_MIN_LANGUAGE } from "@/shared/constant";
import { generateValidator } from "@/utils/validate";
import z from "zod";
const DictionaryActionInputDtoSchema = z.object({
text: z.string().min(1, 'Empty text.').max(30, 'Text too long.'),
queryLang: z.string().min(1, 'Query lang too short.').max(20, 'Query lang too long.'),
const schemaActionInputLookUpDictionary = z.object({
text: z.string().min(LENGTH_MIN_DICTIONARY_TEXT).max(LENGTH_MAX_DICTIONARY_TEXT),
queryLang: z.string().min(LENGTH_MIN_LANGUAGE).max(LENGTH_MAX_LANGUAGE),
forceRelook: z.boolean(),
definitionLang: z.string().min(1, 'Definition lang too short.').max(20, 'Definition lang too long.'),
definitionLang: z.string().min(LENGTH_MIN_LANGUAGE).max(LENGTH_MAX_LANGUAGE),
userId: z.string().optional()
});
export type DictionaryActionInputDto = z.infer<typeof DictionaryActionInputDtoSchema>;
export type ActionInputLookUpDictionary = z.infer<typeof schemaActionInputLookUpDictionary>;
export const validateDictionaryActionInput = (dto: DictionaryActionInputDto): DictionaryActionInputDto => {
const result = DictionaryActionInputDtoSchema.safeParse(dto);
if (result.success) return result.data;
export const validateActionInputLookUpDictionary = generateValidator(schemaActionInputLookUpDictionary);
const errorMessages = result.error.issues.map((issue) =>
`${issue.path.join('.')}: ${issue.message}`
).join('; ');
throw new ValidateError(`Validation failed: ${errorMessages}`);
};
export type DictionaryActionOutputDto = {
export type ActionOutputLookUpDictionary = {
message: string,
success: boolean;
data?: TSharedItem;

View File

@@ -1,15 +1,15 @@
"use server";
import { DictionaryActionInputDto, DictionaryActionOutputDto, validateDictionaryActionInput } from "./dictionary-action-dto";
import { ActionInputLookUpDictionary, ActionOutputLookUpDictionary, validateActionInputLookUpDictionary } from "./dictionary-action-dto";
import { ValidateError } from "@/lib/errors";
import { lookUpService } from "./dictionary-service";
import { serviceLookUp } from "./dictionary-service";
export const lookUpDictionaryAction = async (dto: DictionaryActionInputDto): Promise<DictionaryActionOutputDto> => {
export const actionLookUpDictionary = async (dto: ActionInputLookUpDictionary): Promise<ActionOutputLookUpDictionary> => {
try {
return {
message: 'success',
success: true,
data: await lookUpService(validateDictionaryActionInput(dto))
data: await serviceLookUp(validateActionInputLookUpDictionary(dto))
};
} catch (e) {
if (e instanceof ValidateError) {

View File

@@ -1,6 +1,6 @@
import { TSharedItem } from "@/shared";
export type CreateDictionaryLookUpInputDto = {
export type RepoInputCreateDictionaryLookUp = {
userId?: string;
text: string;
queryLang: string;
@@ -8,15 +8,15 @@ export type CreateDictionaryLookUpInputDto = {
dictionaryItemId?: number;
};
export type SelectLastLookUpResultOutputDto = TSharedItem & {id: number} | null;
export type RepoOutputSelectLastLookUpResult = TSharedItem & {id: number} | null;
export type CreateDictionaryItemInputDto = {
export type RepoInputCreateDictionaryItem = {
standardForm: string;
queryLang: string;
definitionLang: string;
};
export type CreateDictionaryEntryInputDto = {
export type RepoInputCreateDictionaryEntry = {
itemId: number;
ipa?: string;
definition: string;
@@ -24,14 +24,14 @@ export type CreateDictionaryEntryInputDto = {
example: string;
};
export type CreateDictionaryEntryWithoutItemIdInputDto = {
export type RepoInputCreateDictionaryEntryWithoutItemId = {
ipa?: string;
definition: string;
partOfSpeech?: string;
example: string;
};
export type SelectLastLookUpResultInputDto = {
export type RepoInputSelectLastLookUpResult = {
text: string,
queryLang: string,
definitionLang: string;

View File

@@ -1,15 +1,15 @@
import { stringNormalize } from "@/utils/string";
import {
CreateDictionaryEntryInputDto,
CreateDictionaryEntryWithoutItemIdInputDto,
CreateDictionaryItemInputDto,
CreateDictionaryLookUpInputDto,
SelectLastLookUpResultInputDto,
SelectLastLookUpResultOutputDto,
RepoInputCreateDictionaryEntry,
RepoInputCreateDictionaryEntryWithoutItemId,
RepoInputCreateDictionaryItem,
RepoInputCreateDictionaryLookUp,
RepoInputSelectLastLookUpResult,
RepoOutputSelectLastLookUpResult,
} from "./dictionary-repository-dto";
import prisma from "@/lib/db";
export async function selectLastLookUpResult(dto: SelectLastLookUpResultInputDto): Promise<SelectLastLookUpResultOutputDto> {
export async function repoSelectLastLookUpResult(dto: RepoInputSelectLastLookUpResult): Promise<RepoOutputSelectLastLookUpResult> {
const result = await prisma.dictionaryLookUp.findFirst({
where: {
normalizedText: stringNormalize(dto.text),
@@ -48,16 +48,16 @@ export async function selectLastLookUpResult(dto: SelectLastLookUpResultInputDto
return null;
}
export async function createLookUp(content: CreateDictionaryLookUpInputDto) {
export async function repoCreateLookUp(content: RepoInputCreateDictionaryLookUp) {
return (await prisma.dictionaryLookUp.create({
data: { ...content, normalizedText: stringNormalize(content.text) }
})).id;
}
export async function createLookUpWithItemAndEntries(
itemData: CreateDictionaryItemInputDto,
lookUpData: CreateDictionaryLookUpInputDto,
entries: CreateDictionaryEntryWithoutItemIdInputDto[]
export async function repoCreateLookUpWithItemAndEntries(
itemData: RepoInputCreateDictionaryItem,
lookUpData: RepoInputCreateDictionaryLookUp,
entries: RepoInputCreateDictionaryEntryWithoutItemId[]
) {
return await prisma.$transaction(async (tx) => {
const item = await tx.dictionaryItem.create({

View File

@@ -1,6 +1,6 @@
import { TSharedItem } from "@/shared";
export type LookUpServiceInputDto = {
export type ServiceInputLookUp = {
text: string,
queryLang: string,
definitionLang: string,
@@ -8,4 +8,4 @@ export type LookUpServiceInputDto = {
userId?: string;
};
export type LookUpServiceOutputDto = TSharedItem;
export type ServiceOutputLookUp = TSharedItem;

View File

@@ -1,8 +1,8 @@
import { executeDictionaryLookup } from "@/lib/bigmodel/dictionary";
import { createLookUp, createLookUpWithItemAndEntries, selectLastLookUpResult } from "./dictionary-repository";
import { LookUpServiceInputDto } from "./dictionary-service-dto";
import { repoCreateLookUp, repoCreateLookUpWithItemAndEntries, repoSelectLastLookUpResult } from "./dictionary-repository";
import { ServiceInputLookUp } from "./dictionary-service-dto";
export const lookUpService = async (dto: LookUpServiceInputDto) => {
export const serviceLookUp = async (dto: ServiceInputLookUp) => {
const {
text,
queryLang,
@@ -11,7 +11,7 @@ export const lookUpService = async (dto: LookUpServiceInputDto) => {
forceRelook
} = dto;
const lastLookUpResult = await selectLastLookUpResult({
const lastLookUpResult = await repoSelectLastLookUpResult({
text,
queryLang,
definitionLang,
@@ -25,7 +25,7 @@ export const lookUpService = async (dto: LookUpServiceInputDto) => {
);
// 使用事务确保数据一致性
createLookUpWithItemAndEntries(
repoCreateLookUpWithItemAndEntries(
{
standardForm: response.standardForm,
queryLang,
@@ -44,7 +44,7 @@ export const lookUpService = async (dto: LookUpServiceInputDto) => {
return response;
} else {
createLookUp({
repoCreateLookUp({
userId: userId,
text: text,
queryLang: queryLang,