Beispiel #1
0
        public ActionResult ListTopPages(int page)
        {
                try
                {
                    ListPageViewModel m = new ListPageViewModel();
                    m.Page = page;

                    using (UnitOfWork uow = new UnitOfWork())
                    {
                        //Carica gli ultimi 5 POST
                        PageRepository pr = new PageRepository(uow.Current);

                        IList<Page> pages = pr.GetTopPost(5);
                        if (pages != null && pages.Count > 0)
                        {
                            int k = 0;
                            foreach (var p in pages)
                            {
                                PostViewModel pTemp = new PostViewModel();

                                pTemp.Id = p.Id;
                                pTemp.Data = p.Date.Value;
                                pTemp.Testo = p.BodyText;
                                pTemp.Titolo = p.Title;
                                pTemp.Autore = String.Format("{0} {1}", p.Author.Name, p.Author.Surname);
                                pTemp.Categoria = p.Category.Name;
                                pTemp.IdCategoria = p.Category.Id;
                                pTemp.ImageName = p.ImageName;
                                pTemp.Visualizzazioni = p.Views;
                                pTemp.NumComments = p.Comments.Count;

                                m.PostPreview.Add(pTemp);

                                k++;
                            }
                        }
                        else
                        {
                            PostViewModel pTemp = new PostViewModel();

                            pTemp.Id = 0;
                            pTemp.Data = DateTime.Today;
                            pTemp.Titolo = "OOPS...";
                            pTemp.Testo = "Sembra non siano presenti articoli...";
                            pTemp.Autore = "Pasquale Garzillo";

                            m.PostPreview.Add(pTemp);
                        }

                        //Carica le ultime 5 categoria con maggiori post
                        CategoryRepository cr = new CategoryRepository(uow.Current);
                        IList<Category> tempCats = cr.GetTopCategoryByPostCount(3);
                        if (tempCats != null && tempCats.Count > 0)
                        {
                            foreach (var c in tempCats)
                            {
                                CategoryViewModel cvTemp = new CategoryViewModel();

                                cvTemp.Id = c.Id;
                                cvTemp.Nome = c.Name;
                                cvTemp.Descrizione = c.Description;
                                cvTemp.FileName = c.ImageName;

                                m.CategoriesPreview.Add(cvTemp);
                            }
                        }
                        else
                        {
                            CategoryViewModel cvTemp = new CategoryViewModel();

                            cvTemp.Id = 0;
                            cvTemp.Nome = "OOPS...";
                            cvTemp.Descrizione = "Sembra non siano presenti categorie...";

                            m.CategoriesPreview.Add(cvTemp);
                        }


                        pages = pr.GetPostByViews(page, 10);
                        int totalPages = pr.GetNumberOfTotalPages();
                        if (pages != null && pages.Count > 0)
                        {
                            m.TotalPages = totalPages;
                            foreach (var p in pages)
                            {
                                PostViewModel pTemp = new PostViewModel();

                                pTemp.Id = p.Id;
                                pTemp.Data = p.Date.Value;
                                pTemp.Testo = p.BodyText;
                                pTemp.Titolo = p.Title;
                                pTemp.Autore = String.Format("{0} {1}", p.Author.Name, p.Author.Surname);
                                pTemp.Categoria = p.Category.Name;
                                pTemp.IdCategoria = p.Category.Id;
                                pTemp.ImageName = p.ImageName;
                                pTemp.Visualizzazioni = p.Views;
                                pTemp.NumComments = p.Comments.Count;

                                m.Posts.Add(pTemp);
                            }
                        }
                    }

                    return View(m);
                }
                catch (Exception ex)
                {
                    return Error(ex);
                }
        }