//Загрузка главной страницы 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 ActionResult UserProfile() // Загрузка представленя страницы пользователя { if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account"); IUsersContext _user = new UsersContext(); IPostsContext postsContext = new PostsContext(); IUserImageContext userImageContext = new UserImageContext(); ICategoriesContext catContext = new CategoriesContext(); List<Categories> categories = new List<Categories>(); if (catContext.GetAllCategories() != null) { categories = catContext.GetAllCategories().ToList(); ViewBag.categories = categories; } ViewBag.catContext = catContext; int s = 0; if (Request.QueryString.Count == 0) s = 0; else s = Convert.ToInt32(Request.QueryString["user"]); if (s == null || s == 0) s = _user.GetUserByLogin(Convert.ToString(Session["login"])).id; User user = new User(); user = _user.GetUserById(s); ViewBag.login = user.login; ViewBag.user = user; if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false) ViewBag.sessionUser = _user.GetUserByLogin(Session["login"].ToString()); IEnumerable<Post> userPosts; userPosts = postsContext.GetPostsByUserId(user.id); IPostImageContext pic = new PostImageContext(); List<Image> postImageList = new List<Image>(); Image userImage = new Image(); userImage = userImageContext.GetImageByUserId(user.id); if (userImage != null) ViewBag.userImage = userImage.image_path; else ViewBag.userImage = "default.png"; if (userPosts != null) { foreach (var p in userPosts) { postImageList.Add(pic.GetImageByPostId(p.id)); } } ViewBag.posts = userPosts; if (postImageList != null) ViewBag.postImageList = postImageList; return View(); }
public ActionResult PostPage() { if (Request.QueryString["post"] == null) RedirectToAction("Index", "Home"); ICategoriesContext catContext = new CategoriesContext(); IPostsContext postsContext = new PostsContext(); IPostsCommentsContext postsCommentsContext = new PostsCommentsContext(); IUsersContext usersContext = new UsersContext(); User currentUser = new User(); if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false) currentUser = usersContext.GetUserByLogin(Session["login"].ToString()); else currentUser = null; ViewBag.currentUser = currentUser; ViewBag.usersContext = usersContext; Post post = new Post(); if (Request.QueryString["post"] == null) return RedirectToAction("Index", "Home"); post = postsContext.GetPostById(Convert.ToInt32(Request.QueryString["post"])); if (post == null) return RedirectToAction("Index", "Home"); User postUser = usersContext.GetUserById(post.id_user); ViewBag.postUser = postUser; ViewBag.categories = catContext.GetAllCategories().ToList(); ViewBag.catContext = catContext; IPostImageContext postImageContext = new PostImageContext(); if (postImageContext.GetImageByPostId(post.id) != null) ViewBag.postImage = postImageContext.GetImageByPostId(post.id).image_path; ViewBag.post = post; IEnumerable<PostsComments> postComments = postsCommentsContext.GetPostsCommentsByPostId(post.id); int page = 1; if (Request.QueryString["page"] != null) page = Convert.ToInt32(Request.QueryString["page"]); if (postComments != null) { List<PostsComments> _postsComments = new List<PostsComments>(); _postsComments = postComments.ToList(); List<PostsComments> currComments = new List<PostsComments>(Config.pageItems); int pagination = GetPagination(_postsComments.Count); int start = 0, end = 0; start = (page - 1) * Config.pageItems; if (_postsComments.Count == 1) end = 0; else if (_postsComments.Count < Config.pageItems) end = _postsComments.Count - 1; else if (page == pagination && _postsComments.Count % Config.pageItems > 0) end = _postsComments.Count - 1; else end = page * Config.pageItems - 1; for (int i = start; i <= end; i++) { currComments.Add(_postsComments[i]); } ViewBag.currentPage = page; ViewBag.pagination = pagination; ViewBag.postComments = currComments; } else { page = 0; } return View(); }
public ActionResult EditPost() { IPostsContext postsContext = new PostsContext(); IPostImageContext postImageContext = new PostImageContext(); IUsersContext usersContext = new UsersContext(); Post post = new Post(); string url = "~/Post/PostPage?post="; post = postsContext.GetPostById(Convert.ToInt32(Request.QueryString["post"])); ViewBag.post = post; if (post.id_user != usersContext.GetUserByLogin(Session["login"].ToString()).id) return Redirect(url + post.id); ICategoriesContext catContext = new CategoriesContext(); List<Categories> categories = new List<Categories>(); categories = catContext.GetAllCategories().ToList(); ViewBag.categories = categories; return View(); }