Esempio n. 1
0
        private static void InsertToDatabase()
        {
            Console.WriteLine("Insert function initiated");
            while (true)
            {
                Thread.Sleep(3000);
                finishedInsertion = false;
                if (currentID != null)
                {
                    deleteFromQueue();
                }

                if (currentActress != null && finishedGeneratingFilms == true)
                {
                    Console.WriteLine("Can insert");
                    finishedInsertion = false;

                    string insertActressQuery = $"INSERT INTO Actress (actress_name, actress_role, actress_url, letter, id_consumidor) " +
                                                $"VALUES ('{currentActress.name}', '{currentActress.role}', '{currentActress.url}', '{currentActress.letter}', '{currentActress.idConsumidor}')";
                    Console.WriteLine(insertActressQuery);
                    conn.Insert(insertActressQuery);


                    foreach (Film film in films_list)
                    {
                        currentFilm = film;

                        string insertFilmQuery = $"INSERT INTO Films (movie_name, movie_url, id_consumidor) " +
                                                 $"VALUES ('{currentFilm.name}', '{currentFilm.url}', '{currentFilm.idConsumidor}')";
                        Console.WriteLine(insertFilmQuery);
                        conn.Insert(insertFilmQuery);

                        string insertBothQuery = $"INSERT INTO Actress_Filmography(actress_id, film_id) " +
                                                 $"SELECT a.id, f.id " +
                                                 $"FROM Actress as a, Films as f " +
                                                 $"WHERE a.actress_name = '{currentActress.name}' and f.movie_name = '{currentFilm.name}'";
                        Console.WriteLine(insertBothQuery);
                        conn.Insert(insertBothQuery);

                        Thread.Sleep(250);
                    }
                    films_list = new List <Film>();

                    currentActress    = null;
                    currentFilm       = null;
                    currentID         = null;
                    finishedInsertion = true;
                }
                else
                {
                    Console.WriteLine("Pending objects to insert");
                    Thread.Sleep(3000);
                }
            }
        }
Esempio n. 2
0
        private static async void GetItemFromWorker()
        {
            //Go to API and request the Worker for 1 element in Queue
            Console.WriteLine("GetItem function initiated");
            HttpClient client = new HttpClient();

            Console.WriteLine("Client created");
            client.BaseAddress = new Uri("http://listener2020.herokuapp.com");
            while (true)
            {
                Thread.Sleep(10000);
                if (currentActress == null && currentFilm == null)
                {
                    HttpResponseMessage response = await client.GetAsync("/colaUno/");

                    if (response.IsSuccessStatusCode)
                    {
                        //json = await response.Content.ReadAsAsync<FullObject>();
                        currentJSON = await response.Content.ReadAsStringAsync();

                        //Console.WriteLine(currentJSON);
                        Console.WriteLine("Object received from API");
                    }
                    else
                    {
                        Console.WriteLine("Could not get element from the queue");
                        currentJSON = null;
                    }

                    /*
                     * currentJSON = @"{
                     *                  ""url"": ""https://en.wikipedia.org/wiki/Diahnne_Abbott"",
                     *              ""Name"": ""Diahnne Abbott"",
                     *                  ""Role"": ""Actress, singer"",
                     *                  ""Letra"": ""A"",
                     *                  ""films"": [
                     *                  [
                     *                                  ""https://en.wikipedia.org/wiki/Taxi_Driver"",
                     *                                  ""Taxi Driver""
                     *                          ],
                     *                          [
                     *                                  ""https://en.wikipedia.org/wiki/Welcome_to_L.A."",
                     *                                  ""Welcome to L.A""
                     *                          ],
                     *                          [
                     *                                  ""https://en.wikipedia.org/wiki/New_York, _New_York_(1977_film)"",
                     *                                  ""New York, New York""
                     *                          ],
                     *                          [
                     *                                  ""https://en.wikipedia.org/wiki/The_King_of_Comedy_(film)"",
                     *                                  ""The King of Comedy""
                     *                          ]
                     *                  ]
                     *          }";
                     */

                    try
                    {
                        dynamic json = new JavaScriptSerializer().Deserialize <dynamic>(currentJSON);
                        currentID = json["_id"];
                        try
                        {
                            var films = json["result"]["films"];


                            //Console.WriteLine(currentID);
                            // ---------------------------------------------------------- //
                            /*   Create the currentActress object then insert to the DB   */
                            currentActress        = new Actress();
                            currentActress.url    = json["result"]["url"];
                            currentActress.name   = json["result"]["Name"];
                            currentActress.role   = json["result"]["Role"];
                            currentActress.letter = json["result"]["Letra"];
                            //currentActress.idConsumidor = ?

                            //Insert to the DB
                            //InsertToDatabase("actress");


                            // ---------------------------------------------------------- //

                            // ---------------------------------------------------------- //
                            /*   Read the entire array of Films and insert them separately in the DB   */
                            finishedGeneratingFilms = false;
                            foreach (var movie in films)
                            {
                                //Check every movie in the array
                                //  Movie[0] = The url of the movie
                                //  Movie[1] = The name of the movie
                                Film film = new Film();
                                film.name = movie[1];
                                film.url  = movie[0];
                                films_list.Add(film);

                                //InsertToDatabase("film");

                                //Insert into Actress_Filmography DB
                                //InsertToDatabase("both");
                            }
                            finishedGeneratingFilms = true;

                            //Console.ReadLine();
                        }
                        catch (Exception ex)
                        {
                            deleteFromQueue();
                        }
                    }catch (Exception ex)
                    {
                        Console.WriteLine("Empty Queue");
                    }
                }
                else
                {
                    Console.WriteLine("Can't get from API. Consumer is busy.");
                }
            }
        }