diff --git a/README.md b/README.md index f8d8a1a..fef3321 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,11 @@ torrents and authentication. - [x] fully automatic downloads - [x] add tests - [x] add more logs/errors +- [x] make API return proper error codes - [ ] support for movies - [ ] responsive ui - [ ] add check at startup if hardlinks work - [ ] support styling the login with OIDC button -- [ ] make API return proper error codes - [ ] add in-depth documentation on the architecure of the codebase - [ ] expand README with more information and a quickstart guide - [ ] make indexer module multithreaded diff --git a/media_manager/tv/exceptions.py b/media_manager/exceptions.py similarity index 100% rename from media_manager/tv/exceptions.py rename to media_manager/exceptions.py diff --git a/media_manager/torrent/dependencies.py b/media_manager/torrent/dependencies.py index a3db70a..df025af 100644 --- a/media_manager/torrent/dependencies.py +++ b/media_manager/torrent/dependencies.py @@ -2,10 +2,12 @@ from typing import Annotated from fastapi import Depends +from exceptions import NotFoundError from media_manager.database import DbSessionDependency from media_manager.torrent.service import TorrentService from media_manager.torrent.repository import TorrentRepository - +from media_manager.torrent.schemas import TorrentId, Torrent +from fastapi.exceptions import HTTPException def get_torrent_repository(db: DbSessionDependency) -> TorrentRepository: return TorrentRepository(db=db) @@ -19,3 +21,22 @@ def get_torrent_service(torrent_repository: torrent_repository_dep) -> TorrentSe torrent_service_dep = Annotated[TorrentService, Depends(get_torrent_service)] + +def get_torrent_by_id( + torrent_service: torrent_service_dep, + torrent_id: TorrentId +) -> Torrent: + """ + Retrieves a torrent by its ID. + + :param torrent_service: The TorrentService instance. + :param torrent_id: The ID of the torrent to retrieve. + :return: The TorrentService instance with the specified torrent. + """ + try: + torrent = torrent_service.get_torrent_by_id(torrent_id=torrent_id) + except NotFoundError: + raise HTTPException(status_code=404, detail=f"Torrent with ID {torrent_id} not found") + return torrent + +torrent_dep = Annotated[Torrent, Depends(get_torrent_by_id)] \ No newline at end of file diff --git a/media_manager/torrent/repository.py b/media_manager/torrent/repository.py index 7bb678d..63bb968 100644 --- a/media_manager/torrent/repository.py +++ b/media_manager/torrent/repository.py @@ -5,6 +5,7 @@ from media_manager.torrent.models import Torrent from media_manager.torrent.schemas import TorrentId, Torrent as TorrentSchema from media_manager.tv.models import SeasonFile, Show, Season from media_manager.tv.schemas import SeasonFile as SeasonFileSchema, Show as ShowSchema +from tv.exceptions import NotFoundError class TorrentRepository: @@ -44,7 +45,10 @@ class TorrentRepository: ] def get_torrent_by_id(self, torrent_id: TorrentId) -> TorrentSchema: - return TorrentSchema.model_validate(self.db.get(Torrent, torrent_id)) + result = self.db.get(Torrent, torrent_id) + if result is None: + raise NotFoundError(f"Torrent with ID {torrent_id} not found.") + return TorrentSchema.model_validate(result) def delete_torrent(self, torrent_id: TorrentId): self.db.delete(self.db.get(Torrent, torrent_id)) diff --git a/media_manager/torrent/router.py b/media_manager/torrent/router.py index 3194ee8..a617cb4 100644 --- a/media_manager/torrent/router.py +++ b/media_manager/torrent/router.py @@ -3,15 +3,15 @@ from fastapi import status from fastapi.params import Depends from media_manager.auth.users import current_active_user, current_superuser -from media_manager.torrent.dependencies import torrent_service_dep +from media_manager.torrent.dependencies import torrent_service_dep, torrent_dep from media_manager.torrent.schemas import TorrentId, Torrent router = APIRouter() @router.get("/{torrent_id}", status_code=status.HTTP_200_OK, response_model=Torrent) -def get_torrent(service: torrent_service_dep, torrent_id: TorrentId): - return service.get_torrent_by_id(id=torrent_id) +def get_torrent(service: torrent_service_dep, torrent: torrent_dep): + return service.get_torrent_by_id(id=torrent.id) @router.get( @@ -30,8 +30,8 @@ def get_all_torrents(service: torrent_service_dep): dependencies=[Depends(current_active_user)], response_model=Torrent, ) -def import_torrent(service: torrent_service_dep, torrent_id: TorrentId): - return service.import_torrent(service.get_torrent_by_id(id=torrent_id)) +def import_torrent(service: torrent_service_dep, torrent: torrent_dep): + return service.import_torrent(service.get_torrent_by_id(id=torrent.id)) @router.post( @@ -49,5 +49,5 @@ def import_all_torrents(service: torrent_service_dep): status_code=status.HTTP_200_OK, dependencies=[Depends(current_superuser)], ) -def delete_torrent(service: torrent_service_dep, torrent_id: TorrentId): - service.delete_torrent(torrent_id=torrent_id) +def delete_torrent(service: torrent_service_dep, torrent: torrent_dep): + service.delete_torrent(torrent_id=torrent.id) diff --git a/media_manager/tv/dependencies.py b/media_manager/tv/dependencies.py index a7981db..cd1434b 100644 --- a/media_manager/tv/dependencies.py +++ b/media_manager/tv/dependencies.py @@ -6,7 +6,7 @@ from media_manager.database import DbSessionDependency from media_manager.tv.repository import TvRepository from media_manager.tv.schemas import Show, ShowId, SeasonId, Season from media_manager.tv.service import TvService -from media_manager.tv.exceptions import NotFoundError +from media_manager.exceptions import NotFoundError from fastapi import HTTPException from media_manager.indexer.dependencies import indexer_service_dep from media_manager.torrent.dependencies import torrent_service_dep diff --git a/media_manager/tv/repository.py b/media_manager/tv/repository.py index 85ca271..c247f4d 100644 --- a/media_manager/tv/repository.py +++ b/media_manager/tv/repository.py @@ -9,7 +9,7 @@ from media_manager.torrent.models import Torrent from media_manager.torrent.schemas import TorrentId, Torrent as TorrentSchema from media_manager.tv import log from media_manager.tv.models import Season, Show, Episode, SeasonRequest, SeasonFile -from media_manager.tv.exceptions import NotFoundError +from media_manager.exceptions import NotFoundError from media_manager.tv.schemas import ( Season as SeasonSchema, SeasonId, diff --git a/media_manager/tv/router.py b/media_manager/tv/router.py index 26b9f25..6b1f38d 100644 --- a/media_manager/tv/router.py +++ b/media_manager/tv/router.py @@ -10,7 +10,7 @@ from media_manager.indexer.schemas import PublicIndexerQueryResult, IndexerQuery from media_manager.metadataProvider.schemas import MetaDataProviderShowSearchResult from media_manager.torrent.schemas import Torrent from media_manager.tv import log -from media_manager.tv.exceptions import MediaAlreadyExists +from media_manager.exceptions import MediaAlreadyExists from media_manager.tv.schemas import ( Show, SeasonRequest, diff --git a/media_manager/tv/service.py b/media_manager/tv/service.py index e88165b..e8b6e4d 100644 --- a/media_manager/tv/service.py +++ b/media_manager/tv/service.py @@ -31,7 +31,7 @@ from media_manager.tv.schemas import ( ) from media_manager.torrent.schemas import QualityStrings from media_manager.tv.repository import TvRepository -from media_manager.tv.exceptions import NotFoundError +from media_manager.exceptions import NotFoundError import pprint from pathlib import Path from media_manager.config import BasicConfig