internal IEnumerable <MovieResponse> GetAllMovies() { using (var ctx = new TvApiContext()) { return(ctx.Movies.Select(movie => new MovieResponse() { Id = movie.Id, Title = movie.Title, Year = movie.Year }).ToList()); } }
internal void Add(MovieRequest movie) { using (var ctx = new TvApiContext()) { ctx.Movies.Add(new Movie() { Title = movie.Title, Year = movie.Year }); ctx.SaveChanges(); } }
internal IEnumerable <ReviewResponse> GetReviewsForMovie(int movieId) { using (var ctx = new TvApiContext()) { var movie = ctx.Movies.Find(movieId); return(movie.Reviews.Select(x => new ReviewResponse() { Id = x.Id, Comment = x.Comment, Rate = x.Rate }).ToList()); } }
internal void Delete(int id) { using (var ctx = new TvApiContext()) { var movie = ctx.Movies.Find(id); if (movie == null) { return; } ctx.Movies.Remove(movie); ctx.SaveChanges(); } }
internal MovieResponse Find(int id) { using (var ctx = new TvApiContext()) { var movie = ctx.Movies.Find(id); if (movie == null) { return(null); } return(new MovieResponse() { Id = movie.Id, Title = movie.Title, Year = movie.Year }); } }
internal void AddReviewToMovie(int movieId, ReviewRequest request) { using (var ctx = new TvApiContext()) { var movie = ctx.Movies.Find(movieId); if (movie == null) { throw new TvApiException($"Cannot add review. Invalid move id = {movieId}"); } movie.Reviews.Add(new Review() { Comment = request.Comment, Rate = request.Rate }); ctx.SaveChanges(); } }