Added everything
This commit is contained in:
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