public ActionResult Edit(int id) { PostsRepository repo = new PostsRepository(); Post item = repo.GetById(id); EditVM model = new EditVM(item); model.SubRedditsList = new List <SelectListItem>(); SubRedditsRepository subRedditsRepo = new SubRedditsRepository(); List <SubReddit> subscribedSubReddits = subRedditsRepo.GetMySubscribes(AuthManager.LoggedUser.Id).ToList(); foreach (SubReddit subReddit in subscribedSubReddits) { if (!(AuthManager.LoggedUser.BannedInSubReddits.Any(sr => sr.BannedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id)) || AuthManager.LoggedUser.MutedInSubReddits.Any(sr => sr.MutedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id)))) { model.SubRedditsList.Add( new SelectListItem() { Value = subReddit.Id.ToString(), Text = subReddit.Name, Selected = subReddit.Id == model.SelectedSubReddit }); } } //model.SubRedditId = model.SelectedSubReddit; return(PartialView("~/Views/Partials/Edits/_EditPost.cshtml", model)); }
public ActionResult Create() { EditVM model = new EditVM(); model.SubRedditsList = new List <SelectListItem>(); model.UserId = AuthManager.LoggedUser.Id; SubRedditsRepository subRedditsRepo = new SubRedditsRepository(); List <SubReddit> subscribedSubReddits = subRedditsRepo.GetMySubscribes(AuthManager.LoggedUser.Id).ToList(); foreach (SubReddit subReddit in subscribedSubReddits) { model.SubRedditsList.Add( new SelectListItem() { Value = subReddit.Id.ToString(), Text = "r/" + subReddit.Name, Selected = subReddit.Id == model.SelectedSubReddit, Disabled = (((AuthManager.LoggedUser.BannedInSubReddits.Any(sr => sr.Id == subReddit.Id && sr.BannedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id))) || (AuthManager.LoggedUser.MutedInSubReddits.Any(sr => sr.Id == subReddit.Id && sr.MutedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id))))) ? true : false }); } return(PartialView("~/Views/Partials/Edits/_CreatePost.cshtml", model)); }
public ActionResult Index(IndexVM model, int?SubRedditId) { if (SubRedditId != null) { model.SubRedditId = (int)SubRedditId; } PostsRepository repo = new PostsRepository(); if (AuthManager.LoggedUser != null && AuthManager.LoggedUser.AdminToSubReddits.Any(sr => sr.Id == model.SubRedditId)) { model.Posts = repo.GetAll(m => m.SubRedditId == model.SubRedditId).OrderByDescending(a => a.Rating).ToList(); } else { model.Posts = repo.GetAll(m => m.SubRedditId == model.SubRedditId && m.IsApproved == true).OrderByDescending(a => a.Rating).ToList(); } UsersRepository usersRepo = new UsersRepository(); model.User = usersRepo.GetById(model.UserId); SubRedditsRepository subRedditsRepo = new SubRedditsRepository(); model.SubReddit = subRedditsRepo.GetById(model.SubRedditId); return(View(model)); }
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); SubRedditsRepository repo = new SubRedditsRepository(); PostsRepository postsRepository = new PostsRepository(); UsersRepository usersRepository = new UsersRepository(); List <SubReddit> items = repo.GetAll(null); List <Post> posts = postsRepository.GetAll(null); List <User> users = usersRepository.GetAll(null); foreach (SubReddit sr in items) { routes.MapRoute( name: "SubReddit" + sr.Id, url: "r/" + sr.Name, defaults: new { controller = "Posts", action = "Index", SubRedditId = sr.Id }); /*foreach (Post item in posts.Where(a => a.SubRedditId==sr.Id)) * { * routes.MapRoute( * name: "Post" + item.Id, * url: "r/" + sr.Name +"/" + "comments"+ '/' + getTitleForURL(item.Title.ToLower()), * defaults: new { controller = "Comments", action = "Index", PostId = item.Id }); * }*/ } foreach (User item in users) { routes.MapRoute( name: "Profile" + item.Id, url: "user/" + item.Username, defaults: new { controller = "Profile", action = "Index", UserId = item.Id } ); } /*foreach (var item in Reddit.Models.AuthManager.LoggedUser.SubscribedToSubReddits) * { * routes.MapRoute( * name: "Something" , * url: "r/" + item.Name + "/submit", * defaults: new { controller = "Posts", action = "Create", id = UrlParameter.Optional } * ); * } * * string allOrHome = ""; * if (Reddit.Models.AuthManager.LoggedUser != null) allOrHome = "r/home"; * else allOrHome = "r/all"; * routes.MapRoute( * name: "allHome", * url: allOrHome, * defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } * );*/ routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
public ActionResult Search(SearchVM model, int?postId, int?subredditId) { model.SubReddits = new List <SubReddit>(); model.Users = new List <User>(); SubRedditsRepository subRepo = new SubRedditsRepository(); PostsRepository postRepo = new PostsRepository(); UsersRepository userRepo = new UsersRepository(); List <SubReddit> subs = subRepo.GetAll(a => a.Name.Contains(model.Filter)).OrderByDescending(a => a.SubscribedUsers.Count()).ToList(); List <User> users = userRepo.GetAll(a => a.Username.Contains(model.Filter)).OrderByDescending(a => a.Karma).ToList(); // model.SubReddits = subRepo.GetAll(a => a.Name.Contains(model.Filter)).ToList(); var count = 0; Thread ta = new Thread(new ThreadStart(UsersLoop)); Thread tb = new Thread(new ThreadStart(SubLoop)); ta.Start(); tb.Start(); ta.Join(); tb.Join(); void UsersLoop() { Thread.Sleep(400); foreach (var item in users) { if (count == 3) { break; } model.Users.Add(item); count++; } } void SubLoop() { Thread.Sleep(390); foreach (var item in subs) { if (count == 3) { break; } model.SubReddits.Add(item); count++; } } model.Posts = postRepo.GetAll(a => a.Title.ToLower().Contains(model.Filter.ToLower()) || a.SubReddit.Name.ToLower().Contains(model.Filter.ToLower()) || a.Comments.Any(b => b.Text.ToLower().Contains(model.Filter.ToLower())) || a.Content.ToLower().Contains(model.Filter.ToLower())).OrderByDescending(b => b.Rating).ToList(); return(View(model)); }
public ActionResult GetAllSubreddits(SubRedditsVM model, int?userId) { SubRedditsRepository repo = new SubRedditsRepository(); if (userId == null) { model.SubRedditsList = repo.GetAll(null); } else { model.SubRedditsList = repo.GetAll(x => x.SubscribedUsers.Any(b => b.Id == userId)).ToList(); } return(View(model)); }
public ActionResult Index() { PostIndexVM model = new PostIndexVM(); PostsRepository repo = new PostsRepository(); SubRedditsRepository subRedditsRepository = new SubRedditsRepository(); model.Posts = repo.GetAll(p => p.IsApproved == true).OrderByDescending(a => a.Rating).ToList(); model.TrendingSubReddits = subRedditsRepository.GetAll(null); if (AuthManager.LoggedUser != null) { model.SubReddits = subRedditsRepository.GetAll(null) .Where(x => x.SubscribedUsers.Any(b => b.Id == AuthManager.LoggedUser.Id)).OrderByDescending(c => c.Id).ToList(); } else { model.SubReddits = subRedditsRepository.GetAll(null); } return(View(model)); }
public ActionResult Edit(EditVM model, FormCollection form) { if (!ModelState.IsValid) { return(View("Create")); } SubRedditsRepository subRedditsRepo = new SubRedditsRepository(); PostsRepository postsRepo = new PostsRepository(); Post item = new Post(); if (model.SubRedditId == 0) { model.SubRedditId = model.SelectedSubReddit; } if ((Request.Files["image"] != null) && (!String.IsNullOrEmpty(Request.Files["image"].FileName)) && (Request.Files["image"].ContentLength > 0)) { if (FileFormats.Formats.ImageFormats.Contains(Request.Files["image"].FileName.Split('.').Last().ToLower()) || FileFormats.Formats.VideoFormats.Contains(Request.Files["image"].FileName.Split('.').Last().ToLower())) { string file_path = Server.MapPath("~/Content/img/post_images/"); Request.Files["image"].SaveAs(file_path + Request.Files["image"].FileName); model.Content = file_path + Request.Files["image"].FileName; model.PopulateEntity(item); postsRepo.Save(item); return(RedirectToAction("Index", "Posts", new { SubRedditId = model.SubRedditId })); } else { ModelState.AddModelError("notSupportedFormat", "This format is not supported :("); model.SubRedditsList = new List <SelectListItem>(); List <SubReddit> subscribedSubReddits = subRedditsRepo.GetMySubscribes(AuthManager.LoggedUser.Id).ToList(); foreach (SubReddit subReddit in subscribedSubReddits) { if (!(AuthManager.LoggedUser.BannedInSubReddits.Any(sr => sr.BannedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id)) || AuthManager.LoggedUser.MutedInSubReddits.Any(sr => sr.MutedUsers.Any(u => u.Id == AuthManager.LoggedUser.Id)))) { model.SubRedditsList.Add( new SelectListItem() { Value = subReddit.Id.ToString(), Text = subReddit.Name, Selected = subReddit.Id == model.SelectedSubReddit }); } } return(View("Create", model)); } } model.PopulateEntity(item); postsRepo.Save(item); return(RedirectToAction("Index", "Posts", new { SubRedditId = model.SubRedditId })); //return Content(""); }