refactor(skills): remove subcategory field from skills

Drop subcategory column from database schema and update constraints
Remove subcategory handling from backend queries and API endpoints
Clean up frontend components and admin views to reflect the change
This commit is contained in:
2026-04-30 15:24:19 +08:00
parent 3e8265e0c8
commit 02f6dd47c3
11 changed files with 56 additions and 97 deletions

View File

@@ -23,8 +23,6 @@ Pokemon 可配置:
特长 可配置:
- 名称
- 二级分类(可空,用于给乱撒这类特长做二级分类)
Eg: 名称:乱撒,二级分类:棉花
喜欢的环境 可配置:
- 名称

View File

@@ -40,11 +40,12 @@ CREATE INDEX IF NOT EXISTS user_sessions_user_id_idx
CREATE TABLE IF NOT EXISTS skills (
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name text NOT NULL,
subcategory text,
UNIQUE (name, subcategory)
name text NOT NULL UNIQUE
);
ALTER TABLE skills DROP COLUMN IF EXISTS subcategory;
CREATE UNIQUE INDEX IF NOT EXISTS skills_name_key ON skills(name);
CREATE TABLE IF NOT EXISTS favorite_things (
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name text NOT NULL UNIQUE

View File

@@ -19,9 +19,7 @@ type ConfigType =
type ConfigDefinition = {
table: string;
select: string;
order: string;
hasSubcategory?: boolean;
};
type IdQuantity = {
@@ -73,13 +71,13 @@ const timeOfDays = ['早晨', '中午', '傍晚', '晚上'];
const weathers = ['晴天', '阴天', '雨天'];
const configDefinitions: Record<ConfigType, ConfigDefinition> = {
skills: { table: 'skills', select: 'id, name, subcategory', order: 'name, subcategory', hasSubcategory: true },
environments: { table: 'environments', select: 'id, name', order: 'name' },
'favorite-things': { table: 'favorite_things', select: 'id, name', order: 'name' },
'item-categories': { table: 'item_categories', select: 'id, name', order: 'name' },
'item-usages': { table: 'item_usages', select: 'id, name', order: 'name' },
'acquisition-methods': { table: 'acquisition_methods', select: 'id, name', order: 'name' },
maps: { table: 'maps', select: 'id, name', order: 'name' }
skills: { table: 'skills', order: 'name' },
environments: { table: 'environments', order: 'name' },
'favorite-things': { table: 'favorite_things', order: 'name' },
'item-categories': { table: 'item_categories', order: 'name' },
'item-usages': { table: 'item_usages', order: 'name' },
'acquisition-methods': { table: 'acquisition_methods', order: 'name' },
maps: { table: 'maps', order: 'name' }
};
function asString(value: QueryValue): string | undefined {
@@ -112,10 +110,6 @@ function auditJoins(entityAlias: string, createdAlias = 'created_user', updatedA
`;
}
function configSelect(definition: ConfigDefinition): string {
return definition.hasSubcategory ? 'c.id, c.name, c.subcategory' : 'c.id, c.name';
}
function configOrder(definition: ConfigDefinition): string {
return definition.order
.split(', ')
@@ -215,7 +209,7 @@ const pokemonProjection = `
${auditSelect('p', 'pokemon_created_user', 'pokemon_updated_user')},
json_build_object('id', e.id, 'name', e.name) AS environment,
COALESCE((
SELECT json_agg(json_build_object('id', s.id, 'name', s.name, 'subcategory', s.subcategory) ORDER BY s.name, s.subcategory)
SELECT json_agg(json_build_object('id', s.id, 'name', s.name) ORDER BY s.name)
FROM pokemon_skills ps
JOIN skills s ON s.id = ps.skill_id
WHERE ps.pokemon_id = p.id
@@ -241,9 +235,7 @@ export async function getOptions() {
acquisitionMethods,
maps
] = await Promise.all([
query<{ id: number; name: string; subcategory: string | null }>(
'SELECT id, name, subcategory FROM skills ORDER BY name, subcategory'
),
optionSelect('skills'),
optionSelect('environments'),
optionSelect('favorite_things'),
optionSelect('item_categories'),
@@ -272,7 +264,7 @@ export async function listConfig(type: ConfigType) {
const definition = configDefinitions[type];
return query(
`
SELECT ${configSelect(definition)}, ${auditSelect('c')}
SELECT c.id, c.name, ${auditSelect('c')}
FROM ${definition.table} c
${auditJoins('c')}
ORDER BY ${configOrder(definition)}
@@ -284,7 +276,7 @@ async function getConfigById(type: ConfigType, id: number) {
const definition = configDefinitions[type];
return queryOne(
`
SELECT ${configSelect(definition)}, ${auditSelect('c')}
SELECT c.id, c.name, ${auditSelect('c')}
FROM ${definition.table} c
${auditJoins('c')}
WHERE c.id = $1
@@ -296,26 +288,16 @@ async function getConfigById(type: ConfigType, id: number) {
export async function createConfig(type: ConfigType, payload: Record<string, unknown>, userId: number) {
const definition = configDefinitions[type];
const name = cleanName(payload.name);
const subcategory = typeof payload.subcategory === 'string' && payload.subcategory.trim() ? payload.subcategory.trim() : null;
const id = await withTransaction(async (client) => {
const result = definition.hasSubcategory
? await client.query<{ id: number }>(
`
INSERT INTO ${definition.table} (name, subcategory, created_by_user_id, updated_by_user_id)
VALUES ($1, $2, $3, $3)
RETURNING id
`,
[name, subcategory, userId]
)
: await client.query<{ id: number }>(
`
INSERT INTO ${definition.table} (name, created_by_user_id, updated_by_user_id)
VALUES ($1, $2, $2)
RETURNING id
`,
[name, userId]
);
const result = await client.query<{ id: number }>(
`
INSERT INTO ${definition.table} (name, created_by_user_id, updated_by_user_id)
VALUES ($1, $2, $2)
RETURNING id
`,
[name, userId]
);
const createdId = result.rows[0].id;
await recordEditLog(client, type, createdId, 'create', userId);
@@ -328,26 +310,16 @@ export async function createConfig(type: ConfigType, payload: Record<string, unk
export async function updateConfig(type: ConfigType, id: number, payload: Record<string, unknown>, userId: number) {
const definition = configDefinitions[type];
const name = cleanName(payload.name);
const subcategory = typeof payload.subcategory === 'string' && payload.subcategory.trim() ? payload.subcategory.trim() : null;
const updated = await withTransaction(async (client) => {
const result = definition.hasSubcategory
? await client.query(
`
UPDATE ${definition.table}
SET name = $1, subcategory = $2, updated_by_user_id = $3, updated_at = now()
WHERE id = $4
`,
[name, subcategory, userId, id]
)
: await client.query(
`
UPDATE ${definition.table}
SET name = $1, updated_by_user_id = $2, updated_at = now()
WHERE id = $3
`,
[name, userId, id]
);
const result = await client.query(
`
UPDATE ${definition.table}
SET name = $1, updated_by_user_id = $2, updated_at = now()
WHERE id = $3
`,
[name, userId, id]
);
if (result.rowCount === 0) {
return false;

View File

@@ -2,15 +2,14 @@
import type { NamedEntity } from '../services/api';
defineProps<{
items: Array<NamedEntity & { subcategory?: string | null; quantity?: number }>;
items: Array<NamedEntity & { quantity?: number }>;
}>();
</script>
<template>
<div class="chips">
<span v-for="item in items" :key="`${item.id}-${item.name}`" class="chip">
{{ item.name }}<span v-if="item.subcategory"> · {{ item.subcategory }}</span
><span v-if="item.quantity"> × {{ item.quantity }}</span>
{{ item.name }}<span v-if="item.quantity"> × {{ item.quantity }}</span>
</span>
</div>
</template>

View File

@@ -5,7 +5,6 @@ export type TagsSelectOption = {
id: number | string;
name: string;
label?: string;
subcategory?: string | null;
};
type OptionRow = {
@@ -56,7 +55,7 @@ const activeIndex = ref(-1);
const optionRows = computed(() =>
props.options.map((option, index) => ({
value: String(option.id),
label: option.label ?? (option.subcategory ? `${option.name} · ${option.subcategory}` : option.name),
label: option.label ?? option.name,
id: `${props.id}-option-${index}`
}))
);

View File

@@ -7,10 +7,6 @@ export interface NamedEntity {
name: string;
}
export interface Skill extends NamedEntity {
subcategory: string | null;
}
export interface UserSummary {
id: number;
displayName: string;
@@ -27,7 +23,7 @@ export interface Pokemon extends EditInfo {
id: number;
name: string;
environment: NamedEntity;
skills: Skill[];
skills: NamedEntity[];
favorite_things: NamedEntity[];
}
@@ -89,7 +85,7 @@ export interface RecipeDetail extends Recipe {
}
export interface Options {
skills: Skill[];
skills: NamedEntity[];
environments: NamedEntity[];
favoriteThings: NamedEntity[];
itemCategories: NamedEntity[];
@@ -283,11 +279,11 @@ export const api = {
me: () => getJson<{ user: AuthUser }>('/api/auth/me'),
logout: () => postEmpty('/api/auth/logout'),
options: () => getJson<Options>('/api/options'),
config: (type: ConfigType) => getJson<Array<Skill | NamedEntity>>(`/api/admin/config/${type}`),
createConfig: (type: ConfigType, payload: { name: string; subcategory?: string | null }) =>
sendJson<Skill | NamedEntity>(`/api/admin/config/${type}`, 'POST', payload),
updateConfig: (type: ConfigType, id: number, payload: { name: string; subcategory?: string | null }) =>
sendJson<Skill | NamedEntity>(`/api/admin/config/${type}/${id}`, 'PUT', payload),
config: (type: ConfigType) => getJson<NamedEntity[]>(`/api/admin/config/${type}`),
createConfig: (type: ConfigType, payload: { name: string }) =>
sendJson<NamedEntity>(`/api/admin/config/${type}`, 'POST', payload),
updateConfig: (type: ConfigType, id: number, payload: { name: string }) =>
sendJson<NamedEntity>(`/api/admin/config/${type}/${id}`, 'PUT', payload),
deleteConfig: (type: ConfigType, id: number) => deleteJson(`/api/admin/config/${type}/${id}`),
pokemon: (params: Record<string, string | number | undefined>) =>
getJson<Pokemon[]>(`/api/pokemon${buildQuery(params)}`),

View File

@@ -12,12 +12,11 @@ import {
type Item,
type NamedEntity,
type Pokemon,
type Recipe,
type Skill
type Recipe
} from '../services/api';
type AdminTab = 'config' | 'pokemon' | 'items' | 'recipes' | 'habitats';
type EditableConfig = (NamedEntity | Skill) & { subcategory?: string | null };
type EditableConfig = NamedEntity;
const tabs: Array<{ key: AdminTab; label: string }> = [
{ key: 'config', label: '系统配置' },
@@ -27,8 +26,8 @@ const tabs: Array<{ key: AdminTab; label: string }> = [
{ key: 'habitats', label: '栖息地' }
];
const configTypes: Array<{ key: ConfigType; label: string; hasSubcategory?: boolean }> = [
{ key: 'skills', label: '特长', hasSubcategory: true },
const configTypes: Array<{ key: ConfigType; label: string }> = [
{ key: 'skills', label: '特长' },
{ key: 'environments', label: '喜欢的环境' },
{ key: 'favorite-things', label: '喜欢的东西 / 标签' },
{ key: 'item-categories', label: '物品分类' },
@@ -48,7 +47,7 @@ const currentUser = ref<AuthUser | null>(null);
const busy = ref(false);
const contentLoading = ref(false);
const message = ref('');
const configForm = ref({ id: 0, name: '', subcategory: '' });
const configForm = ref({ id: 0, name: '' });
const selectedConfig = computed(() => configTypes.find((item) => item.key === activeConfigType.value) ?? configTypes[0]);
const configTabs = computed<TabOption[]>(() => configTypes.map((item) => ({ value: item.key, label: item.label })));
@@ -87,18 +86,17 @@ async function loadConfig() {
}
function resetConfigForm() {
configForm.value = { id: 0, name: '', subcategory: '' };
configForm.value = { id: 0, name: '' };
}
function editConfig(item: EditableConfig) {
configForm.value = { id: item.id, name: item.name, subcategory: item.subcategory ?? '' };
configForm.value = { id: item.id, name: item.name };
}
async function saveConfig() {
await run(async () => {
const payload = {
name: configForm.value.name,
subcategory: selectedConfig.value.hasSubcategory ? configForm.value.subcategory || null : null
name: configForm.value.name
};
if (configForm.value.id) {
@@ -247,10 +245,6 @@ onMounted(() => {
<label for="config-name">名称</label>
<input id="config-name" v-model="configForm.name" required />
</div>
<div v-if="selectedConfig.hasSubcategory" class="field">
<label for="config-subcategory">二级分类</label>
<input id="config-subcategory" v-model="configForm.subcategory" />
</div>
<div class="form-actions">
<button type="submit" class="link-button" :disabled="busy">{{ busy ? '保存中' : '保存' }}</button>
<button type="button" class="plain-button" :disabled="busy" @click="resetConfigForm">新建</button>
@@ -260,7 +254,7 @@ onMounted(() => {
<h3 class="section-subtitle">{{ selectedConfig.label }}</h3>
<ul v-if="configRows.length" class="row-list">
<li v-for="item in configRows" :key="item.id">
<span>{{ item.name }}<span v-if="item.subcategory"> · {{ item.subcategory }}</span></span>
<span>{{ item.name }}</span>
<span class="row-actions">
<button type="button" @click="editConfig(item)">编辑</button>
<button type="button" @click="removeConfig(item.id)">删除</button>

View File

@@ -138,7 +138,7 @@ async function createMultiOption(selectKey: string, type: ConfigType, name: stri
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
const value = String(created.id);
if (!values.includes(value)) {

View File

@@ -75,7 +75,7 @@ async function createSingleOption(selectKey: string, type: ConfigType, name: str
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
assign(String(created.id));
} catch (error) {
@@ -92,7 +92,7 @@ async function createMultiOption(selectKey: string, type: ConfigType, name: stri
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
const value = String(created.id);
if (!values.includes(value)) {

View File

@@ -69,7 +69,7 @@ async function createSingleOption(selectKey: string, type: ConfigType, name: str
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
assign(String(created.id));
} catch (error) {
@@ -86,7 +86,7 @@ async function createMultiOption(selectKey: string, type: ConfigType, name: stri
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
const value = String(created.id);
if (!values.includes(value)) {

View File

@@ -81,7 +81,7 @@ async function createMultiOption(selectKey: string, type: ConfigType, name: stri
creatingSelect.value = selectKey;
message.value = '';
try {
const created = await api.createConfig(type, { name: cleanName, subcategory: null });
const created = await api.createConfig(type, { name: cleanName });
await loadOptions();
const value = String(created.id);
if (!values.includes(value)) {