File size: 1,393 Bytes
3894d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import time

CATEGORIES = ["Language", "Math", "Science", "Music", "Geography", "History", "Transition", "Health"]

# --- 内部辅助:获取自增 ID ---
async def _get_next_id(db, category):
    await db.inc("counters", category, "current_value", 1)
    doc = await db.get("counters", category)
    return str(doc["current_value"])

# --- 上传资料 (默认 pending 状态) ---
async def upload_material_logic(db, category, title, url, user_id):
    if category not in CATEGORIES: return None
    new_id = await _get_next_id(db, category)
    data = {
        "title": title,
        "url": url,
        "author": user_id,
        "status": "pending",  # 初始状态为待审核
        "time": int(time.time())
    }
    await db.set(category, new_id, data)
    return new_id

# --- 审核资料 ---
async def approve_material_logic(db, category, index_id):
    return await db.update(category, index_id, {"status": "approved"})

# --- 删除资料 ---
async def delete_material_logic(db, category, index_id):
    return await db.delete(category, index_id)

# --- 获取列表 (根据状态过滤) ---
async def list_materials_logic(db, category, status="approved"):
    # 如果传入了 category,只查该学科;否则可能需要遍历(为了简洁,这里建议指定 category)
    query = {"status": status}
    return await db.find_many(category, query=query, limit=20)