//Загрузка главной страницы public ActionResult Index() { IPostsContext postsContext = new PostsContext(); IUsersContext usersContext = new UsersContext(); ICategoriesContext catContext = new CategoriesContext(); IEnumerable<Post> posts = postsContext.GetAllPosts(); List<Categories> categories = new List<Categories>(); if (catContext.GetAllCategories() != null) { categories = catContext.GetAllCategories().ToList(); ViewBag.categories = categories; } ViewBag.usersContext = usersContext; ViewBag.catContext = catContext; User currUser = new User(); if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false) currUser = usersContext.GetUserByLogin(Session["login"].ToString()); else currUser = null; ViewBag.currUser = currUser; IPostImageContext pic = new PostImageContext(); List<Image> postImageList = new List<Image>(); if (posts != null) { foreach (var p in posts) { postImageList.Add(pic.GetImageByPostId(p.id)); } } if (postImageList != null) ViewBag.postImageList = postImageList; return View(); }
//Поиск среди статей по ключевым словам public List<Post> SearchPosts(string words) { string __words = words.Trim(); string[] _words = __words.Split(' ', ',', '.'); List<Post> foundPosts = new List<Post>(); IPostsContext postsContext = new PostsContext(); IEnumerable<Post> posts = postsContext.GetAllPosts(); foreach (var u in posts) { foreach (var w in _words) if (u.title.ToLower().Contains(w.ToLower()) || u.text.Contains(w)) foundPosts.Add(u); } return foundPosts; }
public JsonResult GetPosts() { IPostsContext postsContext = new PostsContext(); IPostImageContext postImageContext = new PostImageContext(); IUsersContext usersContext = new UsersContext(); ICategoriesContext catContext = new CategoriesContext(); IEnumerable<Post> posts = postsContext.GetAllPosts(); ViewBag.usersContext = usersContext; ViewBag.catContext = catContext; ViewBag.posts = posts.ToList(); User currUser = new User(); if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false) currUser = usersContext.GetUserByLogin(Session["login"].ToString()); else currUser = null; List<Post> sortedPosts = new List<Post>(); int category = Convert.ToInt32(Request.Form["category"]); int startFrom = Convert.ToInt32(Request.Form["startFrom"]); sortedPosts = postsContext.GetPostsByUploading(startFrom, category); List<HTPosts> htPosts = new List<HTPosts>(); foreach (var p in sortedPosts) { HTPosts hp; hp.id = p.id; hp.user_id = p.id_user; if (currUser == null) hp.currUser_id = 0; else hp.currUser_id = currUser.id; hp.title = p.title; hp.text = p.text; hp.category = catContext.GetCategoryById(p.category_id).value; if (postImageContext.GetImageByPostId(p.id) != null) hp.image_name = postImageContext.GetImageByPostId(p.id).image_path; else hp.image_name = ""; hp.userName = usersContext.GetUserById(p.id_user).name; hp.userLastName = usersContext.GetUserById(p.id_user).last_name; htPosts.Add(hp); } JsonResult data = Json(htPosts); return data; }