public static async Task API(History_API api) { try { if (api.api_datetime == null) { api.api_datetime = DateTime.Now; } //create a text log of api history Debug.WriteLine("Api -> | Action - " + api.api_action + " | Type - " + api.api_type); db.History_API.Add(api); await db.SaveChangesAsync(); } catch (Exception ex) { Debug.WriteLine("Problem occured when inserting history_api to database : " + ex.Message); } }
/// <summary> /// Insert new movies to database /// </summary> /// <returns></returns> public static async Task MoviesToDatabase() { await History.Create(History.Type.API, new History_API() { api_action = "Starting task -> Add movies to local Database", api_datetime = DateTime.Now, api_type = "Task -> add movies to Db", }); var list = new List <int>(); //item1 = MovieData, item2 = Match foreach (var item in NewMovieEntry) { try { //check if movie is already in database with this title and update values only for data not info!! var Db_Movie = await Db.Movie_Data.Where(x => x.name == item.Item1.name).FirstOrDefaultAsync(); if (Db_Movie == null) { //get movieinfo from api if (MoviesAPI.countAPICalls > 30) { await Task.Delay(5000); MoviesAPI.countAPICalls = 0; } //editMovieInfo, movie[0] is array from method GetMovieName Movie_Info mInfo = await MoviesAPI.Get.Info(item.Item2, DatabaseMovieCount); if (mInfo.id != null) { //tagline error in database has max length of 128 char in SQL if (mInfo.tagline.Length > 128) { mInfo.tagline = mInfo.tagline.Substring(0, 127); } item.Item1.Movie_Info = mInfo; //set tick for movie length item.Item1.Movie_Info.length = Media.Length(item.Item1); try { Db.Movie_Data.Add(item.Item1); await Db.SaveChangesAsync(); //databaseMovieCount++; //temp.Add(mData); Movie_Data movie = Db.Movie_Data.Where(x => x.name == item.Item1.name).First(); await History.Create(History.Type.API, new History_API() { api_action = "Movie " + movie.Movie_Info.title + " was added to the database as id " + movie.Id + "!", api_datetime = DateTime.Now, api_type = "Movie added to database", }); list.Add(movie.Id); } catch (Exception ex) { await History.Create(History.Type.API, new History_API() { api_action = "Exception : Inserting movie to Database --> " + ex.Message, api_datetime = DateTime.Now, api_type = "Exception thrown InsertMoviesToDb", }); } } else { await History.Create(History.Type.API, new History_API() { api_action = "Movie " + item.Item2.Groups["title"].ToString() + " was not added as there was a problem!", api_datetime = DateTime.Now, api_type = "Error on movie addition", }); } } else { //update movie info but only directory at which the movie is located await History.Create(History.Type.API, new History_API() { api_action = "Movie update -> Updating movie: " + Db_Movie.name + " directory!", api_datetime = DateTime.Now, api_type = "Task -> update movie data / info in SQL...", }); if (await Database.Movie.Update.Directory(Db_Movie, item.Item1)) { await History.Create(History.Type.API, new History_API() { api_action = "Movie update -> Succesfully updated movie: " + Db_Movie.name + " directory!", api_datetime = DateTime.Now, api_type = "Task -> Success", }); } else { await History.Create(History.Type.API, new History_API() { api_action = "Movie update -> Failed to update movie: " + Db_Movie.name + " directory!", api_datetime = DateTime.Now, api_type = "Task -> Failed", }); } } } catch (Exception ex) { await History.Create(History.Type.API, new History_API() { api_action = "Error -> An error occured : " + ex.Message, api_datetime = DateTime.Now, api_type = "Error occured on InsertMoviesToDb", }); } } await History.Create(History.Type.API, new History_API() { api_action = "End of import of movies.", api_datetime = DateTime.Now, api_type = "Status -> InsertMoviesToDb", }); History_API hapi; if (list.Count == NewMovieEntry.Count) { hapi = new History_API() { api_type = "Movie to Db status", api_datetime = DateTime.Now, api_action = "Info -> All movies added (" + list.Count + " - ADDED)", }; } else if (list.Count < NewMovieEntry.Count) { hapi = new History_API() { api_type = "Movie to Db status", api_datetime = DateTime.Now, api_action = "Error -> Less movies added than found on local storage! Movies added " + list.Count, }; } else { hapi = new History_API() { api_type = "Movie to Db status", api_datetime = DateTime.Now, api_action = "Error -> Something went wrong with importing data to Db! Movies added " + list.Count, }; } if (hapi != null) { await History.Create(History.Type.API, hapi); } }