public ActionResult Procurar(string termo, int? pagina)
        {
            PostTwitter postTwitter = new PostTwitter();
            Categoria categoria = new Categoria();
            int estadoId = Convert.ToInt32(Request["EstadoId"]);
            string estado = estadoRepository.GetStatebyId(estadoId);
            var service = twitterServiceRepository.ConfigureService();
            int numeroPagina = pagina ?? 1;
            int categoriaId;

            ViewBag.EstadoId = new SelectList(estadoRepository.GetAllStates(), "EstadoId", "Nome");


            if (string.IsNullOrEmpty(termo))
            {
                if (Request.IsAjaxRequest())
                    return PartialView("Tweets", postTwitterRepository.GetAll());

                return View("Index", postTwitterRepository.GetAll());
            }

            if (categoriaRepository.CheckIfExists(termo))
            {
                var tweets = twitterServiceRepository.Search(service, termo + " " + estado);
                categoriaId = categoriaRepository.GetId(termo);
                foreach (var tweet in tweets)
                {
                    postTwitter.NomeUsuario = tweet.User.ScreenName;
                    postTwitter.Texto = tweet.Text;
                    postTwitter.Data = Convert.ToDateTime(tweet.CreatedDate.ToString("d"));
                    postTwitter.CategoriaId = categoriaId;
                    postTwitter.EstadoId = Convert.ToInt32(estadoId);
                    postTwitterRepository.SaveTweets(postTwitter);
                }
            }

            else
            {
                var tweets = twitterServiceRepository.Search(service, termo + " " + estado);
                categoria.Nome = termo;
                categoriaRepository.SaveCategory(categoria);
                categoriaId = categoriaRepository.GetId(termo);

                foreach (var tweet in tweets)
                {
                    postTwitter.NomeUsuario = tweet.User.ScreenName;
                    postTwitter.Texto = tweet.Text;
                    postTwitter.Data = Convert.ToDateTime(tweet.CreatedDate.ToString("d"));
                    postTwitter.CategoriaId = categoriaId;
                    postTwitter.EstadoId = Convert.ToInt32(estadoId);
                    postTwitterRepository.SaveTweets(postTwitter);
                }
            }

            if (Request.IsAjaxRequest())
                return PartialView("Tweets", postTwitterRepository.GetAll().ToPagedList(numeroPagina, tweetsPorPagina));

            return View("Index", postTwitterRepository.GetAll().ToPagedList(numeroPagina, tweetsPorPagina));
        }
Ejemplo n.º 2
0
        public void SaveCategory(Categoria category)
        {

            try
            {
                using (CrawlerDB crawlerDB = new CrawlerDB())
                {
                    contextDB.ConfigureContext(crawlerDB);
                    if (category != null)
                    {
                        crawlerDB.Categorias.Add(category);
                        crawlerDB.SaveChangesAsync();
                    }
                }
            }

            catch (Exception exception)
            {
                throw exception;
            }

            finally
            {
                contextDB = null;
            }
        }