Skip to content

Commit

Permalink
fix: fix base & table name should not contains space
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 17, 2024
1 parent c41760a commit 43c1c79
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 16 deletions.
20 changes: 15 additions & 5 deletions apps/frontend/src/lib/components/blocks/base/base-setting.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { toggleModal, UPDATE_BASE_MODAL } from "$lib/store/modal.store"
import { toggleModal } from "$lib/store/modal.store"
import { trpc } from "$lib/trpc/client"
import { createMutation } from "@tanstack/svelte-query"
import type { IBaseDTO } from "@undb/base"
Expand All @@ -8,21 +8,20 @@
import { zodClient } from "sveltekit-superforms/adapters"
import * as Form from "$lib/components/ui/form"
import { Switch } from "$lib/components/ui/switch"
import Input from "$lib/components/ui/input/input.svelte"
export let base: Omit<IBaseDTO, "spaceId">
const updateBaseMutation = createMutation({
mutationKey: ["base", base.id, "updateBase"],
mutationFn: trpc.base.update.mutate,
onSuccess(data, variables, context) {
toggleModal(UPDATE_BASE_MODAL)
},
})
const form = superForm(
defaults(
{
id: base.id,
name: base.name,
allowTemplate: base.option?.allowTemplate,
},
zodClient(updateBaseCommand),
Expand All @@ -45,8 +44,19 @@

<section>
<form class="max-w-4xl space-y-4" method="POST" use:enhance>
<legend class="mb-4 text-lg font-medium"> Base Setting </legend>
<Form.Field {form} name="name" class="rounded-lg border p-4">
<Form.Control let:attrs>
<div class="space-y-0.5">
<Form.Label>Name</Form.Label>
<Form.Description>Base name.</Form.Description>
</div>
<Input {...attrs} bind:value={$formData.name} />
</Form.Control>
<Form.Description />
<Form.FieldErrors />
</Form.Field>
<fieldset>
<legend class="mb-4 text-lg font-medium"> Base Setting </legend>
<div class="space-y-4">
<Form.Field
{form}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</script>

<div>
<form method="POST" use:enhance>
<form class="space-y-4" method="POST" use:enhance>
<Form.Field {form} name="name">
<Form.Control let:attrs>
<Form.Label>Name</Form.Label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</script>

<div>
<form method="POST" use:enhance>
<form class="space-y-4" method="POST" use:enhance>
<Form.Field {form} name="name">
<Form.Control let:attrs>
<Form.Label>Name</Form.Label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import BaseHeader from "$lib/components/blocks/base/base-header.svelte"
import UpdateBaseDialog from "$lib/components/blocks/base/update-base-dialog.svelte"
import type { LayoutData } from "./$types"
export let data: LayoutData
Expand All @@ -11,6 +12,7 @@
<main class="flex flex-col">
{#if base}
<BaseHeader {base} />
<UpdateBaseDialog {base} />
<slot />
{/if}
</main>
2 changes: 0 additions & 2 deletions apps/frontend/src/routes/(authed)/bases/[baseId]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import BaseDetail from "$lib/components/blocks/base/base-detail.svelte"
import UpdateBaseDialog from "$lib/components/blocks/base/update-base-dialog.svelte"
import type { PageData } from "./$types"
export let data: PageData
Expand All @@ -11,5 +10,4 @@

{#if base}
<BaseDetail {base} />
<UpdateBaseDialog {base} />
{/if}
7 changes: 5 additions & 2 deletions packages/base/src/value-objects/base-name.vo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ValueObject } from "@undb/domain"
import * as z from "@undb/zod"

export const baseNameSchema = z.string().min(1)
export const baseNameSchema = z
.string()
.min(1)
.regex(/^[^\s]*$/, "Base name cannot contain spaces")

export class BaseName extends ValueObject<z.infer<typeof baseNameSchema>> {
static from(name: string): BaseName {
return new BaseName({ value: name })
return new BaseName({ value: baseNameSchema.parse(name) })
}
}
5 changes: 3 additions & 2 deletions packages/table/src/services/methods/update-table.method.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { applyRules, Some } from "@undb/domain"
import type { IUpdateTableDTO } from "../../dto"
import { TableNameShouldBeUnique } from "../../rules/table-name-should-be-unique.rule"
import { TableBaseIdSpecification, TableIdSpecification } from "../../specifications"
import { TableBaseIdSpecification } from "../../specifications"
import { TableIdVo } from "../../table-id.vo"
import type { TableDo } from "../../table.do"
import type { TableService } from "../table.service"

export async function updateTableMethod(this: TableService, dto: IUpdateTableDTO): Promise<TableDo> {
const table = (await this.repository.findOneById(new TableIdVo(dto.id))).unwrap()

const qs = new TableBaseIdSpecification(table.baseId).and(new TableIdSpecification(table.id).not())
const qs = new TableBaseIdSpecification(table.baseId)
console.log(JSON.stringify(qs))
const baseTables = await this.repository.find(Some(qs))

const names = baseTables.map((table) => table.name.value).concat(dto.name)
Expand Down
7 changes: 5 additions & 2 deletions packages/table/src/table-name.vo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ValueObject } from "@undb/domain"
import { z } from "@undb/zod"

export const tableName = z.string().min(2, { message: "table name contains at least 2 chars" })
export const tableName = z
.string()
.min(2, { message: "table name contains at least 2 chars" })
.regex(/^[^\s]*$/, "Table name cannot contain spaces")

export type ITableName = z.infer<typeof tableName>

export class TableNameVo extends ValueObject {
constructor(value: string) {
super({ value })
super({ value: tableName.parse(value) })
}
}
2 changes: 1 addition & 1 deletion packages/utils/src/get-next-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getNextName = (names: string[] = [], defaultName = "field", level =
if (defaultName.endsWith(`(${level - 1})`)) {
name = defaultName.replace(`(${level - 1})`, `(${level})`)
} else {
name = `${defaultName} (${level})`
name = `${defaultName}(${level})`
}
} else {
name = defaultName
Expand Down

0 comments on commit 43c1c79

Please sign in to comment.