79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
from typing import Dict, Optional
|
|
|
|
from fastapi import HTTPException, status
|
|
from uuid6 import UUID
|
|
|
|
from app.core.exceptions import NotFoundException
|
|
from app.models.organization_model import OrganizationModel
|
|
from app.repositories.organization_repository import OrganizationRepository
|
|
from app.schemas.user_schema import UserSchema
|
|
|
|
from . import BaseService
|
|
|
|
|
|
class OrganizationService(BaseService[OrganizationModel, OrganizationRepository]):
|
|
def __init__(self, repository: OrganizationRepository):
|
|
super().__init__(OrganizationModel, repository)
|
|
|
|
async def get_organizations_by_id(self, user: UserSchema, id: UUID) -> Dict[str, str]:
|
|
try:
|
|
organization = await self.repository.find_by_id(user, id)
|
|
if organization is None:
|
|
raise NotFoundException(f"Organization with UUID {id} not found.")
|
|
|
|
return organization
|
|
except HTTPException as e:
|
|
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
async def find_by_name(self, name: str, sensitive: bool = False) -> Optional[OrganizationModel]:
|
|
try:
|
|
return await self.repository.find_by_name(name, sensitive)
|
|
except HTTPException as e:
|
|
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
async def find_all(self, user: UserSchema | None, filters, sort, search="", group_by=None, limit=100, offset=0, landing=False):
|
|
if group_by:
|
|
self._validate_column(group_by)
|
|
|
|
list_model_filters = self._build_filters(filters or [])
|
|
list_sort = self._build_sort(sort or [])
|
|
|
|
return await self.repository.find_all(
|
|
user,
|
|
filters=list_model_filters,
|
|
sort=list_sort,
|
|
search=search,
|
|
group_by=group_by,
|
|
limit=limit,
|
|
offset=offset,
|
|
landing=landing,
|
|
)
|
|
|
|
async def create(self, data: Dict[str, str]) -> OrganizationModel:
|
|
# if await self.find_by_name(data["name"], True):
|
|
# raise HTTPException(
|
|
# status_code=status.HTTP_400_BAD_REQUEST, detail="Organization with this name already exists."
|
|
# )
|
|
|
|
return await super().create(data)
|
|
|
|
async def update(self, id: UUID, data: Dict[str, str]) -> OrganizationModel:
|
|
# Pass None as user to get public view of organization
|
|
organization = await self.repository.find_by_id(None, id)
|
|
if not organization:
|
|
raise NotFoundException(f"Organization with UUID {id} not found.")
|
|
|
|
if "name" in data and await self.find_by_name(data["name"]):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST, detail="Organization with this name already exists."
|
|
)
|
|
|
|
return await self.repository.update(id, data)
|
|
|
|
async def delete(self, id: UUID) -> None:
|
|
organization = await self.repository.find_by_id(None, id)
|
|
if not organization:
|
|
raise NotFoundException(f"Organization with UUID {id} not found.")
|
|
|
|
return await self.repository.delete(id)
|