Esempio n. 1
0
        public ActionResult Add(Post post, HttpPostedFileBase poster)
        {
            IDatabaseUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }



            try
            {
                Authenticate authenticate = new Authenticate(connection);
                User         user         = authenticate.GetUser();

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.JOURNALIST))
                {
                    if (poster == null)
                    {
                        post.AddErrorMessage("poster", "Bạn chưa chọn ảnh bìa cho tin tức");
                    }
                    post.SetConnection(connection);
                    ViewBag.categories = new CategoryListModel(connection).GetAll();

                    post.CheckValidForSummary().CheckValidForContent().CheckValidForTitle().CheckValidForCategoryId();

                    if (post.GetErrorsMap().Count() == 0)
                    {
                        PostImage image = new PostImage(connection);
                        post.valid         = 0;
                        post.journalist_id = user.id;
                        post.Add();
                        image.post_id = (int)connection.GetLastInsertedId();
                        image.path    = PostImage.POSTER_IMAGE_DIR + image.post_id + "_" + new Random().Next() + System.IO.Path.GetExtension(poster.FileName);
                        image.Add();

                        poster.SaveAs(Server.MapPath(image.path));
                        TempData["SuccessMessage"] = "Bạn đã đăng bài thành công hãy tìm nhà kiểm duyệt để duyệt bài của bạn và hiển thị nó";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ViewBag.postback = post;
                        throw new InputException(1, post.GetErrorsMap());
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang web này";
                    return(View("_errors"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = "" + e.Message;
                return(View("_errors"));
            }catch (InputException e)
            {
                ViewBag.ErrorsMap = e.Errors;
                return(View());
            }
            finally
            {
                connection.Close();
            }
        }