a5f884f440
Two tables were named after the concept they came from rather than the module
they belong to, so the schema read as if the template tables were part of the
paper module. Renamed (data preserved, `RENAME TABLE` moves rows in place):
paper_template -> template the 模板 module
section_field -> template_field_library the 字段库 the 模板 module owns
The paper tables and `template_field` already followed the rule. The rename
carries through everything that named a module:
models Template, TemplateField, TemplateFieldLibrary
schemas Template*, TemplateFieldLibrary*
crud app/crud/template.py, app/crud/template_field_library.py
API /template-field-library (was /section-fields); handlers are now
named after library entries, which removes the ambiguity with
TemplateField — a placement, a different thing entirely
client src/api/templateFieldLibrary.ts
`paper_template_filed_sort` is deliberately untouched: it is a column of the
paper module, spelled as the feature was specified.
TiDB v8.5 with tidb_enable_foreign_key on — as this cluster runs — enforces
foreign keys rather than ignoring them, so the docs' "TiDB does not enforce
foreign keys" was wrong. Corrected, with what actually follows from it: the
rename was rehearsed (RENAME TABLE carries a referencing constraint along), the
API keeps checking first so a violation names the row instead of surfacing a
driver error, and the ORM cascades stay so behaviour does not depend on a
cluster setting.
Revision f27a1c6d9e04 verified both ways; 40 smoke checks, type-check and build
all pass.
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import http from './client'
|
|
import type { BatchDeleteResult, PageQuery, PageResult } from './types'
|
|
|
|
/** Mirrors `app.schemas.template_field_library.TemplateFieldLibraryRead`. */
|
|
export interface TemplateFieldLibrary {
|
|
id: number
|
|
/** Display name, numbering included — e.g. `1. Introduction`. */
|
|
name: string
|
|
/** Heading depth: 1 for `1.`, 2 for `1.1`. A rendering hint, not a parent. */
|
|
level: number
|
|
/** Points. Fractional, because 五号 = 10.5pt. */
|
|
font_size: number
|
|
/** Canonical RGB as `#RRGGBB`. */
|
|
font_color: string
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
/** Body for creating or updating a library field. */
|
|
export interface TemplateFieldLibraryPayload {
|
|
name: string
|
|
level: number
|
|
font_size: number
|
|
font_color: string
|
|
}
|
|
|
|
export interface TemplateFieldLibraryQuery extends PageQuery {
|
|
level?: number | null
|
|
}
|
|
|
|
/** The API caps `page_size` at 500; 200 keeps responses modest while looping. */
|
|
const LIBRARY_PAGE_SIZE = 200
|
|
|
|
export async function listTemplateFieldLibrary(
|
|
query: TemplateFieldLibraryQuery = {},
|
|
): Promise<PageResult<TemplateFieldLibrary>> {
|
|
// Drop empty filters so the URL stays clean and the backend sees "no filter"
|
|
// rather than `keyword=`.
|
|
const params: Record<string, unknown> = {}
|
|
if (query.keyword) params.keyword = query.keyword
|
|
if (query.level != null) params.level = query.level
|
|
if (query.page) params.page = query.page
|
|
if (query.page_size) params.page_size = query.page_size
|
|
|
|
const { data } = await http.get<PageResult<TemplateFieldLibrary>>(
|
|
'/template-field-library',
|
|
{ params },
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Fetch the entire library, following pagination.
|
|
*
|
|
* The template form needs every field, not one page, so that a field placed
|
|
* deep in the library can still be shown by name in an existing selection.
|
|
*/
|
|
export async function fetchAllTemplateFieldLibrary(keyword?: string): Promise<TemplateFieldLibrary[]> {
|
|
const all: TemplateFieldLibrary[] = []
|
|
let page = 1
|
|
|
|
for (;;) {
|
|
const result = await listTemplateFieldLibrary({
|
|
keyword,
|
|
page,
|
|
page_size: LIBRARY_PAGE_SIZE,
|
|
})
|
|
all.push(...result.items)
|
|
if (page >= result.pages || result.items.length === 0) {
|
|
return all
|
|
}
|
|
page += 1
|
|
}
|
|
}
|
|
|
|
export async function createTemplateFieldLibrary(
|
|
payload: TemplateFieldLibraryPayload,
|
|
): Promise<TemplateFieldLibrary> {
|
|
const { data } = await http.post<TemplateFieldLibrary>('/template-field-library', payload)
|
|
return data
|
|
}
|
|
|
|
export async function updateTemplateFieldLibrary(
|
|
id: number,
|
|
payload: Partial<TemplateFieldLibraryPayload>,
|
|
): Promise<TemplateFieldLibrary> {
|
|
const { data } = await http.patch<TemplateFieldLibrary>(`/template-field-library/${id}`, payload)
|
|
return data
|
|
}
|
|
|
|
export async function deleteTemplateFieldLibrary(id: number): Promise<void> {
|
|
await http.delete(`/template-field-library/${id}`)
|
|
}
|
|
|
|
export async function batchDeleteTemplateFieldLibrary(
|
|
ids: number[],
|
|
): Promise<BatchDeleteResult> {
|
|
const { data } = await http.post<BatchDeleteResult>('/template-field-library/batch-delete', {
|
|
ids,
|
|
})
|
|
return data
|
|
}
|