113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
"""JSON-based TV show repository implementation."""
|
|
from typing import List, Optional, Dict, Any
|
|
import logging
|
|
|
|
from domain.tv_shows.repositories import TVShowRepository
|
|
from domain.tv_shows.entities import TVShow
|
|
from domain.tv_shows.value_objects import ShowStatus
|
|
from domain.shared.value_objects import ImdbId
|
|
from ..memory import Memory
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class JsonTVShowRepository(TVShowRepository):
|
|
"""
|
|
JSON-based implementation of TVShowRepository.
|
|
|
|
Stores TV shows in the memory.json file (compatible with existing tv_shows structure).
|
|
"""
|
|
|
|
def __init__(self, memory: Memory):
|
|
"""
|
|
Initialize repository.
|
|
|
|
Args:
|
|
memory: Memory instance for persistence
|
|
"""
|
|
self.memory = memory
|
|
|
|
def save(self, show: TVShow) -> None:
|
|
"""Save a TV show to the repository."""
|
|
shows = self._load_all()
|
|
|
|
# Remove existing show with same IMDb ID
|
|
shows = [s for s in shows if s.get('imdb_id') != str(show.imdb_id)]
|
|
|
|
# Add new show
|
|
shows.append(self._to_dict(show))
|
|
|
|
# Save to memory
|
|
self.memory.set('tv_shows', shows)
|
|
logger.debug(f"Saved TV show: {show.imdb_id}")
|
|
|
|
def find_by_imdb_id(self, imdb_id: ImdbId) -> Optional[TVShow]:
|
|
"""Find a TV show by its IMDb ID."""
|
|
shows = self._load_all()
|
|
|
|
for show_dict in shows:
|
|
if show_dict.get('imdb_id') == str(imdb_id):
|
|
return self._from_dict(show_dict)
|
|
|
|
return None
|
|
|
|
def find_all(self) -> List[TVShow]:
|
|
"""Get all TV shows in the repository."""
|
|
shows_dict = self._load_all()
|
|
return [self._from_dict(s) for s in shows_dict]
|
|
|
|
def delete(self, imdb_id: ImdbId) -> bool:
|
|
"""Delete a TV show from the repository."""
|
|
shows = self._load_all()
|
|
initial_count = len(shows)
|
|
|
|
# Filter out the show
|
|
shows = [s for s in shows if s.get('imdb_id') != str(imdb_id)]
|
|
|
|
if len(shows) < initial_count:
|
|
self.memory.set('tv_shows', shows)
|
|
logger.debug(f"Deleted TV show: {imdb_id}")
|
|
return True
|
|
|
|
return False
|
|
|
|
def exists(self, imdb_id: ImdbId) -> bool:
|
|
"""Check if a TV show exists in the repository."""
|
|
return self.find_by_imdb_id(imdb_id) is not None
|
|
|
|
def _load_all(self) -> List[Dict[str, Any]]:
|
|
"""Load all TV shows from memory."""
|
|
return self.memory.get('tv_shows', [])
|
|
|
|
def _to_dict(self, show: TVShow) -> Dict[str, Any]:
|
|
"""Convert TVShow entity to dict for storage."""
|
|
return {
|
|
'imdb_id': str(show.imdb_id),
|
|
'title': show.title,
|
|
'seasons_count': show.seasons_count,
|
|
'status': show.status.value,
|
|
'tmdb_id': show.tmdb_id,
|
|
'overview': show.overview,
|
|
'poster_path': show.poster_path,
|
|
'first_air_date': show.first_air_date,
|
|
'vote_average': show.vote_average,
|
|
'added_at': show.added_at.isoformat(),
|
|
}
|
|
|
|
def _from_dict(self, data: Dict[str, Any]) -> TVShow:
|
|
"""Convert dict from storage to TVShow entity."""
|
|
from datetime import datetime
|
|
|
|
return TVShow(
|
|
imdb_id=ImdbId(data['imdb_id']),
|
|
title=data['title'],
|
|
seasons_count=data['seasons_count'],
|
|
status=ShowStatus.from_string(data['status']),
|
|
tmdb_id=data.get('tmdb_id'),
|
|
overview=data.get('overview'),
|
|
poster_path=data.get('poster_path'),
|
|
first_air_date=data.get('first_air_date'),
|
|
vote_average=data.get('vote_average'),
|
|
added_at=datetime.fromisoformat(data['added_at']) if data.get('added_at') else datetime.now(),
|
|
)
|