feat: add skill main categories and enhance skills catalog functionality
- Introduced endpoints for managing skill main categories, including CRUD operations. - Enhanced skills catalog endpoint to support hierarchical sorting by main category and category. - Updated frontend API utility functions to include new skill main category operations. - Improved admin interface for skills management with new layout and styles. - Documented changes in the changelog for better tracking of new features and updates.
This commit is contained in:
1 parent
f1ee1eec7e
commit
e8b7e62832
7 files changed
+2231
-806
No files matched your search
+137
-35
@@ -5,7 +5,9 @@ Handles CRUD operations for skills and training methods.
|
||||
Read access for all authenticated users.
|
||||
Write access for admins only.
|
||||
"""
|
||||
from typing import Optional
|
||||
import json
|
||||
from typing import Any, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
@@ -14,6 +16,17 @@ from auth import require_auth
|
||||
router = APIRouter(prefix="/api", tags=["skills"])
|
||||
|
||||
|
||||
def _main_id_for_category(cur, category_id: Optional[int]) -> Optional[int]:
|
||||
if not category_id:
|
||||
return None
|
||||
cur.execute(
|
||||
"SELECT main_category_id FROM skill_categories WHERE id = %s",
|
||||
(category_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return row["main_category_id"] if row else None
|
||||
|
||||
|
||||
# ── List Skills ───────────────────────────────────────────────────────
|
||||
@router.get("/skills")
|
||||
def list_skills(
|
||||
@@ -56,6 +69,57 @@ def list_skills(
|
||||
return [r2d(r) for r in rows]
|
||||
|
||||
|
||||
# ── Katalog-Ansicht (Hierarchie-Sortierung, Admin) ───────────────────────
|
||||
@router.get("/skills/catalog")
|
||||
def list_skills_catalog(
|
||||
status: Optional[str] = Query(
|
||||
default="active",
|
||||
description="'active', 'inactive' oder 'all' (nur Admin)",
|
||||
),
|
||||
session=Depends(require_auth),
|
||||
):
|
||||
"""
|
||||
Fähigkeiten für Admin-Katalog: sortiert nach Hauptgruppe → Kategorie → Sortierung → Name.
|
||||
status=all nur für admin/superadmin.
|
||||
"""
|
||||
role = session.get("role")
|
||||
admin = role in ("admin", "superadmin")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
q = """
|
||||
SELECT s.*,
|
||||
mc.name AS catalog_main_category_name,
|
||||
mc.sort_order AS catalog_main_sort,
|
||||
mc.slug AS catalog_main_slug,
|
||||
sc.name AS catalog_category_name,
|
||||
sc.sort_order AS catalog_category_sort,
|
||||
sc.slug AS catalog_category_slug
|
||||
FROM skills s
|
||||
LEFT JOIN skill_main_categories mc ON s.main_category_id = mc.id
|
||||
LEFT JOIN skill_categories sc ON s.category_id = sc.id
|
||||
WHERE 1=1
|
||||
"""
|
||||
params: list[Any] = []
|
||||
if status == "all":
|
||||
if not admin:
|
||||
raise HTTPException(403, "Nur Admins dürfen alle Status sehen")
|
||||
elif status:
|
||||
q += " AND s.status = %s"
|
||||
params.append(status)
|
||||
else:
|
||||
q += " AND s.status = %s"
|
||||
params.append("active")
|
||||
q += """
|
||||
ORDER BY mc.sort_order NULLS LAST,
|
||||
sc.sort_order NULLS LAST,
|
||||
s.sort_order NULLS LAST,
|
||||
s.name
|
||||
"""
|
||||
cur.execute(q, params)
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
|
||||
# ── Get Skill ─────────────────────────────────────────────────────────
|
||||
@router.get("/skills/{skill_id}")
|
||||
def get_skill(skill_id: int, session=Depends(require_auth)):
|
||||
@@ -84,24 +148,46 @@ def create_skill(data: dict, session=Depends(require_auth)):
|
||||
if not name:
|
||||
raise HTTPException(400, "Name ist Pflichtfeld")
|
||||
|
||||
cat_id = data.get("category_id")
|
||||
if cat_id is not None:
|
||||
cat_id = int(cat_id)
|
||||
main_id = data.get("main_category_id")
|
||||
if main_id is not None:
|
||||
main_id = int(main_id)
|
||||
|
||||
focus = data.get("focus_areas")
|
||||
if isinstance(focus, (list, dict)):
|
||||
focus = json.dumps(focus)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
if cat_id and main_id is None:
|
||||
main_id = _main_id_for_category(cur, cat_id)
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO skills (name, category, description, importance, keywords, status)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO skills (
|
||||
name, category, description, importance, keywords, status,
|
||||
category_id, main_category_id, focus_areas, sort_order
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s::jsonb, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
name,
|
||||
data.get('category'),
|
||||
data.get('description'),
|
||||
data.get('importance'),
|
||||
data.get('keywords'),
|
||||
data.get('status', 'active')
|
||||
))
|
||||
""",
|
||||
(
|
||||
name,
|
||||
data.get("category"),
|
||||
data.get("description"),
|
||||
data.get("importance"),
|
||||
data.get("keywords"),
|
||||
data.get("status", "active"),
|
||||
cat_id,
|
||||
main_id,
|
||||
focus if focus is not None else "[]",
|
||||
data.get("sort_order"),
|
||||
),
|
||||
)
|
||||
|
||||
skill_id = cur.fetchone()['id']
|
||||
conn.commit()
|
||||
skill_id = cur.fetchone()["id"]
|
||||
|
||||
return get_skill(skill_id, session)
|
||||
|
||||
@@ -122,28 +208,44 @@ def update_skill(skill_id: int, data: dict, session=Depends(require_auth)):
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(404, "Fähigkeit nicht gefunden")
|
||||
|
||||
# Update
|
||||
cur.execute("""
|
||||
UPDATE skills SET
|
||||
name = %s,
|
||||
category = %s,
|
||||
description = %s,
|
||||
importance = %s,
|
||||
keywords = %s,
|
||||
status = %s,
|
||||
updated_at = NOW()
|
||||
WHERE id = %s
|
||||
""", (
|
||||
data.get('name'),
|
||||
data.get('category'),
|
||||
data.get('description'),
|
||||
data.get('importance'),
|
||||
data.get('keywords'),
|
||||
data.get('status'),
|
||||
skill_id
|
||||
))
|
||||
sets: list = []
|
||||
vals: list = []
|
||||
for key in (
|
||||
"name",
|
||||
"category",
|
||||
"description",
|
||||
"importance",
|
||||
"keywords",
|
||||
"status",
|
||||
"category_id",
|
||||
"main_category_id",
|
||||
"sort_order",
|
||||
):
|
||||
if key in data:
|
||||
sets.append(f"{key} = %s")
|
||||
vals.append(data[key])
|
||||
if "focus_areas" in data:
|
||||
fa = data["focus_areas"]
|
||||
if isinstance(fa, (list, dict)):
|
||||
fa = json.dumps(fa)
|
||||
sets.append("focus_areas = %s::jsonb")
|
||||
vals.append(fa if fa is not None else "[]")
|
||||
|
||||
conn.commit()
|
||||
if sets:
|
||||
sets.append("updated_at = NOW()")
|
||||
vals.append(skill_id)
|
||||
cur.execute(
|
||||
f"UPDATE skills SET {', '.join(sets)} WHERE id = %s",
|
||||
tuple(vals),
|
||||
)
|
||||
|
||||
if "category_id" in data and "main_category_id" not in data:
|
||||
mid = _main_id_for_category(cur, data.get("category_id"))
|
||||
if mid is not None:
|
||||
cur.execute(
|
||||
"UPDATE skills SET main_category_id = %s WHERE id = %s",
|
||||
(mid, skill_id),
|
||||
)
|
||||
|
||||
return get_skill(skill_id, session)
|
||||
|
||||
|
||||
Reference in new issue
Block a user