73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
from typing import Optional, List, Tuple
|
|
from sqlalchemy.orm import Session, joinedload, selectinload
|
|
from sqlalchemy import or_
|
|
from app.models.alumni import Alumni
|
|
from app.repositories.base import BaseRepository
|
|
|
|
class AlumniRepository(BaseRepository[Alumni]):
|
|
"""
|
|
Alumni repository containing operations specific to the Alumni entity.
|
|
"""
|
|
def get_by_nim(self, db: Session, nim: str) -> Optional[Alumni]:
|
|
"""Find an alumni record by their NIM (student identification number)."""
|
|
return db.query(self.model).filter(self.model.nim == nim).first()
|
|
|
|
def get_with_relations(self, db: Session, alumni_id: int) -> Optional[Alumni]:
|
|
"""Fetch alumni with all related details (pekerjaan, kompetensi, tracer, clustering)."""
|
|
return (
|
|
db.query(self.model)
|
|
.options(
|
|
joinedload(self.model.pekerjaan_detail),
|
|
joinedload(self.model.kompetensi),
|
|
joinedload(self.model.tracer_response),
|
|
selectinload(self.model.clustering_results),
|
|
)
|
|
.filter(self.model.id == alumni_id)
|
|
.first()
|
|
)
|
|
|
|
def get_filtered(
|
|
self,
|
|
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 list with multi-column search, filtering, and total count.
|
|
"""
|
|
query = db.query(self.model)
|
|
|
|
if search:
|
|
search_pattern = f"%{search.strip()}%"
|
|
query = query.filter(
|
|
or_(
|
|
self.model.nim.ilike(search_pattern),
|
|
self.model.nama.ilike(search_pattern),
|
|
self.model.pekerjaan.ilike(search_pattern),
|
|
self.model.instansi.ilike(search_pattern),
|
|
self.model.email.ilike(search_pattern),
|
|
)
|
|
)
|
|
|
|
if kode_prodi:
|
|
query = query.filter(self.model.kode_prodi == kode_prodi.strip())
|
|
|
|
if tahun_lulus:
|
|
query = query.filter(self.model.tahun_lulus == tahun_lulus)
|
|
|
|
if status_kerja:
|
|
query = query.filter(self.model.status_kerja == status_kerja.strip())
|
|
|
|
total = query.count()
|
|
alumni_list = query.order_by(self.model.id.desc()).offset(skip).limit(limit).all()
|
|
|
|
return alumni_list, total
|
|
|
|
# Instantiate alumni repository to be imported elsewhere
|
|
alumni_repository = AlumniRepository(Alumni)
|
|
|