diff --git a/main.py b/main.py index 3445756..f9bff10 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,12 @@ from fastapi import FastAPI import logging from contextlib import asynccontextmanager -from database import create_tables, delete_tables + from router_questions import router as questions_router from router_class import router as class_router from router_flight import router as flight_router from router_reviews import router as reviews_router +from router_statistics import router as statistic_router # Настройка логирования logging.basicConfig(level=logging.INFO) @@ -23,4 +24,5 @@ app.include_router(questions_router) app.include_router(class_router) app.include_router(flight_router) app.include_router(reviews_router) +app.include_router(statistic_router) diff --git a/repository.py b/repository.py index 0d09d4b..4560f83 100644 --- a/repository.py +++ b/repository.py @@ -1,4 +1,4 @@ -from sqlalchemy import select, delete +from sqlalchemy import select, delete, func from sqlalchemy.orm import joinedload from datetime import datetime import urllib.parse @@ -68,3 +68,11 @@ class QuestionRepository: question_models = result.scalars().all() question_schemas = [SQuestion.model_validate(question_model) for question_model in question_models] return question_schemas + + @staticmethod + async def get_class_statistics() -> dict: + async with new_session() as session: + query = select(QuestionOrm.answer, func.count(QuestionOrm.id)).group_by(QuestionOrm.answer) + result = await session.execute(query) + statistics = {answer: count for answer, count in result.fetchall()} + return statistics diff --git a/router_class.py b/router_class.py index 7a5b313..e882f04 100644 --- a/router_class.py +++ b/router_class.py @@ -1,6 +1,8 @@ from fastapi import APIRouter from typing import List +from repository import QuestionRepository + router = APIRouter( prefix="/class", tags=["Class"], @@ -21,3 +23,4 @@ async def get_class_names() -> List[str]: + diff --git a/router_statistics.py b/router_statistics.py new file mode 100644 index 0000000..2ba9673 --- /dev/null +++ b/router_statistics.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter + +from repository import QuestionRepository + +router = APIRouter( + prefix="/statistics", + tags=["Statistics"], +) + +@router.get("/") +async def get_class_statistics(): + statistics = await QuestionRepository.get_class_statistics() + return statistics + + +