merge sandbox/edd39ac: chg_0007 lite_demo_restore — 补回 lite-react book/shop demo (TypeORM-free, 33+17+16=66 e2e PASS)
Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
1c2f3ba234
commit
df62c960f0
17 files changed
+1617
-2
No files matched your search
@@ -4,6 +4,9 @@ import Login from './views/Login';
|
||||
import Dashboard from './views/Dashboard';
|
||||
import OperatorList from './views/OperatorList';
|
||||
import Run from './views/Run';
|
||||
// chg_0007 lite-demo: book + product 通用 CRUD 参考实现
|
||||
import Book from './views/Book';
|
||||
import Product from './views/Product';
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
@@ -13,6 +16,8 @@ export default function App() {
|
||||
<Route path="/dashboard" element={<Dashboard />} />
|
||||
<Route path="/operators" element={<OperatorList />} />
|
||||
<Route path="/operators/:code/run" element={<Run />} />
|
||||
<Route path="/books/Book" element={<Book />} />
|
||||
<Route path="/books/Product" element={<Product />} />
|
||||
</Route>
|
||||
<Route path="/" element={<Navigate to="/login" replace />} />
|
||||
<Route path="*" element={<Navigate to="/login" replace />} />
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { client } from './client';
|
||||
|
||||
/**
|
||||
* chg_0007 lite-demo books API.
|
||||
* 端点形态:POST /api/ssw/dth/books/Book/{List|Create|Update|Remove|getInfo}
|
||||
* 响应壳:{code, msg, data} — 见 src/backend/src/common/action-result.filter.ts
|
||||
*/
|
||||
|
||||
export interface BookRow {
|
||||
f_id: string;
|
||||
f_title: string;
|
||||
f_author: string | null;
|
||||
f_isbn: string | null;
|
||||
f_category: string | null;
|
||||
f_price: string | null;
|
||||
f_stock: number;
|
||||
f_creator_user_id: string | null;
|
||||
f_creator_time: string;
|
||||
f_modify_time: string;
|
||||
f_delete_mark: number;
|
||||
}
|
||||
|
||||
interface Envelope<T> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
async function unwrap<T>(p: Promise<AxiosResponse<Envelope<T>>>): Promise<T> {
|
||||
const r = await p;
|
||||
return r.data.data;
|
||||
}
|
||||
|
||||
export function listBooks(): Promise<BookRow[]> {
|
||||
return unwrap(client.post<Envelope<BookRow[]>>('/ssw/dth/books/Book/List'));
|
||||
}
|
||||
|
||||
export function createBook(input: {
|
||||
f_title: string;
|
||||
f_author?: string;
|
||||
f_isbn?: string;
|
||||
f_category?: string;
|
||||
f_price?: string | number;
|
||||
f_stock?: number;
|
||||
}): Promise<BookRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<BookRow>>('/ssw/dth/books/Book/Create', input),
|
||||
);
|
||||
}
|
||||
|
||||
export function updateBook(input: {
|
||||
id: string;
|
||||
f_title?: string;
|
||||
f_author?: string;
|
||||
f_isbn?: string;
|
||||
f_category?: string;
|
||||
f_price?: string | number;
|
||||
f_stock?: number;
|
||||
}): Promise<BookRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<BookRow>>('/ssw/dth/books/Book/Update', input),
|
||||
);
|
||||
}
|
||||
|
||||
export function removeBook(id: string): Promise<{ id: string }> {
|
||||
return unwrap(
|
||||
client.post<Envelope<{ id: string }>>('/ssw/dth/books/Book/Remove', { id }),
|
||||
);
|
||||
}
|
||||
|
||||
export function getBookInfo(id: string): Promise<BookRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<BookRow>>('/ssw/dth/books/Book/getInfo', { id }),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { client } from './client';
|
||||
|
||||
/**
|
||||
* chg_0007 lite-demo shop (products) API.
|
||||
* 端点形态:POST /api/ssw/dth/books/Product/{List|Create|Update|Remove|getInfo}
|
||||
* 响应壳:{code, msg, data} — 见 src/backend/src/common/action-result.filter.ts
|
||||
*/
|
||||
|
||||
export interface ProductRow {
|
||||
f_id: string;
|
||||
f_name: string;
|
||||
f_sku: string | null;
|
||||
f_category: string | null;
|
||||
f_price: string | null;
|
||||
f_stock: number;
|
||||
f_creator_user_id: string | null;
|
||||
f_creator_time: string;
|
||||
f_modify_time: string;
|
||||
f_delete_mark: number;
|
||||
}
|
||||
|
||||
interface Envelope<T> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
async function unwrap<T>(p: Promise<AxiosResponse<Envelope<T>>>): Promise<T> {
|
||||
const r = await p;
|
||||
return r.data.data;
|
||||
}
|
||||
|
||||
export function listProducts(): Promise<ProductRow[]> {
|
||||
return unwrap(client.post<Envelope<ProductRow[]>>('/ssw/dth/books/Product/List'));
|
||||
}
|
||||
|
||||
export function createProduct(input: {
|
||||
f_name: string;
|
||||
f_sku?: string;
|
||||
f_category?: string;
|
||||
f_price?: string | number;
|
||||
f_stock?: number;
|
||||
}): Promise<ProductRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<ProductRow>>('/ssw/dth/books/Product/Create', input),
|
||||
);
|
||||
}
|
||||
|
||||
export function updateProduct(input: {
|
||||
id: string;
|
||||
f_name?: string;
|
||||
f_sku?: string;
|
||||
f_category?: string;
|
||||
f_price?: string | number;
|
||||
f_stock?: number;
|
||||
}): Promise<ProductRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<ProductRow>>('/ssw/dth/books/Product/Update', input),
|
||||
);
|
||||
}
|
||||
|
||||
export function removeProduct(id: string): Promise<{ id: string }> {
|
||||
return unwrap(
|
||||
client.post<Envelope<{ id: string }>>('/ssw/dth/books/Product/Remove', { id }),
|
||||
);
|
||||
}
|
||||
|
||||
export function getProductInfo(id: string): Promise<ProductRow> {
|
||||
return unwrap(
|
||||
client.post<Envelope<ProductRow>>('/ssw/dth/books/Product/getInfo', { id }),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
import { FormEvent, useEffect, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { BookRow, createBook, listBooks, removeBook, updateBook } from '../api/book';
|
||||
import { useAuth } from '../components/AuthContext';
|
||||
import type { ApiError } from '../api/client';
|
||||
|
||||
interface FormState {
|
||||
f_title: string;
|
||||
f_author: string;
|
||||
f_isbn: string;
|
||||
f_category: string;
|
||||
f_price: string;
|
||||
f_stock: string;
|
||||
}
|
||||
|
||||
const EMPTY_FORM: FormState = {
|
||||
f_title: '',
|
||||
f_author: '',
|
||||
f_isbn: '',
|
||||
f_category: '',
|
||||
f_price: '',
|
||||
f_stock: '0',
|
||||
};
|
||||
|
||||
export default function Book() {
|
||||
const { user, clearAuth } = useAuth();
|
||||
const [books, setBooks] = useState<BookRow[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [editing, setEditing] = useState<BookRow | null>(null);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [form, setForm] = useState<FormState>(EMPTY_FORM);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
async function refresh() {
|
||||
setError(null);
|
||||
try {
|
||||
const rows = await listBooks();
|
||||
setBooks(rows);
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
}, []);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setForm(EMPTY_FORM);
|
||||
setShowForm(true);
|
||||
}
|
||||
|
||||
function openEdit(row: BookRow) {
|
||||
setEditing(row);
|
||||
setForm({
|
||||
f_title: row.f_title,
|
||||
f_author: row.f_author ?? '',
|
||||
f_isbn: row.f_isbn ?? '',
|
||||
f_category: row.f_category ?? '',
|
||||
f_price: row.f_price ?? '',
|
||||
f_stock: String(row.f_stock ?? 0),
|
||||
});
|
||||
setShowForm(true);
|
||||
}
|
||||
|
||||
async function onSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
try {
|
||||
const payload = {
|
||||
f_title: form.f_title.trim(),
|
||||
f_author: form.f_author.trim() || undefined,
|
||||
f_isbn: form.f_isbn.trim() || undefined,
|
||||
f_category: form.f_category.trim() || undefined,
|
||||
f_price: form.f_price.trim() || undefined,
|
||||
f_stock: form.f_stock ? Number(form.f_stock) : 0,
|
||||
};
|
||||
if (editing) {
|
||||
await updateBook({ id: editing.f_id, ...payload });
|
||||
} else {
|
||||
await createBook(payload);
|
||||
}
|
||||
setShowForm(false);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '保存失败');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function onRemove(id: string) {
|
||||
if (!confirm('确认软删除这本书?')) return;
|
||||
setError(null);
|
||||
try {
|
||||
await removeBook(id);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="book-list">
|
||||
<header className="book-list__header">
|
||||
<Link className="book-list__back" to="/dashboard">
|
||||
← 返回 Dashboard
|
||||
</Link>
|
||||
<h1>图书管理</h1>
|
||||
<div className="book-list__user">
|
||||
<span>
|
||||
{user?.username ?? '匿名'}
|
||||
{user?.isAdmin ? ' · 管理员' : ''}
|
||||
</span>
|
||||
<button
|
||||
className="book-list__logout"
|
||||
onClick={() => {
|
||||
clearAuth();
|
||||
window.location.assign('/login');
|
||||
}}
|
||||
>
|
||||
退出
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section className="book-list__actions">
|
||||
<button
|
||||
className="book-list__create"
|
||||
onClick={openCreate}
|
||||
data-testid="book-create-btn"
|
||||
>
|
||||
+ 新建图书
|
||||
</button>
|
||||
</section>
|
||||
|
||||
{error && (
|
||||
<div className="book-list__error" role="alert">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!books && !error && <div className="book-list__loading">加载中…</div>}
|
||||
|
||||
{books && books.length === 0 && (
|
||||
<div className="book-list__empty">暂无图书</div>
|
||||
)}
|
||||
|
||||
{books && books.length > 0 && (
|
||||
<table className="book-list__table" data-testid="book-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>书名</th>
|
||||
<th>作者</th>
|
||||
<th>ISBN</th>
|
||||
<th>分类</th>
|
||||
<th>价格</th>
|
||||
<th>库存</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{books.map((b) => (
|
||||
<tr key={b.f_id} data-testid={`book-row-${b.f_id}`}>
|
||||
<td>{b.f_title}</td>
|
||||
<td>{b.f_author ?? '-'}</td>
|
||||
<td>{b.f_isbn ?? '-'}</td>
|
||||
<td>{b.f_category ?? '-'}</td>
|
||||
<td>{b.f_price ?? '-'}</td>
|
||||
<td>{b.f_stock}</td>
|
||||
<td>
|
||||
<button onClick={() => openEdit(b)}>编辑</button>{' '}
|
||||
<button onClick={() => onRemove(b.f_id)}>删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
|
||||
{showForm && (
|
||||
<div className="book-list__modal">
|
||||
<form className="book-list__modal-card" onSubmit={onSubmit}>
|
||||
<h2>{editing ? '编辑图书' : '新建图书'}</h2>
|
||||
<label>
|
||||
书名 <em>*</em>
|
||||
<input
|
||||
required
|
||||
value={form.f_title}
|
||||
onChange={(e) => setForm({ ...form, f_title: e.target.value })}
|
||||
data-testid="book-form-title"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
作者
|
||||
<input
|
||||
value={form.f_author}
|
||||
onChange={(e) => setForm({ ...form, f_author: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
ISBN
|
||||
<input
|
||||
value={form.f_isbn}
|
||||
onChange={(e) => setForm({ ...form, f_isbn: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
分类
|
||||
<input
|
||||
value={form.f_category}
|
||||
onChange={(e) => setForm({ ...form, f_category: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
价格
|
||||
<input
|
||||
value={form.f_price}
|
||||
onChange={(e) => setForm({ ...form, f_price: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
库存
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={form.f_stock}
|
||||
onChange={(e) => setForm({ ...form, f_stock: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<div className="book-list__modal-actions">
|
||||
<button type="submit" disabled={submitting} data-testid="book-form-submit">
|
||||
{submitting ? '保存中…' : '保存'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowForm(false)}
|
||||
disabled={submitting}
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
import { FormEvent, useEffect, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import {
|
||||
ProductRow,
|
||||
createProduct,
|
||||
listProducts,
|
||||
removeProduct,
|
||||
updateProduct,
|
||||
} from '../api/product';
|
||||
import { useAuth } from '../components/AuthContext';
|
||||
import type { ApiError } from '../api/client';
|
||||
|
||||
interface FormState {
|
||||
f_name: string;
|
||||
f_sku: string;
|
||||
f_category: string;
|
||||
f_price: string;
|
||||
f_stock: string;
|
||||
}
|
||||
|
||||
const EMPTY_FORM: FormState = {
|
||||
f_name: '',
|
||||
f_sku: '',
|
||||
f_category: '',
|
||||
f_price: '',
|
||||
f_stock: '0',
|
||||
};
|
||||
|
||||
export default function Product() {
|
||||
const { user, clearAuth } = useAuth();
|
||||
const [products, setProducts] = useState<ProductRow[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [editing, setEditing] = useState<ProductRow | null>(null);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [form, setForm] = useState<FormState>(EMPTY_FORM);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
async function refresh() {
|
||||
setError(null);
|
||||
try {
|
||||
const rows = await listProducts();
|
||||
setProducts(rows);
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '加载失败');
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
}, []);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setForm(EMPTY_FORM);
|
||||
setShowForm(true);
|
||||
}
|
||||
|
||||
function openEdit(row: ProductRow) {
|
||||
setEditing(row);
|
||||
setForm({
|
||||
f_name: row.f_name,
|
||||
f_sku: row.f_sku ?? '',
|
||||
f_category: row.f_category ?? '',
|
||||
f_price: row.f_price ?? '',
|
||||
f_stock: String(row.f_stock ?? 0),
|
||||
});
|
||||
setShowForm(true);
|
||||
}
|
||||
|
||||
async function onSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
try {
|
||||
const payload = {
|
||||
f_name: form.f_name.trim(),
|
||||
f_sku: form.f_sku.trim() || undefined,
|
||||
f_category: form.f_category.trim() || undefined,
|
||||
f_price: form.f_price.trim() || undefined,
|
||||
f_stock: form.f_stock ? Number(form.f_stock) : 0,
|
||||
};
|
||||
if (editing) {
|
||||
await updateProduct({ id: editing.f_id, ...payload });
|
||||
} else {
|
||||
await createProduct(payload);
|
||||
}
|
||||
setShowForm(false);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '保存失败');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function onRemove(id: string) {
|
||||
if (!confirm('确认软删除该商品?')) return;
|
||||
setError(null);
|
||||
try {
|
||||
await removeProduct(id);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
const apiErr = e as ApiError;
|
||||
setError(apiErr.kind === 'envelope' ? apiErr.msg : '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="product-list">
|
||||
<header className="product-list__header">
|
||||
<Link className="product-list__back" to="/dashboard">
|
||||
← 返回 Dashboard
|
||||
</Link>
|
||||
<h1>商品管理</h1>
|
||||
<div className="product-list__user">
|
||||
<span>
|
||||
{user?.username ?? '匿名'}
|
||||
{user?.isAdmin ? ' · 管理员' : ''}
|
||||
</span>
|
||||
<button
|
||||
className="product-list__logout"
|
||||
onClick={() => {
|
||||
clearAuth();
|
||||
window.location.assign('/login');
|
||||
}}
|
||||
>
|
||||
退出
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section className="product-list__actions">
|
||||
<button
|
||||
className="product-list__create"
|
||||
onClick={openCreate}
|
||||
data-testid="product-create-btn"
|
||||
>
|
||||
+ 新建商品
|
||||
</button>
|
||||
</section>
|
||||
|
||||
{error && (
|
||||
<div className="product-list__error" role="alert">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!products && !error && <div className="product-list__loading">加载中…</div>}
|
||||
|
||||
{products && products.length === 0 && (
|
||||
<div className="product-list__empty">暂无商品</div>
|
||||
)}
|
||||
|
||||
{products && products.length > 0 && (
|
||||
<table className="product-list__table" data-testid="product-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>商品名</th>
|
||||
<th>SKU</th>
|
||||
<th>分类</th>
|
||||
<th>价格</th>
|
||||
<th>库存</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{products.map((p) => (
|
||||
<tr key={p.f_id} data-testid={`product-row-${p.f_id}`}>
|
||||
<td>{p.f_name}</td>
|
||||
<td>{p.f_sku ?? '-'}</td>
|
||||
<td>{p.f_category ?? '-'}</td>
|
||||
<td>{p.f_price ?? '-'}</td>
|
||||
<td>{p.f_stock}</td>
|
||||
<td>
|
||||
<button onClick={() => openEdit(p)}>编辑</button>{' '}
|
||||
<button onClick={() => onRemove(p.f_id)}>删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
|
||||
{showForm && (
|
||||
<div className="product-list__modal">
|
||||
<form className="product-list__modal-card" onSubmit={onSubmit}>
|
||||
<h2>{editing ? '编辑商品' : '新建商品'}</h2>
|
||||
<label>
|
||||
商品名 <em>*</em>
|
||||
<input
|
||||
required
|
||||
value={form.f_name}
|
||||
onChange={(e) => setForm({ ...form, f_name: e.target.value })}
|
||||
data-testid="product-form-name"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
SKU
|
||||
<input
|
||||
value={form.f_sku}
|
||||
onChange={(e) => setForm({ ...form, f_sku: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
分类
|
||||
<input
|
||||
value={form.f_category}
|
||||
onChange={(e) => setForm({ ...form, f_category: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
价格
|
||||
<input
|
||||
value={form.f_price}
|
||||
onChange={(e) => setForm({ ...form, f_price: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
库存
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={form.f_stock}
|
||||
onChange={(e) => setForm({ ...form, f_stock: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<div className="product-list__modal-actions">
|
||||
<button type="submit" disabled={submitting} data-testid="product-form-submit">
|
||||
{submitting ? '保存中…' : '保存'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowForm(false)}
|
||||
disabled={submitting}
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user