Merge pull request #18 from AlexisLopez-Dev/feature/backend-v3

Añadido endpoint historial
This commit is contained in:
Mireya Cueto Garrido
2026-04-06 14:27:14 +02:00
committed by GitHub
2 changed files with 83 additions and 2 deletions
+2
View File
@@ -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
+81 -2
View File
@@ -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
}