Files
YunNanProject/Controller/EducationDataController.py
2025-09-11 21:34:02 +08:00

68 lines
2.0 KiB
Python

from fastapi import APIRouter, Query
from Model.EducationDataModel import EducationDataModel
# 创建APIRouter实例
router = APIRouter(prefix="/EducationData", tags=["教育统计数据"])
@router.get("/byYear")
async def get_education_data_by_year(
year: int = Query(default=2023, ge=2015, le=2028,
description="年份: 2015-2028范围内的年份")
):
"""获取指定年份所有学段的教育数据"""
try:
# 调用EducationDataModel的方法获取数据
data = EducationDataModel.get_education_data_by_year(year)
# 返回包含状态和数据的响应
return {
"code": 200,
"message": "success",
"data": data
}
except Exception as e:
# 异常处理
return {
"code": 500,
"message": f"获取数据失败: {str(e)}",
"data": []
}
@router.get("/populationByYear")
async def get_population_data_by_year(
year: int = Query(default=2023, ge=2015, le=2028,
description="年份: 2015-2028范围内的年份")
):
"""获取指定年份的云南省人口数据"""
try:
# 调用EducationDataModel的方法获取人口数据
data = EducationDataModel.get_population_data_by_year(year)
if data:
# 返回包含状态和数据的响应
return {
"code": 200,
"message": "success",
"data": data
}
else:
# 未找到数据的情况
return {
"code": 404,
"message": f"未找到{year}年的云南省人口数据",
"data": None
}
except Exception as e:
# 异常处理
return {
"code": 500,
"message": f"获取数据失败: {str(e)}",
"data": None
}