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

103 lines
4.5 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 Optional, List
from fastapi import HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload
from app.application.company_profiles_repository import CompanyProfilesRepository
from app.application.industries_repository import IndustriesRepository
from app.core.constants import UserRoles
from app.domain.models import CompanyProfile, CompanyProfileSocial, User
from app.domain.entities.company_profiles import CompanyProfileCreate, CompanyProfileRead, CompanyProfileUpdate
class CompanyProfilesService:
def __init__(self, db: AsyncSession):
self.company_profiles_repository = CompanyProfilesRepository(db)
self.industries_repository = IndustriesRepository(db)
async def get_company_by_creator_id(self, user_id: int) -> Optional[CompanyProfileRead]:
company_profile = await self.company_profiles_repository.get_company_by_creator_id(user_id)
if not company_profile:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="У вас нет созданной компании"
)
return CompanyProfileRead.model_validate(company_profile)
async def create_company(self, data: CompanyProfileCreate, user: User) -> CompanyProfileRead:
existing_profile = await self.company_profiles_repository.get_company_by_creator_id(user.id)
if existing_profile:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="У вас уже создан профиль компании. Используйте обновление."
)
industry = await self.industries_repository.get_by_id(data.industry_id)
if not industry:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Указанная индустрия не найдена"
)
new_company = CompanyProfile(
title=data.title,
description=data.description,
website_url=data.website_url,
inn=data.inn,
corporate_email=str(data.corporate_email) if data.corporate_email else None,
video_url=data.video_url,
logo_id=data.logo_id,
official_photo_id=data.official_photo_id,
industry_id=data.industry_id,
creator_user_id=user.id,
)
created_company = await self.company_profiles_repository.create(new_company)
result = await self.company_profiles_repository.get_by_id(created_company.id)
return CompanyProfileRead.model_validate(result)
async def update_company(self, company_id: int, data: CompanyProfileUpdate, current_user: User) -> CompanyProfileRead:
existing_profile = await self.company_profiles_repository.get_by_id(company_id)
if not existing_profile:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Компания с таким ID не найдена."
)
industry = await self.industries_repository.get_by_id(data.industry_id)
if not industry:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Указанная индустрия не найдена"
)
if existing_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='Доступ запрещен',
)
existing_profile.title = data.title
existing_profile.description = data.description
existing_profile.website_url = data.website_url
existing_profile.inn = data.inn
existing_profile.corporate_email = str(data.corporate_email) if data.corporate_email else None
existing_profile.video_url = data.video_url
existing_profile.logo_id = data.logo_id
existing_profile.official_photo_id = data.official_photo_id
existing_profile.industry_id = data.industry_id
existing_profile.creator_user_id = current_user.id
existing_profile = await self.company_profiles_repository.update(existing_profile)
result = await self.company_profiles_repository.get_by_id(existing_profile.id)
return CompanyProfileRead.model_validate(result)