Updated folder structure (for Docker)
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
"""JSON-based TV show repository implementation."""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from domain.shared.value_objects import ImdbId
|
||||
from domain.tv_shows.entities import TVShow
|
||||
from domain.tv_shows.repositories import TVShowRepository
|
||||
from domain.tv_shows.value_objects import ShowStatus
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class JsonTVShowRepository(TVShowRepository):
|
||||
"""
|
||||
JSON-based implementation of TVShowRepository.
|
||||
|
||||
Stores TV shows in the LTM library using the memory context.
|
||||
"""
|
||||
|
||||
def save(self, show: TVShow) -> None:
|
||||
"""
|
||||
Save a TV show to the repository.
|
||||
|
||||
Updates existing show if IMDb ID matches.
|
||||
|
||||
Args:
|
||||
show: TVShow entity to save.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
# Remove existing show with same IMDb ID
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(show.imdb_id)]
|
||||
|
||||
shows.append(self._to_dict(show))
|
||||
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
logger.debug(f"Saved TV show: {show.imdb_id}")
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: ImdbId) -> TVShow | None:
|
||||
"""
|
||||
Find a TV show by its IMDb ID.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to search for.
|
||||
|
||||
Returns:
|
||||
TVShow if found, None otherwise.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
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.
|
||||
|
||||
Returns:
|
||||
List of all TVShow entities.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows_dict = memory.ltm.library.get("tv_shows", [])
|
||||
return [self._from_dict(s) for s in shows_dict]
|
||||
|
||||
def delete(self, imdb_id: ImdbId) -> bool:
|
||||
"""
|
||||
Delete a TV show from the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID of show to delete.
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
initial_count = len(shows)
|
||||
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(imdb_id)]
|
||||
|
||||
if len(shows) < initial_count:
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
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.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to check.
|
||||
|
||||
Returns:
|
||||
True if exists, False otherwise.
|
||||
"""
|
||||
return self.find_by_imdb_id(imdb_id) is not None
|
||||
|
||||
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,
|
||||
"first_air_date": show.first_air_date,
|
||||
"added_at": show.added_at.isoformat(),
|
||||
}
|
||||
|
||||
def _from_dict(self, data: dict[str, Any]) -> TVShow:
|
||||
"""Convert dict from storage to TVShow entity."""
|
||||
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"),
|
||||
first_air_date=data.get("first_air_date"),
|
||||
added_at=(
|
||||
datetime.fromisoformat(data["added_at"])
|
||||
if data.get("added_at")
|
||||
else datetime.now()
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user