137 lines
3.5 KiB
Python
137 lines
3.5 KiB
Python
"""
|
|
Rutas para gestión de localización
|
|
"""
|
|
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.database import get_core_db
|
|
from .dto import LocationCreateDTO, LocationResponseDTO, LocationUpdateDTO
|
|
from .models import Location
|
|
from .service import LocationService
|
|
|
|
router = APIRouter(prefix="/locations", tags=["locations"])
|
|
|
|
|
|
@router.get(
|
|
"",
|
|
response_model=dict,
|
|
summary="Get all locations",
|
|
)
|
|
async def get_all_locations(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=100),
|
|
code: str = Query(None),
|
|
description: str = Query(None),
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Get all locations with optional filtering and pagination"""
|
|
filters = {}
|
|
if code:
|
|
filters["code"] = code
|
|
if description:
|
|
filters["description"] = description
|
|
|
|
locations, total = LocationService.get_all(db, skip, limit, filters)
|
|
|
|
return {
|
|
"data": [LocationResponseDTO.model_validate(location) for location in locations],
|
|
"total": total,
|
|
"skip": skip,
|
|
"limit": limit,
|
|
}
|
|
|
|
|
|
@router.get(
|
|
"/{location_id}",
|
|
response_model=LocationResponseDTO,
|
|
summary="Get location by ID",
|
|
)
|
|
async def get_location(
|
|
location_id: int,
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Get a location by its ID"""
|
|
location = LocationService.get_by_id(db, location_id)
|
|
if not location:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Location not found",
|
|
)
|
|
return LocationResponseDTO.model_validate(location)
|
|
|
|
|
|
@router.get(
|
|
"/code/{code}",
|
|
response_model=LocationResponseDTO,
|
|
summary="Get location by code",
|
|
)
|
|
async def get_location_by_code(
|
|
code: str,
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Get a location by its code"""
|
|
location = LocationService.get_by_code(db, code)
|
|
if not location:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Location not found",
|
|
)
|
|
return LocationResponseDTO.model_validate(location)
|
|
|
|
|
|
@router.post(
|
|
"",
|
|
response_model=LocationResponseDTO,
|
|
status_code=status.HTTP_201_CREATED,
|
|
summary="Create location",
|
|
)
|
|
async def create_location(
|
|
location_data: LocationCreateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Create a new location"""
|
|
location = LocationService.create(db, location_data)
|
|
return LocationResponseDTO.model_validate(location)
|
|
|
|
|
|
@router.put(
|
|
"/{location_id}",
|
|
response_model=LocationResponseDTO,
|
|
summary="Update location",
|
|
)
|
|
async def update_location(
|
|
location_id: int,
|
|
location_data: LocationUpdateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Update a location"""
|
|
location = LocationService.update(db, location_id, location_data)
|
|
if not location:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Location not found",
|
|
)
|
|
return LocationResponseDTO.model_validate(location)
|
|
|
|
|
|
@router.delete(
|
|
"/{location_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
summary="Delete location",
|
|
)
|
|
async def delete_location(
|
|
location_id: int,
|
|
db: Session = Depends(get_core_db),
|
|
):
|
|
"""Delete a location"""
|
|
success = LocationService.delete(db, location_id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Location not found",
|
|
)
|
|
return None
|