Skip to content

Commit

Permalink
Handle missing movies gracefully in repository methods
Browse files Browse the repository at this point in the history
The changes in `GetAllCuevanaMoviesCommand.cs` add a conditional check to skip processing for movies that do not have a corresponding entry in the movie repository.

In `MovieRepository.cs`, the `GetIdByTmdbId` method is modified to return 0 if no movie is found with the given `TmdbId`, instead of throwing an exception. This is achieved by using `FirstOrDefaultAsync` instead of `FirstAsync`.
  • Loading branch information
AbreuHD committed Feb 9, 2025
1 parent 9f65ddb commit 36fd992
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public async Task<bool> Handle(GetAllCuevanaMoviesCommand request, CancellationT
{
var movieWebAdd = await _movieWebRepository.AddAsync(_mapper.Map<MovieWeb>(movie));
var movieRepositoryId = await _movieRepository.GetIdByTmdbId(movie.TMDBTempID);

if(movieRepositoryId == 0)
{
continue;
}
await _movie_MovieWebRepository.AddAsync(new MovieMovieWeb
{
MovieID = movieRepositoryId,
Expand Down
3 changes: 2 additions & 1 deletion Infraestructure.Persistence/Repositories/MovieRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public async Task<List<MovieMovieWeb>> GetId(List<MovieMovieWeb> movieList)

public async Task<int> GetIdByTmdbId(int TmdbId)
{
return (await _dbContext.Set<Movie>().FirstAsync(x => x.TMDBID == TmdbId)).ID;
var movie = await _dbContext.Set<Movie>().FirstOrDefaultAsync(x => x.TMDBID == TmdbId);
return movie?.ID ?? 0;
}

public async Task<Movie> GetMovieInfo(int MovieId)
Expand Down

0 comments on commit 36fd992

Please sign in to comment.