public async Task <ActionResult <Movie> > PostMovie([FromBody] URLDTO URL) { String movieID = Helper.MovieHelper.GetMovieFromLink(URL.URL); Movie newMovie = Helper.MovieHelper.GetMovieFromID(movieID); _context.Movie.Add(newMovie); await _context.SaveChangesAsync(); // We are creating a new context so that loading realted movies don't block the api. MovieDBContext tempContext = new MovieDBContext(); RelatedMoviesController realtedMoviesController = new RelatedMoviesController(tempContext); //// This will be executed in the background. Task addCaptions = Task.Run(async() => { List <RelatedMovie> relatedMovies = Helper.MovieHelper.GetRelatedMovies(movieID); for (int i = 0; i < relatedMovies.Count; i++) { RelatedMovie relatedMovie = relatedMovies.ElementAt(i); relatedMovie.MovieId = newMovie.MovieId; // posting related movies to the database await realtedMoviesController.PostRelatedMovie(relatedMovie); } }); return(CreatedAtAction("GetMovie", new { id = newMovie.MovieId }, newMovie)); }
public async Task <ActionResult <Video> > PostVideo([FromBody] URLDTO data) { String videoURL; String videoId; Video video; try { // Constructing the video object from our helper function videoURL = data.URL; videoId = YouTubeHelper.GetVideoIdFromURL(videoURL); video = YouTubeHelper.GetVideoInfo(videoId); } catch { return(BadRequest("Invalid YouTube URL")); } // Determine if we can get transcriptions from YouTube if (!YouTubeHelper.CanGetTranscriptions(videoId)) { return(BadRequest("Subtitle does not exist on YouTube, failed to add video")); } if (VideoExists(video.VideoTitle)) { return(BadRequest("Video already exists in the database")); } // Add this video object to the database _context.Video.Add(video); await _context.SaveChangesAsync(); // Get the primary key of the newly created video record int id = video.VideoId; // This is needed because context are NOT thread safe, therefore we create another context for the following task. // We will be using this to insert transcriptions into the database on a seperate thread // So that it doesn't block the API. scriberContext tempContext = new scriberContext(); TranscriptionsController transcriptionsController = new TranscriptionsController(tempContext); // This will be executed in the background. Task addCaptions = Task.Run(async() => { List <Transcription> transcriptions = new List <Transcription>(); transcriptions = YouTubeHelper.GetTranscriptions(videoId); for (int i = 0; i < transcriptions.Count; i++) { // Get the transcription objects form transcriptions and assign VideoId to id, the primary key of the newly inserted video Transcription transcription = transcriptions.ElementAt(i); transcription.VideoId = id; // Add this transcription to the database await transcriptionsController.PostTranscription(transcription); } }); // Return success code and the info on the video object return(CreatedAtAction("GetVideo", new { id = video.VideoId }, video)); }