Nuevos cambios en el backend

This commit is contained in:
Mireya Cueto Garrido
2026-05-19 10:21:34 +02:00
parent 02da44fb30
commit ba2507918b
20 changed files with 494 additions and 55 deletions
+43
View File
@@ -0,0 +1,43 @@
from typing import Annotated
from fastapi import APIRouter, Depends, status
from app.core.auth import get_current_user
from app.models.user import User
from app.schemas.user import GoogleLoginRequest, TokenResponse, UserLogin, UserRead, UserRegister
from app.services.auth_service import AuthService, get_auth_service
router = APIRouter(prefix="/auth", tags=["auth"])
@router.post("/register", response_model=UserRead, status_code=status.HTTP_201_CREATED)
def register(
payload: UserRegister,
auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> UserRead:
return auth_service.register(payload)
@router.post("/login", response_model=TokenResponse)
def login(
payload: UserLogin,
auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> TokenResponse:
user = auth_service.authenticate(payload)
token = auth_service.create_access_token(user.id)
return TokenResponse(access_token=token)
@router.post("/google", response_model=TokenResponse)
def login_with_google(
payload: GoogleLoginRequest,
auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> TokenResponse:
user = auth_service.login_with_google(payload.id_token)
token = auth_service.create_access_token(user.id)
return TokenResponse(access_token=token)
@router.get("/me", response_model=UserRead)
def get_me(current_user: Annotated[User, Depends(get_current_user)]) -> UserRead:
return UserRead.model_validate(current_user)
+8 -3
View File
@@ -4,7 +4,9 @@ from typing import Annotated
from fastapi import APIRouter, Depends, Response
from app.api.dependencies import get_exam_service
from app.core.auth import get_current_user
from app.models.exam import ExportFormat
from app.models.user import User
from app.services.exam_service import ExamService
router = APIRouter(prefix="/export", tags=["exports"])
@@ -13,25 +15,28 @@ router = APIRouter(prefix="/export", tags=["exports"])
@router.get("/xml/{template_id}")
def export_xml(
template_id: uuid.UUID,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> Response:
export = service.export(template_id, ExportFormat.XML)
export = service.export(current_user.id, template_id, ExportFormat.XML)
return Response(content=export.content, media_type="application/xml")
@router.get("/txt/{template_id}")
def export_txt(
template_id: uuid.UUID,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> Response:
export = service.export(template_id, ExportFormat.TXT)
export = service.export(current_user.id, template_id, ExportFormat.TXT)
return Response(content=export.content, media_type="text/plain; charset=utf-8")
@router.get("/json/{template_id}")
def export_json(
template_id: uuid.UUID,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> Response:
export = service.export(template_id, ExportFormat.JSON)
export = service.export(current_user.id, template_id, ExportFormat.JSON)
return Response(content=export.content, media_type="application/json")
+13 -3
View File
@@ -4,6 +4,8 @@ from typing import Annotated
from fastapi import APIRouter, Depends
from app.api.dependencies import get_exam_service, get_llm_client
from app.core.auth import get_current_user
from app.models.user import User
from app.schemas.exam import (
BuildPromptRequest,
GenerateExamRequest,
@@ -21,23 +23,31 @@ router = APIRouter(tags=["generation"])
def build_prompt(
template_id: uuid.UUID,
payload: BuildPromptRequest,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> PromptResponse:
return service.build_prompt(template_id, payload.topic_prompt)
return service.build_prompt(current_user.id, template_id, payload.topic_prompt)
@router.post("/generate", response_model=ParsedQuestionsResponse)
async def generate_exam(
payload: GenerateExamRequest,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
llm_client: Annotated[LLMClient, Depends(get_llm_client)],
) -> ParsedQuestionsResponse:
return await service.generate_with_llm(payload.template_id, payload.topic_prompt, llm_client)
return await service.generate_with_llm(
current_user.id,
payload.template_id,
payload.topic_prompt,
llm_client,
)
@router.post("/parse", response_model=ParsedQuestionsResponse)
def parse_ai_output(
payload: ParseRequest,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> ParsedQuestionsResponse:
return service.parse_and_persist(payload)
return service.parse_and_persist(current_user.id, payload)
+19
View File
@@ -0,0 +1,19 @@
from typing import Annotated
from fastapi import APIRouter, Depends
from app.core.auth import get_current_user
from app.models.user import User
from app.schemas.exam import ExamHistoryItem
from app.services.exam_service import ExamService
from app.api.dependencies import get_exam_service
router = APIRouter(prefix="/history", tags=["history"])
@router.get("", response_model=list[ExamHistoryItem])
def list_exam_history(
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> list[ExamHistoryItem]:
return service.list_history(current_user.id)
+11 -4
View File
@@ -4,6 +4,8 @@ from typing import Annotated
from fastapi import APIRouter, Depends, status
from app.api.dependencies import get_exam_service
from app.core.auth import get_current_user
from app.models.user import User
from app.schemas.exam import ExamTemplateCreate, ExamTemplateRead
from app.services.exam_service import ExamService
@@ -13,19 +15,24 @@ router = APIRouter(prefix="/templates", tags=["templates"])
@router.post("", response_model=ExamTemplateRead, status_code=status.HTTP_201_CREATED)
def create_template(
payload: ExamTemplateCreate,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> ExamTemplateRead:
return service.create_template(payload)
return service.create_template(current_user.id, payload)
@router.get("", response_model=list[ExamTemplateRead])
def list_templates(service: Annotated[ExamService, Depends(get_exam_service)]) -> list[ExamTemplateRead]:
return service.list_templates()
def list_templates(
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> list[ExamTemplateRead]:
return service.list_templates(current_user.id)
@router.get("/{template_id}", response_model=ExamTemplateRead)
def get_template(
template_id: uuid.UUID,
current_user: Annotated[User, Depends(get_current_user)],
service: Annotated[ExamService, Depends(get_exam_service)],
) -> ExamTemplateRead:
return service.get_template(template_id)
return service.get_template(current_user.id, template_id)