feat: add new folder

This commit is contained in:
Daniel
2026-03-30 20:49:40 +08:00
commit c7788fdd92
64 changed files with 19910 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
from sqlalchemy import select
from sqlalchemy.orm import Session, selectinload
from app.domain.models import Job, JobSkill
from app.domain.schemas import JobCard
class JobRepository:
def __init__(self, db: Session):
self.db = db
def upsert(self, job_card: JobCard) -> Job:
instance = self.db.get(Job, job_card.job_id)
if instance is None:
instance = Job(id=job_card.job_id)
self.db.add(instance)
instance.title = job_card.title
instance.category = job_card.category
instance.description = job_card.description
instance.city = job_card.city
instance.region = job_card.region
instance.location_detail = job_card.location_detail
instance.start_time = job_card.start_time
instance.duration_hours = job_card.duration_hours
instance.headcount = job_card.headcount
instance.salary_type = job_card.salary.type.value
instance.salary_amount = job_card.salary.amount
instance.salary_currency = job_card.salary.currency
instance.work_mode = job_card.work_mode
instance.tags_json = job_card.tags
instance.confidence = job_card.confidence
instance.skills.clear()
instance.skills.extend(
[
JobSkill(skill_name=skill_name, weight=1.0, is_required=index < 2)
for index, skill_name in enumerate(job_card.skills)
]
)
self.db.commit()
self.db.refresh(instance)
return instance
def list(self, limit: int = 100) -> list[Job]:
stmt = select(Job).options(selectinload(Job.skills)).order_by(Job.created_at.desc()).limit(limit)
return list(self.db.scalars(stmt))
def get(self, job_id: str) -> Job | None:
stmt = select(Job).options(selectinload(Job.skills)).where(Job.id == job_id)
return self.db.scalar(stmt)
def get_many(self, ids: list[str]) -> list[Job]:
if not ids:
return []
stmt = select(Job).options(selectinload(Job.skills)).where(Job.id.in_(ids))
result = list(self.db.scalars(stmt))
order = {item_id: index for index, item_id in enumerate(ids)}
return sorted(result, key=lambda item: order[item.id])

View File

@@ -0,0 +1,49 @@
from sqlalchemy import select
from sqlalchemy.orm import Session
from app.domain.models import MatchRecord
from app.domain.schemas import MatchResult
class MatchRepository:
def __init__(self, db: Session):
self.db = db
def create(self, match: MatchResult) -> MatchRecord:
instance = MatchRecord(
id=match.match_id,
source_type=match.source_type.value,
source_id=match.source_id,
target_id=match.target_id,
match_score=match.match_score,
breakdown_json=match.breakdown.model_dump(),
reasons_json=match.reasons,
)
self.db.add(instance)
self.db.commit()
self.db.refresh(instance)
return instance
def bulk_replace(self, matches: list[MatchResult], source_type: str, source_id: str) -> None:
stmt = select(MatchRecord).where(
MatchRecord.source_type == source_type,
MatchRecord.source_id == source_id,
)
for item in self.db.scalars(stmt):
self.db.delete(item)
for match in matches:
self.db.add(
MatchRecord(
id=match.match_id,
source_type=match.source_type.value,
source_id=match.source_id,
target_id=match.target_id,
match_score=match.match_score,
breakdown_json=match.breakdown.model_dump(),
reasons_json=match.reasons,
)
)
self.db.commit()
def get(self, match_id: str) -> MatchRecord | None:
return self.db.get(MatchRecord, match_id)

View File

@@ -0,0 +1,49 @@
from sqlalchemy import select
from sqlalchemy.orm import Session, selectinload
from app.domain.models import Worker, WorkerSkill
from app.domain.schemas import WorkerCard
class WorkerRepository:
def __init__(self, db: Session):
self.db = db
def upsert(self, worker_card: WorkerCard) -> Worker:
instance = self.db.get(Worker, worker_card.worker_id)
if instance is None:
instance = Worker(id=worker_card.worker_id)
self.db.add(instance)
instance.name = worker_card.name
instance.description = worker_card.description
instance.cities_json = worker_card.cities
instance.regions_json = worker_card.regions
instance.availability_json = worker_card.availability
instance.experience_tags_json = worker_card.experience_tags
instance.reliability_score = worker_card.reliability_score
instance.profile_completion = worker_card.profile_completion
instance.confidence = worker_card.confidence
instance.skills.clear()
instance.skills.extend(
[WorkerSkill(skill_name=skill.name, score=skill.score) for skill in worker_card.skills]
)
self.db.commit()
self.db.refresh(instance)
return instance
def list(self, limit: int = 200) -> list[Worker]:
stmt = select(Worker).options(selectinload(Worker.skills)).order_by(Worker.created_at.desc()).limit(limit)
return list(self.db.scalars(stmt))
def get(self, worker_id: str) -> Worker | None:
stmt = select(Worker).options(selectinload(Worker.skills)).where(Worker.id == worker_id)
return self.db.scalar(stmt)
def get_many(self, ids: list[str]) -> list[Worker]:
if not ids:
return []
stmt = select(Worker).options(selectinload(Worker.skills)).where(Worker.id.in_(ids))
result = list(self.db.scalars(stmt))
order = {item_id: index for index, item_id in enumerate(ids)}
return sorted(result, key=lambda item: order[item.id])