feat(admin): 分区增删、资料设置、操作条与交互收尾

- POST/DELETE /api/categories(需登录,级联,local 保护)
- profile 设置(头像/名称/角色/简介/位置)应用到资料卡
- 详情操作条:浏览 +1、点赞切换、分享复制、跳转外链
- 编辑按钮垂直居中且留 12px;设置页按钮上间距、保存 Toast、Tabs 去滚动条
- 登录密码可见切换
- 测试 48 passed
This commit is contained in:
stash
2026-10-29 20:15:56 +08:00
parent 3c6c20e534
commit 427338b329
15 changed files with 851 additions and 54 deletions
+202
View File
@@ -0,0 +1,202 @@
//! 分区(首页卡片 / 侧栏项)的新增与删除。
//!
//! 分区就是 `node` 表里 `kind='section'` 的行;删除会级联分组、条目与展示块。
//! 新增时顺手建一条首页展示块,保证它立刻出现在首页。
use rusqlite::{Connection, params};
use serde::Deserialize;
use crate::{db::map_error, error::StashError, db::entries};
const KINDS: [&str; 3] = ["apps", "posters", "articles"];
#[derive(Debug, Clone, Deserialize)]
pub struct NewCategory {
/// 业务键,只允许小写字母、数字与连字符
pub id: String,
pub title: String,
/// apps / posters / articles
pub kind: String,
}
fn valid_id(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 32
&& id
.chars()
.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '-')
}
pub fn create(conn: &mut Connection, new: NewCategory) -> Result<(), StashError> {
let id = new.id.trim();
let title = new.title.trim();
if !valid_id(id) {
return Err(StashError::InvalidDirectory(
"标识只能用小写字母、数字与连字符(≤32)".into(),
));
}
if title.is_empty() || title.chars().count() > 40 {
return Err(StashError::InvalidDirectory("标题需为 1–40 个字".into()));
}
if !KINDS.contains(&new.kind.as_str()) {
return Err(StashError::InvalidDirectory("类型不合法".into()));
}
let exists: i64 = conn
.query_row("SELECT COUNT(*) FROM node WHERE key = ?1", [id], |row| row.get(0))
.map_err(map_error)?;
if exists > 0 {
return Err(StashError::Conflict(format!("分区已存在: {id}")));
}
let tx = conn.transaction().map_err(map_error)?;
let sort: i64 = tx
.query_row(
"SELECT COALESCE(MAX(sort), 0) + 1 FROM node WHERE kind = 'section'",
[],
|row| row.get(0),
)
.map_err(map_error)?;
tx.execute(
"INSERT INTO node (key, parent_key, title, kind, view_mode, sort, show_in_sidebar)
VALUES (?1, NULL, ?2, 'section', ?3, ?4, 1)",
params![id, title, new.kind, sort],
)
.map_err(map_error)?;
let block_sort: i64 = tx
.query_row(
"SELECT COALESCE(MAX(sort), 0) + 1 FROM section_block",
[],
|row| row.get(0),
)
.map_err(map_error)?;
tx.execute(
"INSERT INTO section_block (node_key, title, preview_limit, sort) VALUES (?1, NULL, NULL, ?2)",
params![id, block_sort],
)
.map_err(map_error)?;
entries::audit(&tx, "category.create", id, title)?;
tx.commit().map_err(map_error)?;
Ok(())
}
pub fn remove(conn: &mut Connection, id: &str) -> Result<(), StashError> {
if id == "local" {
return Err(StashError::InvalidDirectory("本地服务不能删除".into()));
}
let removed = conn
.execute("DELETE FROM node WHERE key = ?1 AND kind = 'section'", [id])
.map_err(map_error)?;
if removed == 0 {
return Err(StashError::NotFound(format!("分区不存在: {id}")));
}
entries::audit(conn, "category.delete", id, "")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn db() -> Connection {
let conn = Connection::open_in_memory().expect("内存库应能打开");
conn.execute_batch(
"PRAGMA foreign_keys = ON;
CREATE TABLE node (
id INTEGER PRIMARY KEY,
key TEXT NOT NULL UNIQUE,
parent_key TEXT REFERENCES node(key) ON DELETE CASCADE ON UPDATE CASCADE,
title TEXT NOT NULL,
kind TEXT NOT NULL,
view_mode TEXT,
sort INTEGER NOT NULL DEFAULT 0,
show_in_sidebar INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE section_block (
id INTEGER PRIMARY KEY,
node_key TEXT NOT NULL REFERENCES node(key) ON DELETE CASCADE ON UPDATE CASCADE,
title TEXT,
preview_limit INTEGER,
sort INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
at INTEGER NOT NULL,
action TEXT NOT NULL,
target TEXT,
detail TEXT
);",
)
.expect("应能建表");
conn
}
#[test]
fn create_then_delete() {
let mut conn = db();
create(
&mut conn,
NewCategory {
id: "books".into(),
title: "书单".into(),
kind: "posters".into(),
},
)
.expect("应能创建");
let blocks: i64 = conn
.query_row("SELECT COUNT(*) FROM section_block", [], |row| row.get(0))
.expect("应能查");
assert_eq!(blocks, 1);
remove(&mut conn, "books").expect("应能删除");
let nodes: i64 = conn
.query_row("SELECT COUNT(*) FROM node", [], |row| row.get(0))
.expect("应能查");
assert_eq!(nodes, 0);
}
#[test]
fn rejects_bad_id_and_duplicate() {
let mut conn = db();
assert!(
create(
&mut conn,
NewCategory {
id: "Bad Id".into(),
title: "x".into(),
kind: "apps".into()
}
)
.is_err()
);
create(
&mut conn,
NewCategory {
id: "books".into(),
title: "书单".into(),
kind: "apps".into(),
},
)
.expect("应能创建");
let error = create(
&mut conn,
NewCategory {
id: "books".into(),
title: "书单".into(),
kind: "apps".into(),
},
)
.expect_err("重复应失败");
assert!(matches!(error, StashError::Conflict(_)));
}
}