From c7849bface45034df7c2a81c8e267934398bc6ba Mon Sep 17 00:00:00 2001 From: Mireya Cueto Garrido Date: Mon, 6 Apr 2026 14:26:34 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20endpoint=20historial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api/models/user_models.py | 2 + backend/api/routers/history.py | 83 ++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/backend/api/models/user_models.py b/backend/api/models/user_models.py index 4b20fc0..aa55399 100644 --- a/backend/api/models/user_models.py +++ b/backend/api/models/user_models.py @@ -1,3 +1,5 @@ +# api/models/user_models.py + from typing import List, Optional, Union from pydantic import BaseModel, EmailStr, Field from datetime import datetime diff --git a/backend/api/routers/history.py b/backend/api/routers/history.py index 0110241..03c3db1 100644 --- a/backend/api/routers/history.py +++ b/backend/api/routers/history.py @@ -1,3 +1,5 @@ +# api/routers/history.py + from fastapi import APIRouter, HTTPException, status, Depends from datetime import datetime from bson import ObjectId @@ -9,6 +11,9 @@ from api.utils.security import get_current_user router = APIRouter(prefix="/history", tags=["history"]) +# ----------------------------- +# 1. AƱadir un elemento al historial +# ----------------------------- @router.post("/add") async def add_history_item( data: HistoryCreateRequest, @@ -21,7 +26,7 @@ async def add_history_item( "_id": history_item_id, "name": data.name, "created_at": datetime.utcnow(), - "results": [r.dict() for r in data.results], # ahora soporta IT2MF + "results": [r.dict() for r in data.results], } await users_collection.update_one( @@ -35,7 +40,9 @@ async def add_history_item( } - +# ----------------------------- +# 2. Eliminar un elemento del historial +# ----------------------------- @router.delete("/delete/{history_item_id}") async def delete_history_item( history_item_id: str, @@ -55,3 +62,75 @@ async def delete_history_item( ) return {"message": "Elemento eliminado del historial"} + + +# ----------------------------- +# 3. Obtener todos los historiales de todos los usuarios (ordenados) +# ----------------------------- +@router.get("/all") +async def get_all_histories(): + users = await users_collection.find().to_list(None) + + all_histories = [] + + for user in users: + user_id = str(user["_id"]) + username = user.get("username", "unknown") + history = user.get("history", []) + + for item in history: + all_histories.append({ + "user_id": user_id, + "username": username, + "history_item": { + "_id": str(item["_id"]), + "name": item["name"], + "created_at": item["created_at"], + "results": item["results"] + } + }) + + # Ordenar por fecha + all_histories_sorted = sorted( + all_histories, + key=lambda h: h["history_item"]["created_at"] + ) + + return {"count": len(all_histories_sorted), "histories": all_histories_sorted} + + +# ----------------------------- +# 4. Obtener historiales del usuario autenticado (ordenados) +# ----------------------------- +@router.get("/user") +async def get_user_histories(current_user: dict = Depends(get_current_user)): + user_id = current_user["_id"] + + user = await users_collection.find_one({"_id": user_id}) + + if not user: + raise HTTPException( + status_code=404, + detail="Usuario no encontrado." + ) + + history = user.get("history", []) + + # Ordenar por fecha + history_sorted = sorted(history, key=lambda h: h["created_at"]) + + formatted_history = [] + for item in history_sorted: + formatted_history.append({ + "_id": str(item["_id"]), + "name": item["name"], + "created_at": item["created_at"], + "results": item["results"] + }) + + return { + "user_id": str(user_id), + "username": user.get("username", "unknown"), + "count": len(formatted_history), + "history": formatted_history + }