예제 #1
0
        protected Task <bool> TaskImplementation(CancellationToken cancellationToken)
        {
            SqlConnection connection = new SqlConnection(ProjectVariables.dbConnectionString);

            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand();
                command.Connection = connection;

                /* read id of the last movie which it's data refreshed */
                int lastRefreshedId = DBFunctions.getLastRefreshedId(command);

                /* these movies will be refreshed from remote repository */
                List <MovieClass> movieList = DBFunctions.getMovieTitles(command, lastRefreshedId);

                MovieRepoFactory repoFactory = MovieRepoFactory.getInstance();
                IMovieRepo       repo        = repoFactory.getRepo(MovieRepoFactory.Repos.OMDB);

                int i = 0;
                for (i = 0; i < movieList.Count; i++)
                {
                    MovieClass oldMovie = movieList[i];
                    MovieClass newMovie = repo.getMovie(oldMovie.t);
                    newMovie.i = oldMovie.i;

                    DBFunctions.updateMovie(command, newMovie);
                    DBFunctions.updateLastRefreshedId(command, newMovie.i);
                }

                /* if all movies are refreshed, then start from the first one */
                if (i == 0)
                {
                    DBFunctions.updateLastRefreshedId(command, 0);
                }
            }
            catch (Exception ex)
            {
                Functions.LogWebMethodError(this.GetType().Name,
                                            System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }

            return(Task.FromResult <bool>(true));
        }
예제 #2
0
        public JsonResult GetMovie(string userName, string password, string title)
        {
            GetMovieResult result     = new GetMovieResult();
            SqlConnection  connection = new SqlConnection(ProjectVariables.dbConnectionString);

            try
            {
                Functions.EnsureNotNullCredentials(userName, password);

                connection.Open();
                SqlCommand command = new SqlCommand();
                command.Connection = connection;

                bool flag = DBFunctions.checkLogin(command, userName, password);
                if (!flag)
                {
                    result.isErr = true;
                }
                else
                {
                    // get movie from cache if it is cached
                    MovieClass cacheMovie = null;
                    MovieClass movie      = null;
                    if (Functions.getCache <MovieClass>(cache, ProjectVariables.MOVIE_CACHE_TAG, out cacheMovie))
                    {
                        if (cacheMovie.t.Contains(title))
                        {
                            movie = cacheMovie;
                        }
                    }

                    // read movie from database
                    if (movie == null)
                    {
                        movie = DBFunctions.getMovie(command, title);
                    }

                    // get the movie from remote repositories
                    if (movie == null)
                    {
                        MovieRepoFactory repoFactory = MovieRepoFactory.getInstance();
                        IMovieRepo       repo        = repoFactory.getRepo(MovieRepoFactory.Repos.OMDB);

                        movie = repo.getMovie(title);
                        if (movie != null)
                        {
                            DBFunctions.insertMovie(command, movie);
                        }
                    }

                    // add to cache
                    if (movie != null)
                    {
                        Functions.setCache <MovieClass>(cache, ProjectVariables.MOVIE_CACHE_TAG, movie);
                    }

                    result.m = movie;
                }

                connection.Close();
            }
            catch (Exception ex)
            {
                result.isErr = true;
                Functions.LogWebMethodError(this.GetType().Name,
                                            System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }

            return(Json(result));
        }