it-planet-springboard/api/app/infrastructure/company_profile_socials_service.py

80 lines
3.3 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 typing import List
from fastapi import HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.application.company_profile_socials_repository import CompanyProfileSocialsRepository
from app.application.company_profiles_repository import CompanyProfilesRepository
from app.core.constants import UserRoles
from app.domain.entities.company_profile_socials import CompanyProfileSocialRead, CompanyProfileSocialCreate
from app.domain.models import User, CompanyProfileSocial
class CompanyProfileSocialsService:
def __init__(self, db: AsyncSession):
self.company_profile_socials_repository = CompanyProfileSocialsRepository(db)
self.company_profiles_repository = CompanyProfilesRepository(db)
async def get_by_company_profile_id(
self,
company_profile_id: int,
) -> List[CompanyProfileSocialRead]:
company_profile = await self.company_profiles_repository.get_by_id(company_profile_id)
if not company_profile:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Компания с таким ID не найдена'
)
company_profile_socials = await self.company_profile_socials_repository.get_by_company_profile_id(
company_profile_id
)
response = []
for company_profile_social in company_profile_socials:
response.append(CompanyProfileSocialRead.model_validate(company_profile_social))
return response
async def replace_company_profile_socials(
self,
company_profile_socials: List[CompanyProfileSocialCreate],
company_profile_id: int,
current_user: User,
) -> List[CompanyProfileSocialRead]:
company_profile = await self.company_profiles_repository.get_by_id(company_profile_id)
if not company_profile:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Соискатель с таким ID не найден')
if company_profile.creator_user_id != current_user.id and not (
current_user.role.title == UserRoles.MODERATOR and current_user.is_admin
):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail='Доступ запрещен',
)
old_company_profile_socials = await self.company_profile_socials_repository.get_by_company_profile_id(
company_profile_id
)
await self.company_profile_socials_repository.delete_list(old_company_profile_socials)
company_profile_social_models = []
for company_profile_social in company_profile_socials:
company_profile_social_models.append(CompanyProfileSocial(
title=company_profile_social.title,
link=company_profile_social.link,
company_id=company_profile_id,
))
company_profile_social_models = await self.company_profile_socials_repository.create_list(
company_profile_social_models
)
response = []
for company_profile_social_model in company_profile_social_models:
response.append(CompanyProfileSocialRead.model_validate(company_profile_social_model))
return response