示例#1
0
        public ActionResult PageRemoveImage(int id)
        {
            try
            {
                using (UnitOfWork uow = new UnitOfWork())
                {
                    PageRepository pr = new PageRepository(uow.Current);

                    Page p = pr.GetById(id);

                    if (p != null)
                    {
                        p.SetImagePath(null);

                        pr.SaveOrUpdate(p);
                        uow.Commit();

                        return RedirectToAction("PageDetail", new { id = p.Id });
                    }
                    else
                    {
                        return Error("Si è verificato un errore durante la rimozione dell'immagine");
                    }
                }
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
示例#2
0
        //
        // GET: /Page/

        public ActionResult Index(string page, int id)
        {
            try 
            {
                DetailPageViewModel m = new DetailPageViewModel();
                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);
                    }

                    if (id != 0)
                    {
                        Page p = pr.GetById(id);

                        p.AddView();
                        pr.SaveOrUpdate(p);
                        uow.Commit();

                        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.Tags = p.Tags!=null && p.Tags.Count>0 ? p.Tags.Select(x => x.Name).ToList() : null;
                        pTemp.Visualizzazioni = p.Views;
                        pTemp.NumComments = p.Comments.Count;

                        if (pTemp.Tags != null && pTemp.Tags.Count > 0)
                        {
                            var tags = new TagCloudAnalyzer()
                                     .ComputeTagCloud(pTemp.Tags);
                            pTemp.TagCloud = tags;
                        }

                        if (p.Comments != null && p.Comments.Count > 0)
                        {
                            pTemp.Comments = (from c in p.Comments
                                              select new CommentViewModel() { UserMail = c.UserMail, TextComment = c.TextComment, UserName = c.UserName }).ToList<CommentViewModel>();
                        }


                        m.DetailedPost = 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);
                    }
                }
                return View(m);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
示例#3
0
        public ActionResult PageNew(PageViewModel model) 
        {
            try
            {
                //Carica tutti gli elementi necessari a video
                using (UnitOfWork uow = new UnitOfWork())
                {
                    AuthorRepository ar = new AuthorRepository(uow.Current);
                    BlogRepository br = new BlogRepository(uow.Current);
                    CategoryRepository cr = new CategoryRepository(uow.Current);

                    //Ricarica la lista autori
                    IList<Author> tmpAuthors = ar.FindAll().ToList();
                    if (tmpAuthors != null && tmpAuthors.Count > 0)
                    {
                        IEnumerable<SelectListItem> tmpAuthorsItems;

                        tmpAuthorsItems = from s in tmpAuthors
                                          select new SelectListItem
                                          {
                                              Text = s.NameAndSurname,
                                              Value = s.Id.ToString()
                                          };

                        model.Authors = tmpAuthorsItems;
                    }

                    IList<Blog.Model.Domain.Entities.Blog> tmpBlogs = br.FindAll().ToList();
                    if (tmpBlogs != null && tmpBlogs.Count > 0)
                    {
                        IEnumerable<SelectListItem> tmpBlogsItems;

                        tmpBlogsItems = from b in tmpBlogs
                                        select new SelectListItem
                                        {
                                            Text = b.Name,
                                            Value = b.Id.ToString()
                                        };

                        model.Blogs = tmpBlogsItems;
                    }

                    IList<Category> tmpCategories = cr.FindAll().ToList();
                    if (tmpCategories != null && tmpCategories.Count > 0)
                    {
                        IEnumerable<SelectListItem> tmpCategoriesItems;

                        tmpCategoriesItems = from b in tmpCategories
                                             select new SelectListItem
                                             {
                                                 Text = b.Name,
                                                 Value = b.Id.ToString()
                                             };

                        model.Categories = tmpCategoriesItems;
                    }
                }

                if (ModelState.IsValid)
                {
                    using (UnitOfWork uow = new UnitOfWork())
                    {
                        string fileName = null;
                        if (model.File != null && model.File.ContentLength > 0)
                        {
                            //SALVA IL FILE
                            fileName = Path.GetFileName(model.File.FileName);
                            var path = Path.Combine(Server.MapPath("/Uploads"), fileName);
                            model.File.SaveAs(path);
                            model.FileName = fileName;
                        }

                        model.SelectedAuthor = model.SelectedAuthor;
                        model.SelectedBlog = model.SelectedBlog;
                        model.SelectedCategory = model.SelectedCategory;

                        PageRepository pr = new PageRepository(uow.Current);
                        AuthorRepository ar = new AuthorRepository(uow.Current);
                        BlogRepository br = new BlogRepository(uow.Current);
                        CategoryRepository cr = new CategoryRepository(uow.Current);
                        TagRepository tr = new TagRepository(uow.Current);

                        Author au = ar.GetById(Convert.ToInt32(model.SelectedAuthor));
                        Blog.Model.Domain.Entities.Blog bb = br.GetById(Convert.ToInt32(model.SelectedBlog));
                        Category cc = cr.GetById(Convert.ToInt32(model.SelectedCategory));

                        Page p = new Page(model.Titolo, model.Descrizione, model.Data, model.Body, au, bb, cc);

                        if (!String.IsNullOrEmpty(model.Tags))
                        {
                            foreach(var t in model.Tags.Split(','))
                            {
                                if (!String.IsNullOrEmpty(t))
                                {
                                    Tag tg = tr.GetTagByName(t.TrimStart().TrimEnd());
                                    if (tg != null)
                                    {
                                        p.AddTag(tg);
                                    }
                                    else
                                    {
                                        Tag tempTag = new Tag(t.TrimStart().TrimEnd());
                                        p.AddTag(tempTag);
                                    }
                                }
                            }
                        }

                        if (!String.IsNullOrEmpty(fileName))
                            p.SetImagePath(fileName);
                        else
                            model.FileName = p.ImageName;

                        pr.SaveOrUpdate(p);
                        uow.Commit();

                        model.Message = "Salvataggio eseguito con successo!";
                    }
                }
                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }