7c920f38f6
1B — domain layer:
- New entities: Album, Playlist, Like, PlayHistoryEntry
- Track entity extended with album_id, genre, year fields
- New protocols: AlbumRepository, PlaylistRepository, LikeRepository, HistoryRepository
- ArtistRepository / TrackRepository protocols extended (list, count, update, get_many, etc.)
- New repos: SqlAlchemyAlbum/Playlist/Like/HistoryRepository
- Artist and track repos updated to match extended protocols
1H — library API:
- Pagination: PagedResponse[T] generic, offset-based, limit default 50 max 200
- Schemas: TrackOut, AlbumOut, ArtistOut, PlaylistOut/Create/Update,
LikeEvent/State, HistoryIn/Out, LibrarySearchResponse
- GET/PATCH/DELETE /tracks with filters, sort, pagination
- GET /albums, /albums/{id}, /albums/{id}/tracks
- GET /artists, /artists/{id}, /artists/{id}/albums, /artists/{id}/tracks
- GET /search/library (ILIKE across tracks/albums/artists)
- Full /playlists CRUD + track add/remove (append-only version bump)
- POST /likes (append-only event log), GET /likes, GET /likes/state
- POST /history (scrobble), GET /history
- deps.py: TrackRepoDep, ArtistRepoDep, AlbumRepoDep, PlaylistRepoDep,
LikeRepoDep, HistoryRepoDep
ruff ✅ mypy ✅ pytest 45/45 ✅
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
"""Play history repository — adapter over ``AsyncSession``."""
|
|
|
|
import datetime as dt
|
|
import uuid
|
|
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.domain.entities.history import PlayHistoryEntry
|
|
from app.infrastructure.db.models.play_history import PlayHistoryModel
|
|
|
|
|
|
def _to_entity(row: PlayHistoryModel) -> PlayHistoryEntry:
|
|
return PlayHistoryEntry(
|
|
id=row.id,
|
|
user_id=row.user_id,
|
|
track_id=row.track_id,
|
|
played_at=row.played_at,
|
|
play_duration_seconds=row.play_duration_seconds,
|
|
completed=row.completed,
|
|
)
|
|
|
|
|
|
class SqlAlchemyHistoryRepository:
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self._session = session
|
|
|
|
async def add(
|
|
self,
|
|
*,
|
|
user_id: uuid.UUID,
|
|
track_id: uuid.UUID,
|
|
played_at: dt.datetime,
|
|
play_duration_seconds: int | None,
|
|
completed: bool,
|
|
) -> PlayHistoryEntry:
|
|
row = PlayHistoryModel(
|
|
user_id=user_id,
|
|
track_id=track_id,
|
|
played_at=played_at,
|
|
play_duration_seconds=play_duration_seconds,
|
|
completed=completed,
|
|
)
|
|
self._session.add(row)
|
|
await self._session.flush()
|
|
await self._session.refresh(row)
|
|
return _to_entity(row)
|
|
|
|
async def list(self, *, user_id: uuid.UUID, limit: int, offset: int) -> list[PlayHistoryEntry]:
|
|
rows = (
|
|
(
|
|
await self._session.execute(
|
|
select(PlayHistoryModel)
|
|
.where(PlayHistoryModel.user_id == user_id)
|
|
.order_by(PlayHistoryModel.played_at.desc())
|
|
.limit(limit)
|
|
.offset(offset)
|
|
)
|
|
)
|
|
.scalars()
|
|
.all()
|
|
)
|
|
return [_to_entity(r) for r in rows]
|
|
|
|
async def count(self, *, user_id: uuid.UUID) -> int:
|
|
return (
|
|
await self._session.execute(
|
|
select(func.count())
|
|
.select_from(PlayHistoryModel)
|
|
.where(PlayHistoryModel.user_id == user_id)
|
|
)
|
|
).scalar_one()
|