Exemple #1
0
        public ActionResult MagazineNews(Int32 id)
        {
            var user     = UserService.GetCurrentUser();
            var relation = UserService.UserInMagazine(id, user.UserId);

            if (!relation || !ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Magazines"));
            }

            Request.Cookies.Remove("MagazineId");
            SetCookie("MagazineId", id, true);

            if (Request.Cookies["MagazineId"].Value == null)
            {
                SetMessage("Lo sentimos, ha ocurrido un error. Inténtelo de nuevo.", BootstrapAlertTypes.Danger); return(RedirectToAction("Index", "Magazines"));
            }

            var magazine = MagazineService.GetMagazineById(id);

            if (magazine == null)
            {
                SetMessage("Lo sentimos, la revista no existe. Inténtelo de nuevo con una distinta.", BootstrapAlertTypes.Danger);
                return(RedirectToAction("Index", "Magazines"));
            }

            var mostUsedCat   = MagazineService.MostUsedCat(id);
            var mostViewedCat = MagazineService.MostViewedCat(id);

            var mostUsedCatName = "Ninguna";

            if (mostUsedCat != null)
            {
                mostUsedCatName = mostUsedCat.Name;
            }

            var mostViewedCatName = "Ninguna";

            if (mostViewedCat != null)
            {
                mostViewedCatName = mostViewedCat.Name;
            }

            var mostViewedCatCount = 0;

            if (mostViewedCat != null)
            {
                mostViewedCatCount = MagazineService.GetVisitsByCategoryId(mostViewedCat.CategoryId);
            }

            var mostUsedCatCount = 0;

            if (mostUsedCat != null)
            {
                mostUsedCatCount = MagazineService.MostUsedCatCount(mostUsedCat.CategoryId);
            }

            var newsAndVisits = MagazineService.GetNewsWithVisitsByMagazineId(id);

            var viewsCounts = 0;

            if (newsAndVisits != null)
            {
                viewsCounts = newsAndVisits.TotalVisits;
            }

            var newsCount = 0;

            if (newsAndVisits != null)
            {
                newsCount = newsAndVisits.TotalNews;
            }

            var model = new MagazineIndexViewModel
            {
                MagazineId         = id,
                MostUsedCat        = mostUsedCatName,
                MostViewedCat      = mostViewedCatName,
                MostViewedCatCount = mostViewedCatCount,
                MostUsedCatCount   = mostUsedCatCount,
                ViewsCount         = viewsCounts,
                NewsCount          = newsCount,
            };
            //TOP 10 STATISTICS

            var GetNews = MagazineService.GetNewsByMagazineId(id);

            var top10Count = new List <Last10CountModel>();

            foreach (var item in GetNews)
            {
                var addNew = new Last10CountModel
                {
                    NewsId   = item.NewsId,
                    NewTitle = item.Title,
                    Count    = (item.VisitsCount ?? 0)
                };

                top10Count.Add(addNew);
            }

            var top10Visited    = top10Count.OrderByDescending(x => x.Count).Take(10);
            var last10Published = top10Count.Take(10);

            ViewBag.Top10Visited    = JsonConvert.SerializeObject(top10Visited);
            ViewBag.Last10Published = JsonConvert.SerializeObject(last10Published);

            //REPORTES
            var reportsList = MagazineService.GetMyReports(id);

            var reportList = new List <ReportViewModel>();

            foreach (var item in reportsList)
            {
                var report = new ReportViewModel
                {
                    ReportId  = item.MediaId,
                    Impact    = MagazineService.getProm(item.MediaId),
                    MediaName = MagazineService.getMedioName(item.MediaId)
                };

                reportList.Add(report);
            }

            ViewBag.Reports = null;
            if (reportsList.Count > 0)
            {
                ViewBag.Reports = reportList;
            }

            ViewBag.JsonReports = JsonConvert.SerializeObject(reportList);

            return(View(model));
        }