Exemplo n.º 1
0
        public async Task <IEnumerable <FilmInfoDTO> > GetFilm(string filmName)
        {
            if (string.IsNullOrEmpty(filmName))
            {
                return(null);
            }

            //TODO: change find films
            var filmsDAL = (await filmsCache.Get())?
                           .Where(x => x.Title.PartialCompare(filmName));

            IList <FilmInfoDTO> filmsDTO = null;

            if ((filmsDAL == null || filmsDAL.Count() == 0) && !string.IsNullOrEmpty(BaseUri))
            {
                LogingService.LoggingServices.Instance.WriteLine <FilmRepository>($"Film not found in storage: {filmName}");

                string newFilmName = string.Join("+", filmName.Split(' '));
                var    filmFromApi = await net.GetObject <FilmInfo>(BaseUri + newFilmName);

                if (filmFromApi != null)
                {
                    if (filmFromApi.Response)
                    {
                        await filmsCache.Insert(filmFromApi);

                        filmsDTO = new List <FilmInfoDTO>()
                        {
                            AutoMapper.Mapper.Map <FilmInfo, FilmInfoDTO>(filmFromApi)
                        };
                    }
                }
                else
                {
                    LogingService.LoggingServices.Instance.WriteLine <FilmRepository>($"Film not found in internet: {filmName}");
                }
            }
            else
            {
                //TODO: May be -> Refactor copy-past
                filmsDTO = AutoMapper.Mapper.Map <IEnumerable <FilmInfo>, List <FilmInfoDTO> >(filmsDAL);
            }

            return(filmsDTO);
        }