Added everything
This commit is contained in:
0
src/infrastructure/api/__init__.py
Normal file
0
src/infrastructure/api/__init__.py
Normal file
0
src/infrastructure/api/reports/__init__.py
Normal file
0
src/infrastructure/api/reports/__init__.py
Normal file
13
src/infrastructure/api/reports/app.py
Normal file
13
src/infrastructure/api/reports/app.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from fastapi import FastAPI
|
||||
from core.config import ConfSettings
|
||||
from infrastructure.api.reports.router import router
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Factory para crear la aplicación de Reportes"""
|
||||
app = FastAPI(
|
||||
title="Reportes Microservice",
|
||||
version="1.0.0",
|
||||
description="Microservicio de gestión de reportes comunitarios"
|
||||
)
|
||||
app.include_router(router)
|
||||
return app
|
||||
99
src/infrastructure/api/reports/reports.py
Normal file
99
src/infrastructure/api/reports/reports.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from infrastructure.api.reports.schemas import ReportCreateRequest, ReportUpdateVisibilityRequest, ReportResponse
|
||||
from application.services.report_services import (
|
||||
CreateReport, GetReportById, GetReportsByUser, ListAllReports,
|
||||
UpdateReportVisibility, GetShadowbannedReports, DeleteReport
|
||||
)
|
||||
from infrastructure.adapters.persistence.report_repository_mongo import ReportRepositoryMongo
|
||||
from infrastructure.adapters.persistence.user_repository_sql import UserRepositorySQL
|
||||
|
||||
router = APIRouter()
|
||||
report_repo = ReportRepositoryMongo()
|
||||
user_repo = UserRepositorySQL()
|
||||
|
||||
@router.post("/", response_model=ReportResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_report(report_data: ReportCreateRequest):
|
||||
"""Crea un nuevo reporte"""
|
||||
try:
|
||||
create_use_case = CreateReport(report_repo, user_repo)
|
||||
report = create_use_case.execute(
|
||||
id_usuario=report_data.id_usuario,
|
||||
tipo_reporte=report_data.tipo_reporte,
|
||||
descripcion=report_data.descripcion,
|
||||
ubicacion=report_data.ubicacion
|
||||
)
|
||||
return report
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e)
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Error al crear reporte: {str(e)}"
|
||||
)
|
||||
|
||||
@router.get("/{report_id}", response_model=ReportResponse)
|
||||
async def get_report(report_id: str):
|
||||
"""Obtiene un reporte por ID"""
|
||||
get_use_case = GetReportById(report_repo)
|
||||
report = get_use_case.execute(report_id)
|
||||
if not report:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Reporte con ID {report_id} no encontrado"
|
||||
)
|
||||
return report
|
||||
|
||||
@router.get("/user/{user_id}", response_model=list[ReportResponse])
|
||||
async def get_user_reports(user_id: int):
|
||||
"""Obtiene todos los reportes de un usuario"""
|
||||
get_use_case = GetReportsByUser(report_repo)
|
||||
reports = get_use_case.execute(user_id)
|
||||
return reports
|
||||
|
||||
@router.get("/", response_model=list[ReportResponse])
|
||||
async def list_reports():
|
||||
"""Obtiene todos los reportes"""
|
||||
list_use_case = ListAllReports(report_repo)
|
||||
return list_use_case.execute()
|
||||
|
||||
@router.get("/shadowbanned/list", response_model=list[ReportResponse])
|
||||
async def get_shadowbanned_reports(threshold: float = 20):
|
||||
"""Obtiene reportes shadowbaneados (baja visibilidad)"""
|
||||
get_use_case = GetShadowbannedReports(report_repo)
|
||||
return get_use_case.execute(threshold)
|
||||
|
||||
@router.put("/{report_id}/visibility", status_code=status.HTTP_200_OK)
|
||||
async def update_report_visibility(report_id: str, visibility_data: ReportUpdateVisibilityRequest):
|
||||
"""Actualiza la visibilidad de un reporte (basado en votación comunitaria)"""
|
||||
try:
|
||||
update_use_case = UpdateReportVisibility(report_repo, user_repo)
|
||||
update_use_case.execute(
|
||||
report_id=report_id,
|
||||
new_visibility=visibility_data.new_visibility,
|
||||
penalize_author=visibility_data.penalize_author
|
||||
)
|
||||
return {
|
||||
"message": "Visibilidad actualizada exitosamente",
|
||||
"report_id": report_id,
|
||||
"new_visibility": visibility_data.new_visibility
|
||||
}
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e)
|
||||
)
|
||||
|
||||
@router.delete("/{report_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_report(report_id: str):
|
||||
"""Elimina un reporte"""
|
||||
delete_use_case = DeleteReport(report_repo)
|
||||
success = delete_use_case.execute(report_id)
|
||||
if not success:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Reporte con ID {report_id} no encontrado"
|
||||
)
|
||||
return None
|
||||
19
src/infrastructure/api/reports/root.py
Normal file
19
src/infrastructure/api/reports/root.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
"""Verifica el estado de la API"""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"service": "Reportes API"
|
||||
}
|
||||
|
||||
@router.get("/")
|
||||
async def root():
|
||||
"""Endpoint raíz"""
|
||||
return {
|
||||
"message": "API de Reportes - VoxPopuli",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
17
src/infrastructure/api/reports/router.py
Normal file
17
src/infrastructure/api/reports/router.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from fastapi import APIRouter
|
||||
from infrastructure.api.reports.reports import router as reports_router
|
||||
from infrastructure.api.reports.root import router as root_router
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
router.include_router(
|
||||
reports_router,
|
||||
prefix="/reports",
|
||||
tags=["reports"]
|
||||
)
|
||||
|
||||
router.include_router(
|
||||
root_router,
|
||||
prefix='',
|
||||
tags=["root"]
|
||||
)
|
||||
28
src/infrastructure/api/reports/schemas.py
Normal file
28
src/infrastructure/api/reports/schemas.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
class ReportCreateRequest(BaseModel):
|
||||
"""Solicitud para crear un reporte"""
|
||||
id_usuario: int
|
||||
tipo_reporte: int
|
||||
descripcion: str
|
||||
ubicacion: Optional[str] = None
|
||||
|
||||
class ReportUpdateVisibilityRequest(BaseModel):
|
||||
"""Solicitud para actualizar la visibilidad de un reporte"""
|
||||
new_visibility: float
|
||||
penalize_author: bool = False
|
||||
|
||||
class ReportResponse(BaseModel):
|
||||
"""Respuesta con datos de reporte"""
|
||||
id_reporte: str
|
||||
id_usuario: int
|
||||
tipo_reporte: int
|
||||
descripcion: str
|
||||
ubicacion: Optional[str]
|
||||
visibilidad: float
|
||||
fecha_creacion: Optional[datetime]
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
0
src/infrastructure/api/users/__init__.py
Normal file
0
src/infrastructure/api/users/__init__.py
Normal file
13
src/infrastructure/api/users/app.py
Normal file
13
src/infrastructure/api/users/app.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from fastapi import FastAPI
|
||||
from core.config import ConfSettings
|
||||
from infrastructure.api.users.router import router
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Factory para crear la aplicación de Usuarios"""
|
||||
app = FastAPI(
|
||||
title="Usuarios Microservice",
|
||||
version="1.0.0",
|
||||
description="Microservicio de gestión de usuarios y autenticación"
|
||||
)
|
||||
app.include_router(router)
|
||||
return app
|
||||
19
src/infrastructure/api/users/root.py
Normal file
19
src/infrastructure/api/users/root.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
"""Verifica el estado de la API"""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"service": "Usuarios API"
|
||||
}
|
||||
|
||||
@router.get("/")
|
||||
async def root():
|
||||
"""Endpoint raíz"""
|
||||
return {
|
||||
"message": "API de Usuarios - VoxPopuli",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
17
src/infrastructure/api/users/router.py
Normal file
17
src/infrastructure/api/users/router.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from fastapi import APIRouter
|
||||
from infrastructure.api.users.users import router as users_router
|
||||
from infrastructure.api.users.root import router as root_router
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
router.include_router(
|
||||
users_router,
|
||||
prefix="/users",
|
||||
tags=["users"]
|
||||
)
|
||||
|
||||
router.include_router(
|
||||
root_router,
|
||||
prefix='',
|
||||
tags=["root"]
|
||||
)
|
||||
35
src/infrastructure/api/users/schemas.py
Normal file
35
src/infrastructure/api/users/schemas.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
class UserCreateRequest(BaseModel):
|
||||
"""Solicitud para crear un usuario"""
|
||||
nombre: str
|
||||
apellido: str
|
||||
email: str
|
||||
fecha_nacimiento: datetime
|
||||
url_foto_perfil: Optional[str] = None
|
||||
biografia: Optional[str] = None
|
||||
|
||||
class UserUpdateRequest(BaseModel):
|
||||
"""Solicitud para actualizar un usuario"""
|
||||
nombre: Optional[str] = None
|
||||
apellido: Optional[str] = None
|
||||
url_foto_perfil: Optional[str] = None
|
||||
biografia: Optional[str] = None
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
"""Respuesta con datos de usuario"""
|
||||
user_id: int
|
||||
nombre: str
|
||||
apellido: str
|
||||
email: str
|
||||
fecha_nacimiento: datetime
|
||||
fecha_creacion: datetime
|
||||
calificacion: float
|
||||
numero_reportes: int
|
||||
url_foto_perfil: Optional[str]
|
||||
biografia: Optional[str]
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
89
src/infrastructure/api/users/users.py
Normal file
89
src/infrastructure/api/users/users.py
Normal file
@@ -0,0 +1,89 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from infrastructure.api.users.schemas import UserCreateRequest, UserUpdateRequest, UserResponse
|
||||
from application.services.user_services import (
|
||||
CreateUser, GetUserById, GetUserByEmail, ListAllUsers, UpdateUser, DeleteUser
|
||||
)
|
||||
from infrastructure.adapters.persistence.user_repository_sql import UserRepositorySQL
|
||||
|
||||
router = APIRouter()
|
||||
user_repo = UserRepositorySQL()
|
||||
|
||||
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_user(user_data: UserCreateRequest):
|
||||
"""Crea un nuevo usuario"""
|
||||
try:
|
||||
create_use_case = CreateUser(user_repo)
|
||||
user = create_use_case.execute(
|
||||
nombre=user_data.nombre,
|
||||
apellido=user_data.apellido,
|
||||
email=user_data.email,
|
||||
fecha_nacimiento=user_data.fecha_nacimiento,
|
||||
url_foto_perfil=user_data.url_foto_perfil,
|
||||
biografia=user_data.biografia
|
||||
)
|
||||
return user
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Error al crear usuario: {str(e)}"
|
||||
)
|
||||
|
||||
@router.get("/{user_id}", response_model=UserResponse)
|
||||
async def get_user(user_id: int):
|
||||
"""Obtiene un usuario por ID"""
|
||||
get_use_case = GetUserById(user_repo)
|
||||
user = get_use_case.execute(user_id)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Usuario con ID {user_id} no encontrado"
|
||||
)
|
||||
return user
|
||||
|
||||
@router.get("/email/{email}", response_model=UserResponse)
|
||||
async def get_user_by_email(email: str):
|
||||
"""Obtiene un usuario por email"""
|
||||
get_use_case = GetUserByEmail(user_repo)
|
||||
user = get_use_case.execute(email)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Usuario con email {email} no encontrado"
|
||||
)
|
||||
return user
|
||||
|
||||
@router.get("/", response_model=list[UserResponse])
|
||||
async def list_users():
|
||||
"""Obtiene todos los usuarios"""
|
||||
list_use_case = ListAllUsers(user_repo)
|
||||
return list_use_case.execute()
|
||||
|
||||
@router.put("/{user_id}", response_model=UserResponse)
|
||||
async def update_user(user_id: int, user_data: UserUpdateRequest):
|
||||
"""Actualiza un usuario"""
|
||||
update_use_case = UpdateUser(user_repo)
|
||||
user = update_use_case.execute(
|
||||
user_id=user_id,
|
||||
nombre=user_data.nombre,
|
||||
apellido=user_data.apellido,
|
||||
url_foto_perfil=user_data.url_foto_perfil,
|
||||
biografia=user_data.biografia
|
||||
)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Usuario con ID {user_id} no encontrado"
|
||||
)
|
||||
return user
|
||||
|
||||
@router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_user(user_id: int):
|
||||
"""Elimina un usuario"""
|
||||
delete_use_case = DeleteUser(user_repo)
|
||||
success = delete_use_case.execute(user_id)
|
||||
if not success:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Usuario con ID {user_id} no encontrado"
|
||||
)
|
||||
return None
|
||||
Reference in New Issue
Block a user