From d2dedd85f95c998f8ce579c6c61f7cea7b7ab52e Mon Sep 17 00:00:00 2001 From: opbnq-q Date: Sat, 15 Mar 2025 19:21:27 +0700 Subject: [PATCH] feat: search, sort --- generation/templates/scheme.tmpl | 18 ++++++++++++++---- generation/templates/service.tmpl | 12 +++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/generation/templates/scheme.tmpl b/generation/templates/scheme.tmpl index df116ff..0477557 100644 --- a/generation/templates/scheme.tmpl +++ b/generation/templates/scheme.tmpl @@ -6,6 +6,8 @@ import Service from './{{.LowerName}}.service.ts' import type { Scheme } from '../types/scheme.type' import { {{.StructName}} } from '{{.GolangServicesPath}}' import { ref } from 'vue' +import type { Validate } from "../types/validate.type"; +import { getDefaultSortOptions } from "../utils/structs/default-sort-options.util.ts"; import type { Validate } from '../types/validate.type' {{range .Dependencies}} import {{.ImportName}} from '../{{.LowerName}}/{{.LowerName}}.service.ts' @@ -20,11 +22,11 @@ const load = async () => { {{range .Dependencies}} (scheme as any).{{.FieldName}}.type!.nested!.values = await {{.ServiceName}}.readAll(); {{end}} - items.value = await service.readAll(); + items.value = await service.sort(sortOptions.value) ; return items.value; }; -onMounted(async () => { +onMounted(() => { load() }) @@ -37,14 +39,22 @@ const scheme: Scheme<{{.StructName}}> = reactive({ const getDefaults = () => getDefaultValues(scheme) -const validate: Validate<{{.StructName}}> = (entity) => { +const validate: Validate<{{.StructName}}> = entity => { return { status: 'success' } }; +const search = async (input: string) => { + items.value = await service.search(input) +} + +const sortOptions = ref(getDefaultSortOptions(scheme)) + diff --git a/generation/templates/service.tmpl b/generation/templates/service.tmpl index d26ae08..e4eb4f7 100644 --- a/generation/templates/service.tmpl +++ b/generation/templates/service.tmpl @@ -1,6 +1,8 @@ -import { GetAll, Create, Delete, GetById, Update, Count } from "../../bindings/app/internal/services/{{.LowerModelName}}service.ts" +import { GetAll, Create, Delete, GetById, Update, Count, SortedByOrder, SearchByAllTextFields } from "../../bindings/app/internal/services/{{.LowerModelName}}service.ts" import type { {{.ModelName}} } from "{{.ServicesPath}}" import type { IService } from "../types/service.type.ts" +import type { SortOptions } from "../types/sort-options.type.ts"; + export default class {{.ModelName}}Service implements IService<{{.ModelName}}> { async read(id: number) { @@ -26,4 +28,12 @@ export default class {{.ModelName}}Service implements IService<{{.ModelName}}> { async count() { return await Count() } + + async search(input: string) { + return await SearchByAllTextFields(input) as {{ .ModelName }}[] + } + + async sort(options: SortOptions<{{ .ModelName }}>) { + return await SortedByOrder(Object.entries(options).map(item => ({Name: item[0], Order: item[1]}))) as {{ .ModelName }}[] + } } \ No newline at end of file