예제 #1
0
        public ActionResult metadata(int movieId)
        {
            try{
                //build the list of data to work with from the provided .csv file
                List <MovieData> rawData = System.IO.File.ReadAllLines("..\\metadata.csv")
                                           .Skip(1)
                                           .Select(d => DataFromCsv.MovieDataFromCsv(d))
                                           .ToList();

                //initiate a new object to populate with the data for the output
                List <MovieData> movieData = new List <MovieData>();

                //Initial Linq query to filter down the results
                movieData = rawData.Where(x => x.movieId == movieId && x.id != -1).Select(x => x).ToList();

                //Group the results and then order by language
                movieData = movieData.GroupBy(x => x.language)
                            .Select(x => x.OrderByDescending(x => x.id).First())
                            .OrderBy(x => x.language)
                            .ToList();

                //Convert to Dto for export
                List <movieDataDto> Result = movieData.Select(x => new movieDataDto {
                    movieId     = x.movieId,
                    title       = x.title,
                    language    = x.language,
                    duration    = x.duration,
                    releaseYear = x.releaseYear
                }
                                                              ).ToList();


                //Check to make sure that the movie data exists, and if not return a 404
                if (Result == null)
                {
                    return(BadRequest());
                }
                //if the movie data does exist, serialise to Json and then return the result
                else
                {
                    var JsonMovieData = JsonSerializer.Serialize <List <movieDataDto> >(Result);

                    return(Ok(JsonMovieData));
                }
            }
            //catch in case there is an issue with reading the data, and if there is return a 404 with the innder exception.
            catch (Exception ex) {
                return(NotFound(ex.InnerException));
            }
        }
예제 #2
0
        public ActionResult moviesStats()
        {
            //Collect the Data from the CSV Sources
            List <MovieData> rawData = System.IO.File.ReadAllLines("..\\metadata.csv")
                                       .Skip(1)
                                       .Select(d => DataFromCsv.MovieDataFromCsv(d))
                                       .ToList();
            List <MovieStats> rawStats = System.IO.File.ReadAllLines("..\\stats.csv")
                                         .Skip(1)
                                         .Select(d => DataFromCsv.MovieStatsFromCsv(d))
                                         .ToList();

            //refine data by grouping
            rawData  = rawData.GroupBy(g => g.movieId).Select(x => x.OrderByDescending(x => x.movieId).First()).ToList();
            rawStats = rawStats.GroupBy(g => g.movieId)
                       .Select(x => new MovieStats
            {
                movieId = x.First().movieId,
                averageWatchDurationS = x.Sum(c => c.averageWatchDurationS),
                count = x.Count()
            }).ToList();

            //combine data for output Json
            List <MovieStatsDto> movieStatsDto = rawData.Join(rawStats, d => d.movieId, s => s.movieId, (d, s) => new MovieStatsDto {
                movieId = d.movieId,
                title   = d.title,
                averageWatchDurationS = s.averageWatchDurationS,
                watches     = (s.averageWatchDurationS / Convert.ToInt64(s.count)),
                releaseYear = d.releaseYear
            }).OrderByDescending(x => x.watches).ThenBy(x => x.releaseYear).ToList();

            //Covert output to Json
            var JsonMovieStats = JsonSerializer.Serialize <List <MovieStatsDto> >(movieStatsDto);

            //Return Json
            return(Ok(JsonMovieStats));
        }