114 lines
3.8 KiB
Python
114 lines
3.8 KiB
Python
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/admin_services.py
|
|
"""
|
|
Admin Service Catalog CRUD végpontok.
|
|
Prefix: /admin/services
|
|
Védve: Depends(get_current_admin)
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, func
|
|
from typing import List
|
|
|
|
from app.api import deps
|
|
from app.models.identity import User
|
|
from app.models.core_logic import ServiceCatalog
|
|
from app.schemas.service_catalog import (
|
|
ServiceCatalogCreate,
|
|
ServiceCatalogUpdate,
|
|
ServiceCatalogResponse,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[ServiceCatalogResponse], tags=["Admin Service Catalog"])
|
|
async def list_service_catalog(
|
|
skip: int = Query(0, ge=0, description="Hány rekordot hagyjunk ki (lapozás)"),
|
|
limit: int = Query(50, ge=1, le=200, description="Maximum visszaadott rekordok száma"),
|
|
is_active: bool | None = Query(None, description="Szűrés aktív/inaktív státuszra"),
|
|
db: AsyncSession = Depends(deps.get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
_ = Depends(deps.RequirePermission("services:manage")),
|
|
):
|
|
"""
|
|
Szolgáltatás katalógus bejegyzések listázása lapozással.
|
|
"""
|
|
query = select(ServiceCatalog)
|
|
|
|
if is_active is not None:
|
|
query = query.where(ServiceCatalog.is_active == is_active)
|
|
|
|
# Teljes darabszám
|
|
count_query = select(func.count()).select_from(query.subquery())
|
|
total_result = await db.execute(count_query)
|
|
total = total_result.scalar() or 0
|
|
|
|
query = query.order_by(ServiceCatalog.id.desc()).offset(skip).limit(limit)
|
|
result = await db.execute(query)
|
|
items = result.scalars().all()
|
|
|
|
return items
|
|
|
|
|
|
@router.post("", response_model=ServiceCatalogResponse, status_code=status.HTTP_201_CREATED, tags=["Admin Service Catalog"])
|
|
async def create_service_catalog(
|
|
data: ServiceCatalogCreate,
|
|
db: AsyncSession = Depends(deps.get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
_ = Depends(deps.RequirePermission("services:manage")),
|
|
):
|
|
"""
|
|
Új szolgáltatás katalógus bejegyzés létrehozása.
|
|
"""
|
|
# Ellenőrizzük, hogy a service_code már létezik-e
|
|
existing = await db.execute(
|
|
select(ServiceCatalog).where(ServiceCatalog.service_code == data.service_code)
|
|
)
|
|
if existing.scalar_one_or_none():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=f"Service code '{data.service_code}' already exists.",
|
|
)
|
|
|
|
entry = ServiceCatalog(
|
|
service_code=data.service_code,
|
|
name=data.name,
|
|
description=data.description,
|
|
credit_cost=data.credit_cost,
|
|
is_active=data.is_active,
|
|
)
|
|
db.add(entry)
|
|
await db.commit()
|
|
await db.refresh(entry)
|
|
return entry
|
|
|
|
|
|
@router.patch("/{service_id}", response_model=ServiceCatalogResponse, tags=["Admin Service Catalog"])
|
|
async def update_service_catalog(
|
|
service_id: int,
|
|
data: ServiceCatalogUpdate,
|
|
db: AsyncSession = Depends(deps.get_db),
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
_ = Depends(deps.RequirePermission("services:manage")),
|
|
):
|
|
"""
|
|
Meglévő szolgáltatás katalógus bejegyzés módosítása.
|
|
"""
|
|
result = await db.execute(select(ServiceCatalog).where(ServiceCatalog.id == service_id))
|
|
entry = result.scalar_one_or_none()
|
|
|
|
if not entry:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Service catalog entry with ID {service_id} not found.",
|
|
)
|
|
|
|
# Csak a megadott mezőket frissítjük
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(entry, field, value)
|
|
|
|
await db.commit()
|
|
await db.refresh(entry)
|
|
return entry
|