121 lines
3.7 KiB
Python
121 lines
3.7 KiB
Python
from typing import Any, Dict
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user, has_role
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .dto import IdentifierDTO
|
|
from .models import IdentifierCatalog
|
|
|
|
router = APIRouter(prefix="/identifiers")
|
|
|
|
|
|
@router.get("/", response_model=Dict[str, Any])
|
|
async def list_identifiers(
|
|
page: int = Query(1, ge=1, description="Número de página"),
|
|
page_size: int = Query(50, ge=1, le=1000, description="Tamaño de página"),
|
|
level: str = Query(None, description="Filtrar por nivel (G o P)"),
|
|
q: str = Query(None, description="Búsqueda general"),
|
|
search: str = Query(None, description="Término de búsqueda"),
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
skip = (page - 1) * page_size
|
|
query = db.query(IdentifierCatalog)
|
|
|
|
if search:
|
|
search_filter = f"%{search}%"
|
|
query = query.filter(
|
|
or_(
|
|
IdentifierCatalog.key.ilike(search_filter),
|
|
IdentifierCatalog.description.ilike(search_filter),
|
|
IdentifierCatalog.level.ilike(search_filter),
|
|
IdentifierCatalog.complement.ilike(search_filter)
|
|
)
|
|
)
|
|
|
|
if level:
|
|
query = query.filter(IdentifierCatalog.nivel == level)
|
|
|
|
if q:
|
|
search_term = f"%{q}%"
|
|
query = query.filter(
|
|
or_(
|
|
IdentifierCatalog.key.ilike(search_term),
|
|
IdentifierCatalog.description.ilike(search_term),
|
|
IdentifierCatalog.complement.ilike(search_term)
|
|
)
|
|
)
|
|
|
|
items = query.offset(skip).limit(page_size).all()
|
|
total = query.count()
|
|
return {
|
|
"items": [IdentifierDTO.model_validate(obj) for obj in items],
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": page_size,
|
|
}
|
|
|
|
|
|
@router.get("/{key}", response_model=IdentifierDTO)
|
|
async def get_identifier(
|
|
key: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
obj = db.query(IdentifierCatalog).filter(IdentifierCatalog.key == key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
return obj
|
|
|
|
|
|
@router.post("/", response_model=IdentifierDTO, status_code=201)
|
|
async def create_identifier(
|
|
data: IdentifierDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin")),
|
|
):
|
|
# Check if already exists
|
|
existing = db.query(IdentifierCatalog).filter(IdentifierCatalog.key == data.key).first()
|
|
if existing:
|
|
raise HTTPException(status_code=400, detail="Identifier with this key already exists")
|
|
|
|
obj = IdentifierCatalog(**data.dict())
|
|
db.add(obj)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
|
|
@router.put("/{key}", response_model=IdentifierDTO)
|
|
async def update_identifier(
|
|
key: str,
|
|
data: IdentifierDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin")),
|
|
):
|
|
obj = db.query(IdentifierCatalog).filter(IdentifierCatalog.key == key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
for field, value in data.dict().items():
|
|
setattr(obj, field, value)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
|
|
@router.delete("/{key}", status_code=204)
|
|
async def delete_identifier(
|
|
key: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin")),
|
|
):
|
|
obj = db.query(IdentifierCatalog).filter(IdentifierCatalog.key == key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
db.delete(obj)
|
|
db.commit()
|
|
return None
|