Added everything
This commit is contained in:
115
src/application/services/report_services.py
Normal file
115
src/application/services/report_services.py
Normal file
@@ -0,0 +1,115 @@
|
||||
from domain.reports import Report
|
||||
from application.ports.report_repository import ReportRepository
|
||||
from application.ports.user_repository import UserRepository
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from uuid import uuid4
|
||||
|
||||
class CreateReport:
|
||||
"""Use case para crear un nuevo reporte"""
|
||||
def __init__(self, repo: ReportRepository, user_repo: UserRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
if not isinstance(user_repo, UserRepository):
|
||||
raise TypeError("user_repo must implement UserRepository")
|
||||
self.repo = repo
|
||||
self.user_repo = user_repo
|
||||
|
||||
def execute(self, id_usuario: int, tipo_reporte: int, descripcion: str,
|
||||
ubicacion: Optional[str] = None) -> Report:
|
||||
# Verificar que el usuario existe
|
||||
user = self.user_repo.find_by_id(id_usuario)
|
||||
if not user:
|
||||
raise ValueError(f"Usuario con ID {id_usuario} no existe")
|
||||
|
||||
report = Report(
|
||||
id_reporte=str(uuid4()),
|
||||
id_usuario=id_usuario,
|
||||
tipo_reporte=tipo_reporte,
|
||||
descripcion=descripcion,
|
||||
ubicacion=ubicacion,
|
||||
visibilidad=50.0, # Visibilidad inicial neutral
|
||||
fecha_creacion=datetime.now()
|
||||
)
|
||||
|
||||
# Incrementar contador de reportes del usuario
|
||||
self.user_repo.increment_reports(id_usuario)
|
||||
|
||||
return self.repo.save(report)
|
||||
|
||||
class GetReportById:
|
||||
"""Use case para obtener un reporte por ID"""
|
||||
def __init__(self, repo: ReportRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
|
||||
def execute(self, report_id: str) -> Optional[Report]:
|
||||
return self.repo.find_by_id(report_id)
|
||||
|
||||
class GetReportsByUser:
|
||||
"""Use case para obtener todos los reportes de un usuario"""
|
||||
def __init__(self, repo: ReportRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
|
||||
def execute(self, user_id: int) -> List[Report]:
|
||||
return self.repo.find_by_user_id(user_id)
|
||||
|
||||
class ListAllReports:
|
||||
"""Use case para obtener todos los reportes"""
|
||||
def __init__(self, repo: ReportRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
|
||||
def execute(self) -> List[Report]:
|
||||
return self.repo.find_all()
|
||||
|
||||
class UpdateReportVisibility:
|
||||
"""Use case para actualizar la visibilidad de un reporte basado en votación comunitaria"""
|
||||
def __init__(self, repo: ReportRepository, user_repo: UserRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
self.user_repo = user_repo
|
||||
|
||||
def execute(self, report_id: str, new_visibility: float, penalize_author: bool = False) -> None:
|
||||
# Validar rango de visibilidad
|
||||
if new_visibility < 0 or new_visibility > 100:
|
||||
raise ValueError("La visibilidad debe estar entre 0 y 100")
|
||||
|
||||
report = self.repo.find_by_id(report_id)
|
||||
if not report:
|
||||
raise ValueError(f"Reporte con ID {report_id} no existe")
|
||||
|
||||
self.repo.update_visibility(report_id, new_visibility)
|
||||
|
||||
# Si la visibilidad es muy baja (shadowban), penalizar al autor
|
||||
if penalize_author and new_visibility < 20:
|
||||
user = self.user_repo.find_by_id(report.id_usuario)
|
||||
if user:
|
||||
# Reducir calificación del usuario
|
||||
new_rating = max(0, user.calificacion - 5)
|
||||
self.user_repo.update_rating(report.id_usuario, new_rating)
|
||||
|
||||
class GetShadowbannedReports:
|
||||
"""Use case para obtener reportes shadowbaneados (baja visibilidad)"""
|
||||
def __init__(self, repo: ReportRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
|
||||
def execute(self, visibility_threshold: float = 20) -> List[Report]:
|
||||
return self.repo.find_shadowbanned(visibility_threshold)
|
||||
|
||||
class DeleteReport:
|
||||
"""Use case para eliminar un reporte"""
|
||||
def __init__(self, repo: ReportRepository):
|
||||
if not isinstance(repo, ReportRepository):
|
||||
raise TypeError("repo must implement ReportRepository")
|
||||
self.repo = repo
|
||||
|
||||
def execute(self, report_id: str) -> bool:
|
||||
return self.repo.delete(report_id)
|
||||
Reference in New Issue
Block a user