Selaa lähdekoodia

增加查询下一个图片的接口

AnlaAnla 4 viikkoa sitten
vanhempi
commit
721a6c275c
3 muutettua tiedostoa jossa 43 lisäystä ja 2 poistoa
  1. 3 1
      Test/test01.py
  2. 40 0
      app/api/cards.py
  3. 0 1
      app/core/config.py

+ 3 - 1
Test/test01.py

@@ -34,5 +34,7 @@ def send(url):
 
 if __name__ == '__main__':
     # base_url = 'http://127.0.0.1:7745/api/cards/query/9'
-    base_url = 'http://192.168.31.243:7745/api/cards/query/6'
+    base_url = 'http://127.0.0.1:7745/api/cards/query_next/5'
+
+    # base_url = 'http://192.168.31.243:7745/api/cards/query/6'
     send(base_url)

+ 40 - 0
app/api/cards.py

@@ -81,6 +81,46 @@ def get_card_details(id: int, db_conn: PooledMySQLConnection = db_dependency):
             cursor.close()
 
 
+@router.get("/query_next/{id}", response_model=CardDetailResponse, summary="获取指定卡牌id的下一个卡的详细信息")
+def get_card_details(id: int, db_conn: PooledMySQLConnection = db_dependency):
+    """获取指定ID的下一张卡牌的元数据以及所有与之关联的图片信息。"""
+    try:
+        with db_conn.cursor(dictionary=True) as cursor:
+
+            # 1. 获取下一张卡牌的
+            query_next_card = (
+                f"SELECT * FROM {settings.DB_CARD_TABLE_NAME} "
+                f"WHERE id > %s ORDER BY id ASC LIMIT 1"
+            )
+            cursor.execute(query_next_card, (id,))
+            next_card_data = cursor.fetchone()
+
+            # 如果没有找到下一张卡牌,则抛出 404 错误
+            if not next_card_data:
+                raise HTTPException(status_code=404, detail=f"ID为 {id} 的下一个卡牌未找到。")
+
+            # 从获取到的下一张卡牌数据中提取其ID
+            next_card_id = next_card_data['id']
+
+            # 2. 使用【下一个卡牌的ID】来获取所有关联的图片信息
+            query_images = f"SELECT * FROM {settings.DB_IMAGE_TABLE_NAME} WHERE card_id = %s"
+            cursor.execute(query_images, (next_card_id,))  # <-- 关键修正:使用 next_card_id
+            image_records = cursor.fetchall()
+            images = [CardImageResponse.model_validate(row) for row in image_records]
+
+            # 3. 使用【正确的下一张卡牌数据】和【正确的图片数据】进行计算
+            card_response = card_score_calculate(next_card_data, images)  # <-- 关键修正:传入 next_card_data
+
+            return card_response
+
+    except Exception as e:
+        logger.error(f"查询下一个卡牌详情失败 (基准ID: {id}): {e}")
+        # 如果异常已经是 HTTPException,直接重新抛出,否则包装成 500 错误
+        if isinstance(e, HTTPException):
+            raise e
+        raise HTTPException(status_code=500, detail="服务器内部错误,查询数据库失败。")
+
+
 @router.get("/card_list", response_model=List[CardListDetailResponse], summary="获取卡牌列表")
 def list_cards_detailed(
         start_id: Optional[int] = Query(None, description="筛选条件:起始 card_id"),

+ 0 - 1
app/core/config.py

@@ -23,7 +23,6 @@ class Settings:
     DB_NAME = 'card_score_database'
     # 从 Config.json 读取的旧表名, 我们将不再使用它, 但保留以兼容旧文件
     # 建议直接在代码中定义新表名, 避免混淆
-    DB_LEGACY_TABLE_NAME = 'image_records'
     DB_CARD_TABLE_NAME = 'cards'
     DB_IMAGE_TABLE_NAME = 'card_images' # 新的图片表名