84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
from sqlalchemy.orm import Session
|
|
from fastapi import HTTPException, status
|
|
from app.repositories.alumni import alumni_repository
|
|
from app.schemas.alumni import AlumniCreate, AlumniUpdate
|
|
from app.models.alumni import Alumni
|
|
from typing import List, Optional, Tuple
|
|
|
|
class AlumniService:
|
|
"""
|
|
Service containing business logic for Alumni tracking features.
|
|
"""
|
|
@staticmethod
|
|
def get_by_id(db: Session, alumni_id: int) -> Alumni:
|
|
"""Fetch alumni by ID or raise HTTP 404 if not found."""
|
|
alumni = alumni_repository.get(db, alumni_id)
|
|
if not alumni:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Data alumni dengan ID {alumni_id} tidak ditemukan"
|
|
)
|
|
return alumni
|
|
|
|
@staticmethod
|
|
def get_by_id_detailed(db: Session, alumni_id: int) -> Alumni:
|
|
"""Fetch alumni by ID with all relations (pekerjaan, kompetensi, tracer_responses, clustering)."""
|
|
alumni = alumni_repository.get_with_relations(db, alumni_id)
|
|
if not alumni:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Data alumni dengan ID {alumni_id} tidak ditemukan"
|
|
)
|
|
return alumni
|
|
|
|
@staticmethod
|
|
def get_all(db: Session, skip: int = 0, limit: int = 100) -> List[Alumni]:
|
|
"""Fetch all alumni with pagination limits."""
|
|
return alumni_repository.get_all(db, skip=skip, limit=limit)
|
|
|
|
@staticmethod
|
|
def get_filtered(
|
|
db: Session,
|
|
skip: int = 0,
|
|
limit: int = 10,
|
|
search: Optional[str] = None,
|
|
kode_prodi: Optional[str] = None,
|
|
tahun_lulus: Optional[int] = None,
|
|
status_kerja: Optional[str] = None,
|
|
) -> Tuple[List[Alumni], int]:
|
|
"""Fetch alumni with multi-criteria filtering and count."""
|
|
return alumni_repository.get_filtered(
|
|
db=db,
|
|
skip=skip,
|
|
limit=limit,
|
|
search=search,
|
|
kode_prodi=kode_prodi,
|
|
tahun_lulus=tahun_lulus,
|
|
status_kerja=status_kerja,
|
|
)
|
|
|
|
@staticmethod
|
|
def create(db: Session, alumni_in: AlumniCreate) -> Alumni:
|
|
"""Create a new alumni record after verifying NIM uniqueness."""
|
|
existing_nim = alumni_repository.get_by_nim(db, alumni_in.nim)
|
|
if existing_nim:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Alumni dengan NIM {alumni_in.nim} sudah terdaftar"
|
|
)
|
|
return alumni_repository.create(db, alumni_in)
|
|
|
|
@staticmethod
|
|
def update(db: Session, alumni_id: int, alumni_in: AlumniUpdate) -> Alumni:
|
|
"""Update an existing alumni record or raise HTTP 404 if not found."""
|
|
alumni = AlumniService.get_by_id(db, alumni_id)
|
|
return alumni_repository.update(db, alumni, alumni_in)
|
|
|
|
@staticmethod
|
|
def delete(db: Session, alumni_id: int) -> Alumni:
|
|
"""Delete an alumni record by ID or raise HTTP 404 if not found."""
|
|
alumni = AlumniService.get_by_id(db, alumni_id)
|
|
deleted = alumni_repository.delete(db, alumni_id)
|
|
return deleted if deleted is not None else alumni
|
|
|