public async Task <T[]> DeserializeAsync <T>() { return(await JsonSerializer.DeserializeAsync <T[]>(new MemoryStream(Encoding.UTF8.GetBytes(_result)))); }
private async Task <MethodResponse> ToggleGardenLights(MethodRequest methodRequest, object userContext) { var state = await _mediator.Send(new ToggleGardenLightsCommand()); return(new MethodResponse(JsonSerializer.Serialize(state), 200)); }
public void Utf8Json2() { using Stream stream = new MemoryStream(Utf8JsonData); _ = Utf8JsonSerializer.Deserialize <T>(stream); }
public T[] Deserialize <T>() { return(JsonSerializer.Deserialize <T[]>(_result)); }
public async Task <IActionResult> GetFromCast(string castId) { var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes($"cast:{castId}")); try { var cachedMovie = await _cachingService.GetCache(hash); if (cachedMovie != null) { try { return(Content(cachedMovie, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var imdbParameter = new SqlParameter("@imdbCode", castId); var query = @" SELECT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames FROM MovieSet AS Movie INNER JOIN CastSet AS Cast ON Cast.MovieId = Movie.Id WHERE Cast.ImdbCode = @imdbCode"; var movieQuery = await context.Database.ExecuteSqlQueryAsync(query, new CancellationToken(), imdbParameter); var reader = movieQuery.DbDataReader; var movies = new List <MovieLightJson>(); while (await reader.ReadAsync()) { var movie = new MovieLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d, PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty, ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty, Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty }; movies.Add(movie); } var response = new MovieLightResponse { TotalMovies = movies.Count, Movies = movies }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } }
public string Utf8Json() => Utf8JsonSerializer.ToJsonString(SignUpScenario.MutableObject, JsonSettings.Utf8JsonResolver);
public async Task <IActionResult> GetLight(string imdb) { var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes($"light:{imdb}")); try { var cachedMovie = await _cachingService.GetCache(hash); if (cachedMovie != null) { try { return(Content(cachedMovie, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var imdbParameter = new SqlParameter("@imdbCode", imdb); var query = @" SELECT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames FROM MovieSet AS Movie WHERE Movie.ImdbCode = @imdbCode"; var movieQuery = await context.Database.ExecuteSqlQueryAsync(query, new CancellationToken(), imdbParameter); var reader = movieQuery.DbDataReader; var movie = new MovieLightJson(); while (await reader.ReadAsync()) { movie.Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty; movie.Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0; movie.Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d; movie.PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty; movie.ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty; movie.Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty; } if (string.IsNullOrEmpty(movie.ImdbCode)) { return(BadRequest()); } var json = JsonSerializer.ToJsonString(movie, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } }
public async Task <IActionResult> Get([RequiredFromQuery] int page, [FromQuery] int limit, [FromQuery] int minimum_rating, [FromQuery] string query_term, [FromQuery] string genre, [FromQuery] string sort_by) { var watch = new Stopwatch(); watch.Start(); var nbMoviesPerPage = 20; if (limit >= 20 && limit <= 50) { nbMoviesPerPage = limit; } var currentPage = 1; if (page >= 1) { currentPage = page; } var queryTerm = string.Empty; if (!string.IsNullOrWhiteSpace(query_term)) { queryTerm = query_term; } var genreFilter = string.Empty; if (!string.IsNullOrWhiteSpace(genre)) { genreFilter = genre; } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=movies&page={page}&limit={limit}&minimum_rating={minimum_rating}&query_term={ query_term }&genre={genre}&sort_by={sort_by}")); try { var cachedMovies = await _cachingService.GetCache(hash); if (cachedMovies != null) { try { watch.Stop(); return(Content(cachedMovies, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var skipParameter = new SqlParameter("@skip", (currentPage - 1) * nbMoviesPerPage); var takeParameter = new SqlParameter("@take", nbMoviesPerPage); var ratingParameter = new SqlParameter("@rating", minimum_rating); var queryParameter = new SqlParameter("@Keywords", string.Format(@"""{0}""", queryTerm)); var genreParameter = new SqlParameter("@genre", genreFilter); var query = @" SELECT DISTINCT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames, Torrent.Peers, Torrent.Seeds, COUNT(*) OVER () as TotalCount, Movie.DateUploadedUnix, Movie.Id, Movie.DownloadCount, Movie.LikeCount FROM MovieSet AS Movie CROSS APPLY ( SELECT TOP 1 Torrent.MovieId, Torrent.Peers, Torrent.Seeds FROM TorrentMovieSet AS Torrent WHERE Torrent.MovieId = Movie.Id AND Torrent.Url <> '' AND Torrent.Url IS NOT NULL ) Torrent INNER JOIN CastSet AS Cast ON Cast.MovieId = Movie.Id WHERE 1 = 1"; if (minimum_rating > 0 && minimum_rating < 10) { query += @" AND Rating >= @rating"; } if (!string.IsNullOrWhiteSpace(query_term)) { query += @" AND (CONTAINS(Movie.Title, @Keywords) OR CONTAINS(Cast.Name, @Keywords) OR CONTAINS(Movie.ImdbCode, @Keywords) OR CONTAINS(Cast.ImdbCode, @Keywords))"; } if (!string.IsNullOrWhiteSpace(genre)) { query += @" AND CONTAINS(Movie.GenreNames, @genre)"; } query += " GROUP BY Movie.Id, Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames, Torrent.Peers, Torrent.Seeds, Movie.DateUploadedUnix, Movie.Id, Movie.DownloadCount, Movie.LikeCount"; if (!string.IsNullOrWhiteSpace(sort_by)) { switch (sort_by) { case "title": query += " ORDER BY Movie.Title ASC"; break; case "year": query += " ORDER BY Movie.Year DESC"; break; case "rating": query += " ORDER BY Movie.Rating DESC"; break; case "peers": query += " ORDER BY Torrent.Peers DESC"; break; case "seeds": query += " ORDER BY Torrent.Seeds DESC"; break; case "download_count": query += " ORDER BY Movie.DownloadCount DESC"; break; case "like_count": query += " ORDER BY Movie.LikeCount DESC"; break; case "date_added": query += " ORDER BY Movie.DateUploadedUnix DESC"; break; default: query += " ORDER BY Movie.DateUploadedUnix DESC"; break; } } else { query += " ORDER BY Movie.DateUploadedUnix DESC"; } query += @" OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY"; var moviesQuery = await context.Database.ExecuteSqlQueryAsync(query, new CancellationToken(), skipParameter, takeParameter, ratingParameter, queryParameter, genreParameter); var reader = moviesQuery.DbDataReader; var count = 0; var movies = new List <MovieLightJson>(); while (await reader.ReadAsync()) { var movie = new MovieLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d, PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty, ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty, Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty }; movies.Add(movie); count = !await reader.IsDBNullAsync(8) ? reader.GetInt32(8) : 0; } var response = new MovieLightResponse { TotalMovies = count, Movies = movies }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); watch.Stop(); return(Content(json, "application/json")); } }
public async Task <IActionResult> GetMoviesByIds([FromBody] IEnumerable <string> imdbIds) { if (!imdbIds.Any()) { return(Json(new MovieLightResponse { Movies = new List <MovieLightJson>(), TotalMovies = 0 })); } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=movies&imdbIds={string.Join(',', imdbIds)}")); try { var cachedMovies = await _cachingService.GetCache(hash); if (cachedMovies != null) { try { return(Content(cachedMovies, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var query = @" SELECT DISTINCT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames, COUNT(*) OVER () as TotalCount FROM MovieSet AS Movie WHERE Movie.ImdbCode IN ({@imdbIds}) ORDER BY Movie.Rating DESC"; using (var cmd = new SqlCommand(query, new SqlConnection(context.Database.GetDbConnection().ConnectionString))) { cmd.AddArrayParameters(imdbIds, "@imdbIds"); await cmd.Connection.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(new CancellationToken()); var count = 0; var movies = new List <MovieLightJson>(); while (await reader.ReadAsync()) { var movie = new MovieLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d, PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty, ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty, Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty }; movies.Add(movie); count = !await reader.IsDBNullAsync(6) ? reader.GetInt32(6) : 0; } var response = new MovieLightResponse { TotalMovies = count, Movies = movies }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } } }
public async Task <IActionResult> GetSimilar([FromBody] IEnumerable <string> imdbIds, [RequiredFromQuery] int page, [FromQuery] int limit, [FromQuery] int minimum_rating, [FromQuery] string query_term, [FromQuery] string genre, [FromQuery] string sort_by) { var nbMoviesPerPage = 20; if (limit >= 20 && limit <= 50) { nbMoviesPerPage = limit; } var currentPage = 1; if (page >= 1) { currentPage = page; } var queryTerm = string.Empty; if (!string.IsNullOrWhiteSpace(query_term)) { queryTerm = query_term; } var genreFilter = string.Empty; if (!string.IsNullOrWhiteSpace(genre)) { genreFilter = genre; } if (!imdbIds.Any()) { return(Json(new MovieLightResponse { Movies = new List <MovieLightJson>(), TotalMovies = 0 })); } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=movies&similar&imdbIds={string.Join(',', imdbIds)}&page={page}&limit={limit}&minimum_rating={minimum_rating}&query_term={ query_term }&genre={genre}&sort_by={sort_by}")); try { var cachedMovies = await _cachingService.GetCache(hash); if (cachedMovies != null) { try { return(Content(cachedMovies, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var skipParameter = new SqlParameter("@skip", (currentPage - 1) * nbMoviesPerPage); var takeParameter = new SqlParameter("@take", nbMoviesPerPage); var ratingParameter = new SqlParameter("@rating", minimum_rating); var queryParameter = new SqlParameter("@Keywords", string.Format(@"""{0}""", queryTerm)); var genreParameter = new SqlParameter("@genre", genreFilter); var query = @" SELECT DISTINCT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames, COUNT(*) OVER () as TotalCount FROM MovieSet AS Movie WHERE Movie.ImdbCode IN (SELECT Similar.TmdbId FROM Similar AS Similar INNER JOIN ( SELECT Movie.ID FROM MovieSet AS Movie WHERE Movie.ImdbCode IN ({@imdbIds}) ) Movie ON Similar.MovieId = Movie.Id) AND 1 = 1"; if (minimum_rating > 0 && minimum_rating < 10) { query += @" AND Rating >= @rating"; } if (!string.IsNullOrWhiteSpace(query_term)) { query += @" AND (CONTAINS(Movie.Title, @Keywords) OR CONTAINS(Movie.ImdbCode, @Keywords))"; } if (!string.IsNullOrWhiteSpace(genre)) { query += @" AND CONTAINS(Movie.GenreNames, @genre)"; } query += " ORDER BY Movie.Rating DESC"; query += @" OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY"; using (var cmd = new SqlCommand(query, new SqlConnection(context.Database.GetDbConnection().ConnectionString))) { cmd.AddArrayParameters(imdbIds, "@imdbIds"); cmd.Parameters.Add(skipParameter); cmd.Parameters.Add(takeParameter); cmd.Parameters.Add(ratingParameter); cmd.Parameters.Add(queryParameter); cmd.Parameters.Add(genreParameter); await cmd.Connection.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(new CancellationToken()); var count = 0; var movies = new List <MovieLightJson>(); while (await reader.ReadAsync()) { var movie = new MovieLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d, PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty, ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty, Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty }; movies.Add(movie); count = !await reader.IsDBNullAsync(6) ? reader.GetInt32(6) : 0; } var response = new MovieLightResponse { TotalMovies = count, Movies = movies }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } } }
public SignUpDto Utf8Json() => Utf8JsonSerializer.Deserialize <SignUpDto>(SignUpScenario.Json, JsonSettings.Utf8JsonResolver);
public async Task <IActionResult> Get([RequiredFromQuery] int page, [FromQuery] int limit, [FromQuery] int minimum_rating, [FromQuery] string query_term, [FromQuery] string genre, [FromQuery] string sort_by) { var nbShowsPerPage = 20; if (limit >= 20 && limit <= 50) { nbShowsPerPage = limit; } var currentPage = 1; if (page >= 1) { currentPage = page; } var queryTerm = string.Empty; if (!string.IsNullOrWhiteSpace(query_term)) { queryTerm = query_term; } var genreFilter = string.Empty; if (!string.IsNullOrWhiteSpace(genre)) { genreFilter = genre; } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=shows&page={page}&limit={limit}&minimum_rating={minimum_rating}&query_term={ query_term }&genre={genre}&sort_by={sort_by}")); try { var cachedShows = await _cachingService.GetCache(hash); if (cachedShows != null) { try { return(Content(cachedShows, "application/json")); } catch (Exception ex) { _telemetryClient.TrackException(ex); } } } catch (Exception ex) { _telemetryClient.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var skipParameter = new SqlParameter("@skip", (currentPage - 1) * nbShowsPerPage); var takeParameter = new SqlParameter("@take", nbShowsPerPage); var ratingParameter = new SqlParameter("@rating", minimum_rating); var queryParameter = new SqlParameter("@Keywords", string.Format(@"""{0}""", queryTerm)); var genreParameter = new SqlParameter("@genre", genreFilter); var query = @" SELECT Show.Title, Show.Year, Rating.Percentage, Rating.Loved, Rating.Votes, Rating.Hated, Rating.Watching, Show.LastUpdated, Image.Banner, Image.Poster, Show.ImdbId, Show.TvdbId, Show.GenreNames, COUNT(*) OVER () as TotalCount FROM ShowSet AS Show INNER JOIN ImageShowSet AS Image ON Image.Id = Show.ImagesId INNER JOIN RatingSet AS Rating ON Rating.Id = Show.RatingId WHERE Show.NumSeasons <> 0"; if (minimum_rating > 0 && minimum_rating < 10) { query += @" AND Rating >= @rating"; } if (!string.IsNullOrWhiteSpace(query_term)) { query += @" AND (CONTAINS(Title, @Keywords) OR CONTAINS(ImdbId, @Keywords) OR CONTAINS(TvdbId, @Keywords))"; } if (!string.IsNullOrWhiteSpace(genre)) { query += @" AND CONTAINS(GenreNames, @genre)"; } if (!string.IsNullOrWhiteSpace(sort_by)) { switch (sort_by) { case "title": query += " ORDER BY Show.Title ASC"; break; case "year": query += " ORDER BY Show.Year DESC"; break; case "rating": query += " ORDER BY Rating.Percentage DESC"; break; case "loved": query += " ORDER BY Rating.Loved DESC"; break; case "votes": query += " ORDER BY Rating.Votes DESC"; break; case "watching": query += " ORDER BY Rating.Watching DESC"; break; case "date_added": query += " ORDER BY Show.LastUpdated DESC"; break; default: query += " ORDER BY Show.LastUpdated DESC"; break; } } else { query += " ORDER BY Show.LastUpdated DESC"; } query += @" OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY"; var showsQuery = await context.Database.ExecuteSqlQueryAsync(query, new CancellationToken(), skipParameter, takeParameter, ratingParameter, queryParameter, genreParameter); var reader = showsQuery.DbDataReader; var count = 0; var shows = new List <ShowLightJson>(); while (await reader.ReadAsync()) { var show = new ShowLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = new RatingJson { Percentage = !await reader.IsDBNullAsync(2) ? reader.GetInt32(2) : 0, Loved = !await reader.IsDBNullAsync(3) ? reader.GetInt32(3) : 0, Votes = !await reader.IsDBNullAsync(4) ? reader.GetInt32(4) : 0, Hated = !await reader.IsDBNullAsync(5) ? reader.GetInt32(5) : 0, Watching = !await reader.IsDBNullAsync(6) ? reader.GetInt32(6) : 0 }, Images = new ImageShowJson { Banner = !await reader.IsDBNullAsync(8) ? reader.GetString(8) : string.Empty, Poster = !await reader.IsDBNullAsync(9) ? reader.GetString(9) : string.Empty, }, ImdbId = !await reader.IsDBNullAsync(10) ? reader.GetString(10) : string.Empty, TvdbId = !await reader.IsDBNullAsync(11) ? reader.GetString(11) : string.Empty, Genres = !await reader.IsDBNullAsync(12) ? reader.GetString(12) : string.Empty }; shows.Add(show); count = !await reader.IsDBNullAsync(13) ? reader.GetInt32(13) : 0; } var response = new ShowLightResponse { TotalShows = count, Shows = shows }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } }
public async Task <IActionResult> GetShowByIds([FromBody] IEnumerable <string> imdbIds) { if (!imdbIds.Any()) { return(Json(new ShowLightResponse { Shows = new List <ShowLightJson>(), TotalShows = 0 })); } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=shows&imdbIds={string.Join(',', imdbIds)}")); try { var cachedShow = await _cachingService.GetCache(hash); if (cachedShow != null) { try { return(Content(cachedShow, "application/json")); } catch (Exception ex) { _telemetryClient.TrackException(ex); } } } catch (Exception ex) { _telemetryClient.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var query = @" SELECT DISTINCT Show.Title, Show.Year, Rating.Percentage, Rating.Loved, Rating.Votes, Rating.Hated, Rating.Watching, Show.LastUpdated, Image.Banner, Image.Poster, Show.ImdbId, Show.TvdbId, Show.GenreNames, COUNT(*) OVER () as TotalCount FROM ShowSet AS Show INNER JOIN ImageShowSet AS Image ON Image.Id = Show.ImagesId INNER JOIN RatingSet AS Rating ON Rating.Id = Show.RatingId WHERE Show.ImdbId IN ({@imdbIds})"; using (var cmd = new SqlCommand(query, new SqlConnection(context.Database.GetDbConnection().ConnectionString))) { cmd.AddArrayParameters(imdbIds, "@imdbIds"); await cmd.Connection.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(new CancellationToken()); var count = 0; var shows = new List <ShowLightJson>(); while (await reader.ReadAsync()) { var show = new ShowLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = new RatingJson { Percentage = !await reader.IsDBNullAsync(2) ? reader.GetInt32(2) : 0, Loved = !await reader.IsDBNullAsync(3) ? reader.GetInt32(3) : 0, Votes = !await reader.IsDBNullAsync(4) ? reader.GetInt32(4) : 0, Hated = !await reader.IsDBNullAsync(5) ? reader.GetInt32(5) : 0, Watching = !await reader.IsDBNullAsync(6) ? reader.GetInt32(6) : 0 }, Images = new ImageShowJson { Banner = !await reader.IsDBNullAsync(8) ? reader.GetString(8) : string.Empty, Poster = !await reader.IsDBNullAsync(9) ? reader.GetString(9) : string.Empty, }, ImdbId = !await reader.IsDBNullAsync(10) ? reader.GetString(10) : string.Empty, TvdbId = !await reader.IsDBNullAsync(11) ? reader.GetString(11) : string.Empty, Genres = !await reader.IsDBNullAsync(12) ? reader.GetString(12) : string.Empty }; shows.Add(show); count = !await reader.IsDBNullAsync(13) ? reader.GetInt32(13) : 0; } var response = new ShowLightResponse { TotalShows = count, Shows = shows }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application /json")); } } }
public async Task <IActionResult> GetLight(string imdb) { var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes($"light:{imdb}")); try { var cachedShow = await _cachingService.GetCache(hash); if (cachedShow != null) { try { return(Content(cachedShow, "application/json")); } catch (Exception ex) { _telemetryClient.TrackException(ex); } } } catch (Exception ex) { _telemetryClient.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var imdbParameter = new SqlParameter("@imdbId", imdb); var query = @" SELECT Show.Title, Show.Year, Rating.Percentage, Rating.Loved, Rating.Votes, Rating.Hated, Rating.Watching, Show.LastUpdated, Image.Banner, Image.Poster, Show.ImdbId, Show.TvdbId, Show.GenreNames FROM ShowSet AS Show INNER JOIN ImageShowSet AS Image ON Image.Id = Show.ImagesId INNER JOIN RatingSet AS Rating ON Rating.Id = Show.RatingId WHERE Show.ImdbId = @imdbId"; var showQuery = await context.Database.ExecuteSqlQueryAsync(query, new CancellationToken(), imdbParameter); var reader = showQuery.DbDataReader; var show = new ShowLightJson(); while (await reader.ReadAsync()) { show.Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty; show.Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0; show.Rating = new RatingJson { Percentage = !await reader.IsDBNullAsync(2) ? reader.GetInt32(2) : 0, Loved = !await reader.IsDBNullAsync(3) ? reader.GetInt32(3) : 0, Votes = !await reader.IsDBNullAsync(4) ? reader.GetInt32(4) : 0, Hated = !await reader.IsDBNullAsync(5) ? reader.GetInt32(5) : 0, Watching = !await reader.IsDBNullAsync(6) ? reader.GetInt32(6) : 0 }; show.Images = new ImageShowJson { Banner = !await reader.IsDBNullAsync(8) ? reader.GetString(8) : string.Empty, Poster = !await reader.IsDBNullAsync(9) ? reader.GetString(9) : string.Empty, }; show.ImdbId = !await reader.IsDBNullAsync(10) ? reader.GetString(10) : string.Empty; show.TvdbId = !await reader.IsDBNullAsync(11) ? reader.GetString(11) : string.Empty; show.Genres = !await reader.IsDBNullAsync(12) ? reader.GetString(12) : string.Empty; } if (string.IsNullOrEmpty(show.ImdbId)) { return(BadRequest()); } var json = JsonSerializer.ToJsonString(show, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } }
public async Task <IActionResult> GetSimilar([FromBody] IEnumerable <string> imdbIds, [RequiredFromQuery] int page, [FromQuery] int limit) { if (!imdbIds.Any()) { return(Json(new MovieLightResponse { Movies = new List <MovieLightJson>(), TotalMovies = 0 })); } var nbMoviesPerPage = 20; if (limit >= 20 && limit <= 50) { nbMoviesPerPage = limit; } var currentPage = 1; if (page >= 1) { currentPage = page; } var hash = Convert.ToBase64String( Encoding.UTF8.GetBytes( $@"type=movies&page={page}&limit={limit}&imdbId={string.Join(',', imdbIds)}")); try { var cachedMovies = await _cachingService.GetCache(hash); if (cachedMovies != null) { try { return(Content(cachedMovies, "application/json")); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { var skipParameter = new SqlParameter("@skip", (currentPage - 1) * nbMoviesPerPage); var takeParameter = new SqlParameter("@take", nbMoviesPerPage); var query = @" SELECT DISTINCT Movie.Title, Movie.Year, Movie.Rating, Movie.PosterImage, Movie.ImdbCode, Movie.GenreNames, Torrent.Peers, Torrent.Seeds, COUNT(*) OVER () as TotalCount FROM MovieSet AS Movie INNER JOIN TorrentMovieSet AS Torrent ON Torrent.MovieId = Movie.Id INNER JOIN Similar ON Similar.MovieId = Movie.Id AND Torrent.Quality = '720p' WHERE Similar.TmdbId IN ({@imdbIds}) AND Torrent.Url <> '' AND Torrent.Url IS NOT NULL ORDER BY Movie.Rating DESC"; query += @" OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY"; using (var cmd = new SqlCommand(query, new SqlConnection(context.Database.GetDbConnection().ConnectionString))) { cmd.AddArrayParameters(imdbIds, "@imdbIds"); cmd.Parameters.Add(skipParameter); cmd.Parameters.Add(takeParameter); await cmd.Connection.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(new CancellationToken()); var count = 0; var movies = new List <MovieLightJson>(); while (await reader.ReadAsync()) { var movie = new MovieLightJson { Title = !await reader.IsDBNullAsync(0) ? reader.GetString(0) : string.Empty, Year = !await reader.IsDBNullAsync(1) ? reader.GetInt32(1) : 0, Rating = !await reader.IsDBNullAsync(2) ? reader.GetDouble(2) : 0d, PosterImage = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : string.Empty, ImdbCode = !await reader.IsDBNullAsync(4) ? reader.GetString(4) : string.Empty, Genres = !await reader.IsDBNullAsync(5) ? reader.GetString(5) : string.Empty }; movies.Add(movie); count = !await reader.IsDBNullAsync(8) ? reader.GetInt32(8) : 0; } var response = new MovieLightResponse { TotalMovies = count, Movies = movies }; var json = JsonSerializer.ToJsonString(response, StandardResolver.SnakeCase); await _cachingService.SetCache(hash, json, TimeSpan.FromDays(1)); return(Content(json, "application/json")); } } }