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(); } }
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)); } }
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 ActionResult Index() { //Hien thi danh muc tin 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)) { 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(); cate.SetConnection(connection); cate.id = id; cate.Load(); categories.Add(cate); } ViewBag.categories = categories; return(View()); } 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()); } }