fix: address code review issues and improve code quality

- Add owner_id check in repository delete operation
- Fix time range validation for partial updates
- Wrap pre-query in try/except for consistent error handling
- Use default_factory instead of mutable defaults
- Add max_length constraint for timezone field
- Remove unused dependencies and empty validators
- Extract magic numbers to constants
- Simplify update logic with model_dump
This commit is contained in:
qzl
2026-02-28 11:29:06 +08:00
parent 50b38de488
commit 9b48939de8
4 changed files with 53 additions and 48 deletions
+12 -2
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
from datetime import datetime
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Protocol
from uuid import UUID
@@ -95,7 +95,17 @@ class SQLAlchemyScheduleItemRepository(BaseRepository[ScheduleItem]):
self, item_id: UUID, owner_id: UUID
) -> ScheduleItem | None:
try:
return await self.soft_delete_by_id(item_id)
stmt = (
update(ScheduleItem)
.where(ScheduleItem.id == item_id)
.where(ScheduleItem.owner_id == owner_id)
.where(ScheduleItem.deleted_at.is_(None))
.values(deleted_at=datetime.now(timezone.utc))
.returning(ScheduleItem)
)
result = await self._session.execute(stmt)
await self._session.flush()
return result.scalar_one_or_none()
except SQLAlchemyError:
logger.exception("Schedule item delete failed", item_id=str(item_id))
raise