183 lines
5.6 KiB
Python

import logging
from typing import List
from bson import ObjectId
from src.db.repositories.team_repository import team_repository
from src.schemas.team import TeamCreate, TeamUpdate, TeamResponse, TeamListResponse
from src.models.team import TeamModel
logger = logging.getLogger(__name__)
class TeamService:
"""Service class for handling team-related business logic"""
async def create_team(self, team_data: TeamCreate) -> TeamResponse:
"""
Create a new team
Args:
team_data: The team creation data
Returns:
TeamResponse: The created team
"""
# Create team
team = TeamModel(
name=team_data.name,
description=team_data.description
)
created_team = await team_repository.create(team)
# Convert to response model
return TeamResponse(
id=str(created_team.id),
name=created_team.name,
description=created_team.description,
created_at=created_team.created_at,
updated_at=created_team.updated_at
)
async def list_teams(self, skip: int = 0, limit: int = 50) -> TeamListResponse:
"""
List all teams with pagination
Args:
skip: Number of records to skip for pagination (default: 0)
limit: Maximum number of records to return (default: 50)
Returns:
TeamListResponse: Paginated list of teams
"""
# Get teams with pagination
teams = await team_repository.get_all(skip=skip, limit=limit)
# Get total count for pagination
total_count = await team_repository.count()
# Convert to response models
response_teams = []
for team in teams:
response_teams.append(TeamResponse(
id=str(team.id),
name=team.name,
description=team.description,
created_at=team.created_at,
updated_at=team.updated_at
))
return TeamListResponse(teams=response_teams, total=total_count)
async def get_team(self, team_id: str) -> TeamResponse:
"""
Get a team by ID
Args:
team_id: The team ID to retrieve
Returns:
TeamResponse: The team data
Raises:
ValueError: If team_id is invalid
RuntimeError: If team not found
"""
try:
obj_id = ObjectId(team_id)
except Exception:
raise ValueError("Invalid team ID")
# Get the team
team = await team_repository.get_by_id(obj_id)
if not team:
raise RuntimeError("Team not found")
# Convert to response model
return TeamResponse(
id=str(team.id),
name=team.name,
description=team.description,
created_at=team.created_at,
updated_at=team.updated_at
)
async def update_team(self, team_id: str, team_data: TeamUpdate) -> TeamResponse:
"""
Update a team
Args:
team_id: The team ID to update
team_data: The update data
Returns:
TeamResponse: The updated team
Raises:
ValueError: If team_id is invalid
RuntimeError: If team not found or update fails
"""
try:
obj_id = ObjectId(team_id)
except Exception:
raise ValueError("Invalid team ID")
# Get the team
team = await team_repository.get_by_id(obj_id)
if not team:
raise RuntimeError("Team not found")
# Update the team
update_data = team_data.dict(exclude_unset=True)
if not update_data:
# No fields to update
return TeamResponse(
id=str(team.id),
name=team.name,
description=team.description,
created_at=team.created_at,
updated_at=team.updated_at
)
updated_team = await team_repository.update(obj_id, update_data)
if not updated_team:
raise RuntimeError("Failed to update team")
# Convert to response model
return TeamResponse(
id=str(updated_team.id),
name=updated_team.name,
description=updated_team.description,
created_at=updated_team.created_at,
updated_at=updated_team.updated_at
)
async def delete_team(self, team_id: str) -> bool:
"""
Delete a team
Args:
team_id: The team ID to delete
Returns:
bool: True if successfully deleted
Raises:
ValueError: If team_id is invalid
RuntimeError: If team not found or deletion fails
"""
try:
obj_id = ObjectId(team_id)
except Exception:
raise ValueError("Invalid team ID")
# Get the team
team = await team_repository.get_by_id(obj_id)
if not team:
raise RuntimeError("Team not found")
# Delete the team
success = await team_repository.delete(obj_id)
if not success:
raise RuntimeError("Failed to delete team")
return True