Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ body:
label: 期望的解决方案
description: 描述你期望的功能行为,如有具体的 API 设计或交互方式请一并说明
validations:
required: true
required: false

- type: textarea
id: alternatives
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qb_api"
version = "1.0.0"
version = "1.1.0"
edition = "2021"

[dependencies]
Expand Down
58 changes: 57 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
| `GET /auth/me` | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `PATCH /auth/me/password` | — | ✅ | ✅ | ✅ | — | ✅ |
| `POST /auth/logout` | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `GET` questions/papers/tags | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `GET /questions`, `POST /questions/search`, `GET` questions/papers/tags | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `POST` bundles | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `POST /questions`(上传) | — | — | ✅ | ✅ | ✅ | ✅ |
| `PATCH /questions/:id`(更新) | — | — | ⚠️¹ | ⚠️³ | ✅ | ✅ |
Expand Down Expand Up @@ -474,6 +474,62 @@ Bot 账号不会自动生成;管理员创建或轮换 bot 账号时,接口

**Query 参数**:`paper_id`, `category`, `tag`, `author`, `reviewer`(支持逗号分隔多值,匹配任一), `assigned_reviewer_id`, `score_min`, `score_max`, `difficulty_tag`, `difficulty_min`, `difficulty_max`, `q`, `created_after`, `created_before`, `updated_after`, `updated_before`, `limit` (1-100, 默认 20), `offset` (默认 0)。

- `tag` 仍用于简单单标签精确匹配
- 复杂标签组合查询请使用 `POST /questions/search`

**成功响应** `200`:分页包裹,`items` 为 `QuestionSummary[]`。

---

#### `POST /questions/search`

按 JSON 逻辑树进行高级题目搜索。认证:`viewer` 及以上。

- **Content-Type**:`application/json`
- **说明**:除 `tag_filter` 外,其余字段与 `GET /questions` 的过滤字段一致;返回格式也一致

**请求体示例**:

```json
{
"category": "T",
"q": "pendulum",
"limit": 20,
"offset": 0,
"tag_filter": {
"type": "or",
"children": [
{ "type": "tag", "tag": "mechanics" },
{
"type": "and",
"children": [
{ "type": "tag", "tag": "contest" },
{
"type": "not",
"child": { "type": "tag", "tag": "deprecated" }
}
]
}
]
}
}
```

**`tag_filter` 节点结构**:

| `type` | 其他字段 | 说明 |
|---|---|---|
| `"tag"` | `tag: string` | 精确匹配单个标签 |
| `"and"` | `children: TagFilter[]` | 子条件全部满足 |
| `"or"` | `children: TagFilter[]` | 子条件任一满足 |
| `"not"` | `child: TagFilter` | 子条件不满足 |

**校验规则**:

- `tag_filter.tag` 会先 trim,不能为空
- `and` / `or` 的 `children` 不能为空数组
- 后端只接受固定 JSON 结构并把所有 tag 值作为 SQL 绑定参数处理,不接受原始 SQL / 表达式字符串

**成功响应** `200`:分页包裹,`items` 为 `QuestionSummary[]`。

---
Expand Down
2 changes: 2 additions & 0 deletions migrations/0004_question_tags_tag_question_id_idx.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE INDEX IF NOT EXISTS idx_question_tags_tag_question_id
ON question_tags(tag, question_id);
11 changes: 10 additions & 1 deletion scripts/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def _cfg(key: str, default: str) -> str:
return os.environ.get(key, _dotenv.get(key, default))


def _cfg_any(keys: tuple[str, ...], default: str) -> str:
"""Resolve config from the first present key across env and .env."""
for key in keys:
value = os.environ.get(key, _dotenv.get(key))
if value is not None:
return value
return default


