it-planet-springboard/api/app/application/vacancies_repository.py

41 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload
from app.domain.models import Vacancy, LocationCoordinate, CompanyProfile
class VacanciesRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def create_location(self, lat: float, lon: float) -> LocationCoordinate:
location = LocationCoordinate(latitude=lat, longitude=lon)
self.db.add(location)
await self.db.flush()
return location
async def create_vacancy(self, vacancy: Vacancy) -> Vacancy:
self.db.add(vacancy)
await self.db.commit()
# Делаем глубокую загрузку всех связей для ответа API
query = (
select(Vacancy)
.filter_by(id=vacancy.id)
.options(
# Подгружаем простые связи вакансии
joinedload(Vacancy.work_format),
joinedload(Vacancy.employment_type),
joinedload(Vacancy.experience_level),
joinedload(Vacancy.location),
# Подгружаем профиль компании И его вложенные связи (лого, фото, индустрия, соцсети)
joinedload(Vacancy.company_profile).options(
joinedload(CompanyProfile.logo),
joinedload(CompanyProfile.official_photo),
joinedload(CompanyProfile.industry),
joinedload(CompanyProfile.socials)
)
)
)
result = await self.db.execute(query)
return result.scalars().first()