Example #1
0
        public ActionResult AddArticle( Article article, HttpPostedFileBase picture, int categoryId )
        {
            if( ModelState.IsValid )
            {

                if( picture != null )
                {
                    if( picture.ContentType == "image/jpeg" || picture.ContentType == "image/png" )
                    {
                        Image image = Image.FromStream( picture.InputStream );

                        if( image.Height > 200 || image.Width > 200 )
                        {
                            Image small = Class.ImageHelper.ScaleImage( image, 200, 200 );
                            Bitmap b = new Bitmap( small );

                            Guid guid = Guid.NewGuid();
                            string imageName = guid.ToString() + ".jpg";

                            b.Save( Server.MapPath( "~/uploads/aticleImage/" + imageName ), ImageFormat.Jpeg );
                            b.Save( Server.MapPath( "~/uploads/articleImage/" + imageName ), ImageFormat.Jpeg );

                            small.Dispose();
                            b.Dispose();

                            article.ImageName = imageName;
                        }
                        else
                        {
                            picture.SaveAs( Server.MapPath( "~/uploads/Article/" + picture.FileName ) );
                        }
                    }
                }
                ArticleCategoryDao articleCategoryDao = new ArticleCategoryDao();

                ArticleCategory articleCategory = articleCategoryDao.GetById( categoryId );
                article.Category = articleCategory;
                article.PostDate = DateTime.Now;
                article.User = new BlogUserDao().GetByLogin(User.Identity.Name);
                ArticleDao articleDao = new ArticleDao();

                articleDao.Create( article );

                TempData["message-success"] = "Kniha byla pridana";

            }
            else
            {
                return View( "Create", article );
            }

            return RedirectToAction( "Index" );
        }
Example #2
0
        public ActionResult UpdateArticle( Article article, HttpPostedFileBase picture, int categoryId )
        {
            try
            {
                ArticleDao articleDao = new ArticleDao();
                ArticleCategoryDao articleCategoryDao = new ArticleCategoryDao();

                ArticleCategory articleCategory = articleCategoryDao.GetById( categoryId );
                BlogUser user = new BlogUserDao().GetByLogin(User.Identity.Name);

                article.Category = articleCategory;
                article.User = user;

                if( picture != null )
                {
                    if( picture.ContentType == "image/jpeg" || picture.ContentType == "image/png" )
                    {
                        Image image = Image.FromStream( picture.InputStream );

                        Guid guid = Guid.NewGuid();
                        string imageName = guid.ToString() + ".jpg";

                        if( image.Height > 200 || image.Width > 200 )
                        {
                            Image small = Class.ImageHelper.ScaleImage( image, 200, 200 );
                            Bitmap b = new Bitmap( small );

                            b.Save( Server.MapPath( "~/uploads/articleImage/" + imageName ), ImageFormat.Jpeg );

                            small.Dispose();
                            b.Dispose();

                        }
                        else
                        {
                            picture.SaveAs( Server.MapPath( "~/uploads/articleImage/" + picture.FileName ) );
                        }

                        System.IO.File.Delete( Server.MapPath( "~/uploads/articleImage/" + article.ImageName ) );
                        article.ImageName = imageName;
                    };
                }

                articleDao.Update( article );

                TempData["message-success"] = "Kniha" + article.Title + "byla upravena.";
            }
            catch( Exception )
            {

                throw;
            }

            return RedirectToAction( "Index", "Article" );
        }
        public ActionResult AddArticle(Article article, HttpPostedFileBase picture, int categoryId)
        {
            if (ModelState.IsValid)
            {
                 IList<Article> art = new ArticleDao().GetAll();

                foreach (Article a in art)
                {
                    if (a.Title == article.Title)
                    {
                        TempData["message-unsuccess"] = "Nadpis už existuje";
                        IList<ArticleCategory> categories = new ArticleCategoryDao().GetAll();
                        ViewBag.Category = categories;
                        return View("CreateArticle", article);
                    }

                }

                if (picture != null)
                {
                    if (picture.ContentType == "image/jpeg" || picture.ContentType == "image/png")
                    {
                        Image image = Image.FromStream(picture.InputStream);

                        if (image.Height > 200 || image.Width > 200)
                        {
                            Image small = Helper.ImageHelper.ScaleImage(image, 200, 200);
                            Bitmap b = new Bitmap(small);

                            Guid guid = Guid.NewGuid();
                            string imageName = guid.ToString() + ".jpg";

                            b.Save(Server.MapPath("~/image/aticleImage/" + imageName), ImageFormat.Jpeg);
                            b.Save(Server.MapPath("~/image/articleImage/" + imageName), ImageFormat.Jpeg);

                            small.Dispose();
                            b.Dispose();

                            article.ImageName = imageName;
                        }
                        else
                        {
                            picture.SaveAs(Server.MapPath("~/image/articleImage/" + picture.FileName));
                        }
                    }
                }

                ArticleCategoryDao articleCategoryDao = new ArticleCategoryDao();

                ArticleCategory articleCategory = articleCategoryDao.GetById(categoryId);
                article.Category = articleCategory;
                article.PosteDate = DateTime.Now;
                article.User = new UserDao().GetByLogin(User.Identity.Name);
                ArticleDao articleDao = new ArticleDao();

                articleDao.Create(article);

                TempData["message-success"] = "Článek byl přidán";

            }
            else
            {
                IList<ArticleCategory> categories = new ArticleCategoryDao().GetAll();
                ViewBag.Category = categories;
                return View("CreateArticle", article);
            }

            return RedirectToAction("Index");
        }