POSTGRES_USER = _cfg("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD = _cfg("POSTGRES_PASSWORD", "postgres")
POSTGRES_DB = _cfg("POSTGRES_DB", "qb")
Expand All @@ -49,5 +58,5 @@ def _cfg(key: str, default: str) -> str:
CONTAINER_NAME = _cfg("CONTAINER_NAME", "qb-postgres-e2e")
POSTGRES_IMAGE = _cfg("POSTGRES_IMAGE", f"postgres:{POSTGRES_MAJOR}")
POSTGRES_PORT = _cfg("POSTGRES_PORT", "55433")
API_PORT = _cfg("QB_BIND_PORT", "8080")
API_PORT = _cfg_any(("QB_BIND_PORT", "API_PORT"), "18080")
DB_URL = f"postgres://{POSTGRES_USER}:{POSTGRES_PASSWORD}@127.0.0.1:{POSTGRES_PORT}/{POSTGRES_DB}"
130 changes: 130 additions & 0 deletions scripts/tests/test_1_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def assert_question_query(
)


def assert_question_search(
api: ApiClient, payload: dict, expected_ids: list[str],
) -> None:
_, body, _ = api.post_json("/questions/search", payload)
actual = question_ids_from_body(body)
if expected_ids:
missing = set(expected_ids) - set(actual)
assert not missing, (
f"search {payload}: missing {sorted(missing)} from {sorted(actual)}"
)
else:
assert actual == [], (
f"search {payload}: expected empty, got {sorted(actual)}"
)


def _apply_question_patch(api: ApiClient, question_id: str, patch: dict) -> None:
"""Apply per-field PATCH / POST calls to match the spec['patch'] dict.

Expand Down Expand Up @@ -388,6 +404,120 @@ def test_filter_questions(api, state):
api.get("/questions?updated_before=abc", expect=400)


def test_search_questions(api, state):
"""POST /questions/search supports boolean tag logic and shared filters."""
qs = state.q_by_slug

assert_question_search(
api,
{
"tag_filter": {
"type": "or",
"children": [
{"type": "tag", "tag": "mechanics"},
{"type": "tag", "tag": "optics"},
],
},
"limit": 100,
"offset": 0,
},
[qs["mechanics"], qs["optics"]],
)

assert_question_search(
api,
{
"tag_filter": {
"type": "and",
"children": [
{"type": "tag", "tag": "mechanics"},
{
"type": "not",
"child": {"type": "tag", "tag": "optics"},
},
],
},
},
[qs["mechanics"]],
)

assert_question_search(
api,
{
"tag": "mechanics",
"tag_filter": {
"type": "or",
"children": [
{"type": "tag", "tag": "mechanics"},
{"type": "tag", "tag": "optics"},
],
},
},
[qs["mechanics"]],
)

assert_question_search(
api,
{
"q": "热学",
"tag_filter": {"type": "tag", "tag": "thermal"},
"difficulty_tag": "human",
"difficulty_min": 5,
"difficulty_max": 5,
},
[qs["thermal"]],
)

assert_question_search(
api,
{
"category": "E",
"tag_filter": {
"type": "or",
"children": [
{"type": "tag", "tag": "mechanics"},
{"type": "tag", "tag": "optics"},
],
},
},
[qs["optics"]],
)


def test_search_questions_validation(api):
api.post_json(
"/questions/search",
{"tag_filter": {"type": "tag", "tag": " "}},
expect=400,
)
api.post_json(
"/questions/search",
{"tag_filter": {"type": "and", "children": []}},
expect=400,
)
api.post_json(
"/questions/search",
{"difficulty_min": 5},
expect=400,
)
api.post_json(
"/questions/search",
{"q": "ok", "extra": 1},
expect=400,
)
api.post_json(
"/questions/search",
{
"tag_filter": {
"type": "tag",
"tag": "mechanics",
"extra": 1,
},
},
expect=400,
)


def test_filter_questions_by_author(api, state):
"""Filter questions by author (exact match on questions.author field).

Expand Down
1 change: 1 addition & 0 deletions src/api/admin/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl AdminQuestionsParams {
paper_id: self.paper_id.clone(),
category: self.category.clone(),
tag: self.tag.clone(),
tag_filter: None,
author: self.author.clone(),
reviewer: self.reviewer.clone(),
assigned_reviewer_id: self.assigned_reviewer_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
| `GET /auth/me` | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `PATCH /auth/me/password` | — | ✅ | ✅ | ✅ | — | ✅ |
| `POST /auth/logout` | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `GET` questions/papers/tags | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `GET /questions`, `POST /questions/search`, `GET` questions/papers/tags | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `POST` bundles | — | ✅ | ✅ | ✅ | ✅ | ✅ |
| `POST /questions`(上传) | — | — | ✅ | ✅ | ✅ | ✅ |
| `PATCH /questions/:id`(更新) | — | — | ⚠️¹ | ⚠️³ | ✅ | ✅ |
Expand Down
4 changes: 4 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub fn router(state: AppState, cors_origins: &[String]) -> Router {
"/questions/tags",
axum::routing::get(questions::handlers::list_question_tags),
)
.route(
"/questions/search",
axum::routing::post(questions::handlers::search_questions),
)
.route(
"/questions/difficulty-tags",
axum::routing::get(questions::handlers::list_difficulty_tags),
Expand Down
56 changes: 56 additions & 0 deletions src/api/questions/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,62 @@

**Query 参数**:`paper_id`, `category`, `tag`, `author`, `reviewer`(支持逗号分隔多值,匹配任一), `assigned_reviewer_id`, `score_min`, `score_max`, `difficulty_tag`, `difficulty_min`, `difficulty_max`, `q`, `created_after`, `created_before`, `updated_after`, `updated_before`, `limit` (1-100, 默认 20), `offset` (默认 0)。

- `tag` 仍用于简单单标签精确匹配
- 复杂标签组合查询请使用 `POST /questions/search`

**成功响应** `200`:分页包裹,`items` 为 `QuestionSummary[]`。

---

### `POST /questions/search`

按 JSON 逻辑树进行高级题目搜索。认证:`viewer` 及以上。

- **Content-Type**:`application/json`
- **说明**:除 `tag_filter` 外,其余字段与 `GET /questions` 的过滤字段一致;返回格式也一致

**请求体示例**:

```json
{
"category": "T",
"q": "pendulum",
"limit": 20,
"offset": 0,
"tag_filter": {
"type": "or",
"children": [
{ "type": "tag", "tag": "mechanics" },
{
"type": "and",
"children": [
{ "type": "tag", "tag": "contest" },
{
"type": "not",
"child": { "type": "tag", "tag": "deprecated" }
}
]
}
]
}
}
```

**`tag_filter` 节点结构**:

| `type` | 其他字段 | 说明 |
|---|---|---|
| `"tag"` | `tag: string` | 精确匹配单个标签 |
| `"and"` | `children: TagFilter[]` | 子条件全部满足 |
| `"or"` | `children: TagFilter[]` | 子条件任一满足 |
| `"not"` | `child: TagFilter` | 子条件不满足 |

**校验规则**:

- `tag_filter.tag` 会先 trim,不能为空
- `and` / `or` 的 `children` 不能为空数组
- 后端只接受固定 JSON 结构并把所有 tag 值作为 SQL 绑定参数处理,不接受原始 SQL / 表达式字符串

**成功响应** `200`:分页包裹,`items` 为 `QuestionSummary[]`。

---
Expand Down
Loading
Loading