skills.py 1.1 KB
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.schema.common import ApiResponse
from app.schema.skill import SkillListResponse, SkillDetailResponse
from app.service.skill_service import SkillService
from app.infrastructure.database import get_db
from app.infrastructure.cache import InMemorySkillCache, SkillCache

router = APIRouter(prefix="/api/v1/skills")

_default_cache = InMemorySkillCache()


def get_cache() -> SkillCache:
    return _default_cache


@router.get("", response_model=ApiResponse[SkillListResponse])
def list_skills(db: Session = Depends(get_db), cache: SkillCache = Depends(get_cache)):
    svc = SkillService(db, cache)
    items = svc.list_skills()
    return SkillListResponse(list=items)


@router.get("/{skill_code}", response_model=ApiResponse[SkillDetailResponse])
def get_skill_detail(skill_code: str, db: Session = Depends(get_db), cache: SkillCache = Depends(get_cache)):
    svc = SkillService(db, cache)
    try:
        return svc.get_detail(skill_code)
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))