Пример #1
0
        public IQsiTreeNode Parse(QsiScript script, CancellationToken cancellationToken = default)
        {
            var parser = MySQLUtility.CreateParser(script.Script, _version);
            var query  = parser.query();

            if (query.children[0] is not SimpleStatementContext simpleStatement)
            {
                return(null);
            }

            switch (simpleStatement.children[0])
            {
            case SelectStatementContext selectStatement:
                return(TableVisitor.VisitSelectStatement(selectStatement));

            case CreateStatementContext createStatement when
                createStatement.children[1] is CreateViewContext createView:
                return(TableVisitor.VisitCreateView(createView));

            case DeleteStatementContext deleteStatement:
                return(ActionVisitor.VisitDeleteStatement(deleteStatement));

            case ReplaceStatementContext replaceStatement:
                return(ActionVisitor.VisitReplaceStatement(replaceStatement));

            case UpdateStatementContext updateStatement:
                return(ActionVisitor.VisitUpdateStatement(updateStatement));

            case InsertStatementContext insertStatement:
                return(ActionVisitor.VisitInsertStatement(insertStatement));

            default:
                throw TreeHelper.NotSupportedTree(simpleStatement.children[0]);
            }
        }
Пример #2
0
        public ActionResult Approve(int?id)
        {
            if (id == null)
            {
                ViewBag.ErrorMessage = "Trang này không tồn tại";
                return(View("_errors"));
            }

            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.INSPECTOR))
                {
                    Post post = new Post(connection);
                    post.id = (int)id;

                    if (post.LoadPost())
                    {
                        post.inspector_id = user.id;
                        post.Approve();
                        TempData["SuccessMessage"] = "Đã duyệt thành công";
                        return(RedirectToAction("Index", "Posts", new { id = post.id }));
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Trang này không tồn tại";
                        return(View("_errors"));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền thực hiện thao tác này";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #3
0
        public ActionResult ChangePassword()
        {
            MySQLUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();

                Authenticate authenticate = new Authenticate(connection);

                User user = authenticate.GetUser();

                if (user.IsLogin())
                {
                    return(View(user));
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn chưa đăng nhập không thể thay đổi mật khẩu";
                    return(View("_errors"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #4
0
        public ActionResult Index()
        {
            MySQLUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();
                Authenticate auth = new Authenticate(connection);

                User user = auth.GetUser();

                if (user.IsLogin())
                {
                    return(View(user));
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn chưa đăng nhập";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #5
0
        public ActionResult Register()
        {
            MySQLUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();
                Authenticate auth = new Authenticate(connection);

                User user = auth.GetUser();

                if (user.IsLogin())
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    return(View());
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #6
0
        public ActionResult Index()
        {
            IDatabaseUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();
                CategoryListModel categorylist = new CategoryListModel(connection);

                List <Category> categories = categorylist.GetAll();

                List <List <Post> > categoryposts = new List <List <Post> >();

                foreach (Category category in categories)
                {
                    PostListModel postlist = new PostListModel(connection);
                    List <Post>   posts    = postlist.GetByCategoryLimit(category.link, 0, 5, "created_time DESC");
                    categoryposts.Add(posts);
                }

                ViewBag.categories = categories;
                return(View(categoryposts));
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
        }
Пример #7
0
        public ActionResult Add()
        {
            MySQLUtility connection = new MySQLUtility();

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


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

            if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
            {
                return(View());
            }
            else
            {
                ViewBag.Title = "Bạn không thể truy cập trang này";
                return(View("_Error"));
            }
        }
Пример #8
0
        public ActionResult Login(User input)
        {
            IDatabaseUtility connection = new MySQLUtility();

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

            User user = new Authenticate(connection).GetUser();

            if (user.IsLogin())
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                if (input.username != null && input.password != null)
                {
                    string username = input.username;
                    string password = input.password;
                    try
                    {
                        input.SetConnection(connection);
                        if (input.Login(input.username, input.password))
                        {
                            HttpCookie cusername = new HttpCookie("username", username);
                            cusername.HttpOnly = true;
                            cusername.Expires  = DateTime.Now.AddMonths(1);
                            Response.Cookies.Add(cusername);
                            HttpCookie cpassword = new HttpCookie("password", password);
                            cpassword.HttpOnly = true;
                            cpassword.Expires  = DateTime.Now.AddMonths(1);
                            Response.Cookies.Add(cpassword);
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            ViewBag.error = "Tên đăng nhập hoặc tài khoản không đúng";
                            return(View());
                        }
                    }catch (DBException e)
                    {
                        ViewBag.error = e.Message;
                        return(View("~/Views/Shared/_Error.cshtml"));
                    }
                }
                else
                {
                    ViewBag.error = "Tên đăng nhập hoặc tài khoản không hợp lệ";
                    return(View());
                }
            }
        }
Пример #9
0
        public ActionResult Register(User input, string[] password)
        {
            MySQLUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();

                Authenticate auth = new Authenticate(connection);
                User         user = auth.GetUser();

                if (user.IsLogin())
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    input.SetConnection(connection);
                    if (password == null || password.Length != 2 || password[0] != password[1])
                    {
                        input.AddErrorMessage("retypepassword", "Nhập lại mật khẩu không trùng khớp!");
                        input.password = password[0];
                    }

                    if (!input.CheckValid())
                    {
                        throw new InputException(1, input.GetErrorsMap());
                    }

                    if (input.Register())
                    {
                        HttpCookie cusername = new HttpCookie("username", input.username);
                        cusername.Expires  = DateTime.Now.AddMonths(1);
                        cusername.HttpOnly = true;
                        HttpCookie cpassword = new HttpCookie("password", input.password);
                        cpassword.Expires  = DateTime.Now.AddMonths(1);
                        cpassword.HttpOnly = true;

                        Response.Cookies.Add(cusername);
                        Response.Cookies.Add(cpassword);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        throw new InputException(1, null, "Đăng ký tài khoản thất bại. Vui lòng thử lại!");
                    }
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_Error"));
            }catch (InputException e)
            {
                ViewBag.ErrorMessage = e.Message;
                ViewBag.ErrorsMap    = e.Errors;
                return(View(input));
            }
        }
Пример #10
0
        public ActionResult Add()
        {
            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))
                {
                    List <int> ids = new List <int>();
                    using (MySqlDataReader result = (MySqlDataReader)connection.select("*").from("category").Execute())
                    {
                        while (result.Read())
                        {
                            ids.Add(result.GetInt32("id"));
                        }
                    }

                    ViewBag.categories = new List <Category>();

                    foreach (int id in ids)
                    {
                        Category cate = new Category(connection);
                        cate.id = id;
                        cate.Load();
                        ViewBag.categories.Add(cate);
                    }
                    return(View());
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền truy cập vào đây";
                    return(View("_errors"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View());
            }
            finally
            {
                connection.Close();
            }
        }
Пример #11
0
        public ActionResult Add(Category category)
        {
            MySQLUtility connection = new MySQLUtility();

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


            Authenticate authenticate = new Authenticate(connection);

            User user = authenticate.GetUser();

            try
            {
                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    //Đặt giá trị cho biến chấp nhận rỗng null => ""
                    category.link = category.link == null ? "" : category.link;
                    category.SetConnection(connection);
                    category.CheckValidForLink().CheckValidForName();
                    if (category.GetErrorsMap().Count == 0)
                    {
                        category.Add();
                        TempData["SuccessMessage"] = "Bạn đã tạo thành công danh mục " + category.name;
                        return(RedirectToAction("Index", "CategoryManage"));
                    }
                    else
                    {
                        throw new InputException(1, category.GetErrorsMap());
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang này";
                    return(View("_Error"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View(category));
            }catch (InputException e)
            {
                ViewBag.ErrorsMap = e.Errors;
                return(View(category));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #12
0
        public ActionResult GrantRole(int?id)
        {
            if (id == null)
            {
                ViewBag.ErrorMessage = "Trang web này không tồn tại!";
                return(View("_errors"));
            }

            IDatabaseUtility connection = new MySQLUtility();

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


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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    User grantUser = new User(connection);
                    grantUser.id = (int)id;
                    if (grantUser.Load())
                    {
                        return(View(grantUser));
                    }
                    else
                    {
                        ViewBag.ErrorMessage("Nguời dùng này không tồn tại!");
                        return(View("_errors"));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền truy cập trang này";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #13
0
        //controller cho việc trình diễn nội dung tin tức
        public ActionResult Index(int?id)
        {
            IDatabaseUtility connection = new MySQLUtility();

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


            try
            {
                if (id == null)
                {
                    ViewBag.ErrorMessage = "Trang tin này không tồn tại!";
                    return(View("_errors"));
                }
                else
                {
                    Post post = new Post(connection);
                    post.id = (int)id;
                    if (post.Load())
                    {
                        if (post.IsShown())
                        {
                            return(View(post));
                        }
                        else
                        {
                            ViewBag.ErrorMessage = "Trang này không tồn tại!";
                            return(View("_errors"));
                        }
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Trang nay không tồn tại!";
                        return(View("_errors"));
                    }
                }
            }
            catch (DBException e)
            {
                ViewBag.ERrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #14
0
        public ActionResult Index(User input)
        {
            MySQLUtility connection = new MySQLUtility();
            User         user       = new User();

            try
            {
                connection.Connect();
                Authenticate authenticate = new Authenticate(connection);

                user = authenticate.GetUser();

                if (user.IsLogin())
                {
                    input.SetConnection(connection);

                    //Thao tác đặt giá trị mặc định
                    //Giá trị cho phép rỗng lastname
                    input.lastname = input.lastname == null ? "" : input.lastname;


                    input.CheckValidForFirstname().CheckValidForLastname().CheckValidForFirstname().CheckValidForPhone().CheckValidForEmail().CheckValidForGender();


                    if (input.GetErrorsMap().Count() != 0)
                    {
                        throw new InputException(1, input.GetErrorsMap());
                    }

                    input.password = Request.Cookies["password"].Value;
                    input.role     = user.role;
                    user.Update(input);

                    ViewBag.SuccessMessage = "Cập nhật thông tin thành công";
                    return(View(user));
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn chưa đăng nhập";
                    return(View("_errors"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View(user));
            }catch (InputException e)
            {
                ViewBag.ErrorsMap = e.Errors;
                return(View(user));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #15
0
        public virtual TViewModel Get(Expression <Func <TViewModel, bool> > whereSelector, string include = "", IEnumerable <OrderByEntity> orderBy = null)
        {
            var item = DAL.Get(MySQLUtility.GetModelExpr <TViewModel, TModel>(whereSelector), include, orderBy);

            if (item != null)
            {
                return(item.MapTo(RetrieveSelector));
            }
            else
            {
                return(default);
Пример #16
0
        public ActionResult Delete(int id)
        {
            MySQLUtility connection = new MySQLUtility();

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


            User user = null;

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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    Category category = new Category(connection);
                    category.id = id;
                    if (!category.Load())
                    {
                        TempData["ErrorMessage"] = "Danh mục không tồn tại";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        category.Delete();
                        TempData["SuccessMessage"] = "Xóa thành công";
                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang này";
                    return(View("_Error"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_Error"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #17
0
        public ActionResult Delete(int?id)
        {
            MySQLUtility connection = new MySQLUtility();

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


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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    Category category = new Category(connection);
                    category.id = (int)id;
                    if (id == null || !category.Load())
                    {
                        TempData["ErrorMessage"] = "ID của danh mục không hợp lệ";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(View(category));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền truy cập";
                    return(View("_error"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_Error"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #18
0
        public ActionResult Index(int page = 1)
        {
            IDatabaseUtility connection = new MySQLUtility();

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



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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    UserListModel list = new UserListModel(connection);

                    ViewBag.users         = list.GetAll((page - 1) * 10, 10);
                    ViewBag.pagepartition = new PagePartitionModel(page, (int)Math.Ceiling(list.GetTotal() / 10.0));

                    return(View());
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền thực hiện thao tác này";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #19
0
        public ActionResult Update(int?id)
        {
            MySQLUtility connection = new MySQLUtility();

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


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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.ADMIN))
                {
                    Category category = new Category(connection);
                    category.id = (int)id;
                    if (id == null || !category.Load())
                    {
                        TempData["ErrorMessage"] = "Danh mục tin tức không tồn tại";
                        return(RedirectToAction("Index"));
                    }
                    ViewBag.oldname = category.name;
                    return(View(category));
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang này";
                    return(View("_Error"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View());
            }
            finally
            {
                connection.Close();
            }
        }
Пример #20
0
        public ActionResult Index(int page = 1)
        {
            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))
                {
                    PostListModel list = new PostListModel(connection);
                    ViewBag.posts = list.GetWhere("journalist_id=" + user.id, (page - 1) * 10, 10);
                    int total = list.GetTotalWhere("journalist_id=" + user.id);
                    ViewBag.pagepartition = new PagePartitionModel("Index", "PostManage", page, (int)Math.Ceiling(total / 10.0));
                    return(View());
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền truy cập";
                    return(View("_errors"));
                }
            }catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #21
0
        public ActionResult ApprovedList(int page = 1)
        {
            IDatabaseUtility connection = new MySQLUtility();

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

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

                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.INSPECTOR))
                {
                    PostListModel list = new PostListModel(connection);
                    ViewBag.posts         = list.GetWhere("valid=1", (page - 1) * 10, 10);
                    ViewBag.pagepartition = new PagePartitionModel("ApprovedList", "Inspector", page, (int)Math.Ceiling(list.GetTotalWhere("valid=1") / 10.0));
                    return(View());
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang này";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = "" + e;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #22
0
        public ActionResult Header()
        {
            try
            {
                MySQLUtility connection = new MySQLUtility();
                connection.Connect();

                Authenticate auth = new Authenticate(connection);

                User user = auth.GetUser();

                ViewBag.user = user;

                List <int> ids = new List <int>();

                using (MySqlDataReader result = (MySqlDataReader)connection.select("*").from("category").Execute())
                {
                    while (result.Read())
                    {
                        ids.Add(result.GetInt32("id"));
                    }
                }

                List <Category> categories = new List <Category>();

                foreach (int id in ids)
                {
                    Category cate = new Category(connection);
                    cate.id = id;
                    cate.Load();
                    categories.Add(cate);
                }

                ViewBag.categories = categories;

                return(PartialView());
            }
            catch (DBException e)
            {
                return(Content("Không thể load heading. Vui lòng tải lại trang web! " + e.Message));
            }
        }
Пример #23
0
        public ActionResult Login()
        {
            IDatabaseUtility connection = new MySQLUtility();

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

            User user = new Authenticate(connection).GetUser();

            if (user.IsLogin())
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(View());
        }
Пример #24
0
        public ActionResult ListByCategory(string category, int page = 1)
        {
            MySQLUtility connection = new MySQLUtility();

            try
            {
                connection.Connect();
                using (IDataReader result = connection.select("*").from("category").where ("link=" + new DBString(category).SqlValue()).Execute()){
                    if (result.Read())
                    {
                        ViewBag.category      = new Category();
                        ViewBag.category.id   = (int)result["id"];
                        ViewBag.category.name = (string)result["name"];
                        ViewBag.category.link = (string)result["link"];
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Danh mục này không tồn tại!";
                        return(View("_errors"));
                    }
                }
                PostListModel      list          = new PostListModel(connection);
                List <Post>        posts         = list.GetByCategoryLimit(category, page, 20, "created_time desc");
                PagePartitionModel pagepartition = new PagePartitionModel("ListByCategory", "PostShow", page, (int)Math.Ceiling(list.GetTotalPageByCategory(category) / 20.0));
                ViewBag.pagepartition = pagepartition;
                return(View(posts));
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
 public virtual int Count(Expression <Func <TViewModel, TForeignModel, bool> > whereSelector, string include = "", bool isAll = true)
 {
     return(DAL.Count(MySQLUtility.GetModelExpr <TViewModel, TModel, TForeignModel>(whereSelector), include, isAll));
 }
Пример #26
0
        public ActionResult Update(int?id)
        {
            if (id == null)
            {
                ViewBag.ErrorMessage = "Trang này không tồn tại!";
                return(View("_errors"));
            }
            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))
                {
                    ViewBag.categories = new CategoryListModel(connection).GetAll();
                    Post post = new Post(connection);
                    post.id = (int)id;
                    if (post.LoadPost())
                    {
                        post.LoadInspector();
                        post.LoadCategory();
                        post.LoadJournalist();
                        post.LoadPoster();
                        ViewBag.oldtitle = post.title;
                        return(View(post));
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Bài viết này không tồn tại hoặc không phải thuộc quyền sở hữu của bạn";
                        return(View("_errors"));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không thể truy cập trang này";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            finally
            {
                connection.Close();
            }
        }
Пример #27
0
        public ActionResult Update(HttpPostedFileBase poster, Post newdata)
        {
            IDatabaseUtility connection = new MySQLUtility();

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



            Post post = new Post(connection);

            try
            {
                Authenticate authenticate = new Authenticate(connection);
                User         user         = authenticate.GetUser();
                if (user.IsLogin() && user.HaveRole(NewsApplication.Models.User.JOURNALIST))
                {
                    ViewBag.categories = new CategoryListModel(connection).GetAll();
                    newdata.SetConnection(connection);
                    post.id = newdata.id;
                    if (post.LoadPost())
                    {
                        post.LoadPoster();
                        if (post.journalist_id == user.id)
                        {
                            newdata.CheckValidForCategoryId().CheckValidForContent().CheckValidForSummary().CheckValidForTitle();

                            if (newdata.GetErrorsMap().Count == 0)
                            {
                                if (post.Update(newdata))
                                {
                                    if (poster != null)
                                    {
                                        string filename = post.poster.path;
                                        post.poster.SetConnection(connection);
                                        if (System.IO.File.Exists(Server.MapPath(filename)))
                                        {
                                            System.IO.File.Delete(Server.MapPath(filename));
                                        }

                                        try
                                        {
                                            PostImage image = new PostImage(connection);
                                            image.post_id = post.id;
                                            image.path    = PostImage.POSTER_IMAGE_DIR + image.post_id + "_" + new Random().Next() + System.IO.Path.GetExtension(poster.FileName);

                                            post.poster.Update(image);
                                            poster.SaveAs(Server.MapPath(image.path));
                                        }
                                        catch (System.Exception e)
                                        {
                                            ViewBag.ErrorMessage = e.Message;
                                            ViewBag.oldtitle     = post.title;
                                            newdata.poster       = post.poster;
                                            return(View(newdata));
                                        }
                                    }
                                    TempData.Add("SuccessMessage", "Bạn đã cập nhật tin tức thành công!");
                                    return(RedirectToAction("Index", "PostManage"));
                                }
                                else
                                {
                                    ViewBag.ErrorMessage = "Cập nhật tin tức không thành công!";
                                    ViewBag.oldtitle     = post.title;
                                    newdata.poster       = post.poster;
                                    return(View(newdata));
                                }
                            }
                            else
                            {
                                throw new InputException(1, newdata.GetErrorsMap());
                            }
                        }
                        else
                        {
                            ViewBag.ErrorMessage = "Tin tức này không thuộc quyền sở hữu của bạn";
                            return(View("_errors"));
                        }
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Tin tức này không tồn tại!";
                        return(View("_errors"));
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Bạn không có quyền truy cập vào đây";
                    return(View("_errors"));
                }
            }
            catch (DBException e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("_errors"));
            }
            catch (InputException e)
            {
                ViewBag.ErrorsMap = e.Errors;
                ViewBag.oldtitle  = post.title;
                newdata.poster    = post.poster;
                return(View(newdata));
            }
            finally
            {
                connection.Close();
            }
        }
 public virtual List <TViewModel> GetList(Expression <Func <TViewModel, TForeignModel, bool> > whereSelector, int pageSize, int pageNum, string include = "", IEnumerable <OrderByEntity> orderBy = null)
 {
     return(DAL.GetList(MySQLUtility.GetModelExpr <TViewModel, TModel, TForeignModel>(whereSelector), pageSize, pageNum, include, orderBy).MapAllTo(RetrieveSelector).ToList());
 }
Пример #29
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();
            }
        }
 public virtual int Count(WhereQueryable <TViewModel, TForeignModel> whereSelector, string include = "", bool isAll = true)
 {
     return(DAL.Count(MySQLUtility.GetModelExpr <TViewModel, TModel, TForeignModel>(whereSelector.Expr), include, isAll));
 }