Files
social-app/backend/src/v1/schedule_items/service.py
T

198 lines
7.1 KiB
Python
Raw Normal View History

2026-02-28 11:03:29 +08:00
from __future__ import annotations
from typing import TYPE_CHECKING
from uuid import UUID
from fastapi import HTTPException
from sqlalchemy.exc import SQLAlchemyError
from core.auth.models import CurrentUser
from core.db.base_service import BaseService
from core.logging import get_logger
from models.schedule_items import ScheduleItem
from v1.schedule_items.repository import ScheduleItemRepository
from v1.schedule_items.schemas import (
ScheduleItemCreateRequest,
ScheduleItemListRequest,
ScheduleItemMetadata,
ScheduleItemResponse,
ScheduleItemUpdateRequest,
ScheduleItemSourceType,
ScheduleItemStatus,
)
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession
logger = get_logger("v1.schedule_items.service")
class ScheduleItemService(BaseService):
_repository: ScheduleItemRepository
_session: AsyncSession
def __init__(
self,
repository: ScheduleItemRepository,
session: AsyncSession,
current_user: CurrentUser | None,
) -> None:
super().__init__(current_user=current_user)
self._repository = repository
self._session = session
async def create(self, request: ScheduleItemCreateRequest) -> ScheduleItemResponse:
user_id = self.require_user_id()
if request.end_at and request.end_at <= request.start_at:
raise HTTPException(status_code=400, detail="end_at must be after start_at")
data = {
"owner_id": user_id,
"title": request.title,
"description": request.description,
"start_at": request.start_at,
"end_at": request.end_at,
"timezone": request.timezone,
"metadata": request.metadata.model_dump() if request.metadata else {},
"source_type": ScheduleItemSourceType.MANUAL,
"status": ScheduleItemStatus.ACTIVE,
"created_by": user_id,
}
try:
item = await self._repository.create(data)
await self._session.commit()
except SQLAlchemyError:
await self._session.rollback()
logger.exception("Failed to create schedule item")
raise HTTPException(
status_code=503, detail="Schedule item store unavailable"
)
return self._to_response(item)
async def get_by_id(self, item_id: UUID) -> ScheduleItemResponse:
user_id = self.require_user_id()
try:
item = await self._repository.get_by_item_id(item_id, user_id)
except SQLAlchemyError:
logger.exception("Failed to get schedule item", item_id=str(item_id))
raise HTTPException(
status_code=503, detail="Schedule item store unavailable"
)
if item is None:
raise HTTPException(status_code=404, detail="Schedule item not found")
return self._to_response(item)
async def update(
self, item_id: UUID, request: ScheduleItemUpdateRequest
) -> ScheduleItemResponse:
user_id = self.require_user_id()
try:
existing = await self._repository.get_by_item_id(item_id, user_id)
if existing is None:
raise HTTPException(status_code=404, detail="Schedule item not found")
update_data: dict = {}
if request.title is not None:
update_data["title"] = request.title
if request.description is not None:
update_data["description"] = request.description
if request.start_at is not None:
update_data["start_at"] = request.start_at
if request.end_at is not None:
update_data["end_at"] = request.end_at
if request.timezone is not None:
update_data["timezone"] = request.timezone
if request.status is not None:
update_data["status"] = request.status
if request.metadata is not None:
update_data["metadata"] = request.metadata.model_dump()
next_start = (
request.start_at if request.start_at is not None else existing.start_at
)
next_end = request.end_at if request.end_at is not None else existing.end_at
if next_end is not None and next_end <= next_start:
raise HTTPException(
status_code=400, detail="end_at must be after start_at"
)
2026-02-28 11:03:29 +08:00
if not update_data:
return self._to_response(existing)
2026-02-28 11:03:29 +08:00
item = await self._repository.update_by_item_id(
item_id, user_id, update_data
)
await self._session.commit()
except SQLAlchemyError:
await self._session.rollback()
logger.exception("Failed to update schedule item", item_id=str(item_id))
raise HTTPException(
status_code=503, detail="Schedule item store unavailable"
)
if item is None:
raise HTTPException(status_code=404, detail="Schedule item not found")
return self._to_response(item)
async def delete(self, item_id: UUID) -> None:
user_id = self.require_user_id()
try:
existing = await self._repository.get_by_item_id(item_id, user_id)
if existing is None:
raise HTTPException(status_code=404, detail="Schedule item not found")
2026-02-28 11:03:29 +08:00
await self._repository.delete_by_item_id(item_id, user_id)
await self._session.commit()
except SQLAlchemyError:
await self._session.rollback()
logger.exception("Failed to delete schedule item", item_id=str(item_id))
raise HTTPException(
status_code=503, detail="Schedule item store unavailable"
)
async def list_by_date_range(
self, request: ScheduleItemListRequest
) -> list[ScheduleItemResponse]:
user_id = self.require_user_id()
if request.end_at <= request.start_at:
raise HTTPException(status_code=400, detail="end_at must be after start_at")
try:
items = await self._repository.list_by_date_range(
user_id, request.start_at, request.end_at
)
except SQLAlchemyError:
logger.exception("Failed to list schedule items")
raise HTTPException(
status_code=503, detail="Schedule item store unavailable"
)
return [self._to_response(item) for item in items]
def _to_response(self, item: ScheduleItem) -> ScheduleItemResponse:
return ScheduleItemResponse(
id=item.id,
title=item.title,
description=item.description,
start_at=item.start_at,
end_at=item.end_at,
timezone=item.timezone,
metadata=ScheduleItemMetadata.model_validate(item.extra_metadata)
if item.extra_metadata
else None,
status=ScheduleItemStatus(item.status.value),
source_type=ScheduleItemSourceType(item.source_type.value),
created_at=item.created_at,
updated_at=item.updated_at,
)