public ActionResult EditSubCategory(ForumSubCategory model) { using (var context = new DasKlubDbContext()) { ForumSubCategory currentThread = context.ForumSubCategory.First( currentForumSubCategory => currentForumSubCategory.ForumSubCategoryID == model.ForumSubCategoryID); if (_ua.UserAccountID != currentThread.CreatedByUserID && !_ua.IsAdmin) { throw new UnauthorizedAccessException(); } currentThread.UpdatedByUserID = _ua.UserAccountID; if (currentThread.ForumCategoryID != model.ForumCategoryID) { // update switched category var originalForum = context.ForumCategory.First(x => x.ForumCategoryID == currentThread.ForumCategoryID); var newForum = context.ForumCategory.First(x => x.ForumCategoryID == model.ForumCategoryID); originalForum.PostCount -= currentThread.PostCount; originalForum.ThreadCount -= 1; newForum.PostCount += currentThread.PostCount; newForum.ThreadCount += 1; var newForumLastThread = context.ForumSubCategory.First(x => x.ForumSubCategoryID == newForum.LastActiveSubCategoryId); if (newForumLastThread.LastActiveDateTimeUtc < currentThread.LastActiveDateTimeUtc) { newForumLastThread.LastActiveDateTimeUtc = currentThread.LastActiveDateTimeUtc; newForumLastThread.LastActiveForumPostId = currentThread.LastActiveForumPostId; newForumLastThread.LastActiveUserId = currentThread.LastActiveUserId; } var originalForumLastThread = context.ForumSubCategory .OrderByDescending(x => x.LastActiveDateTimeUtc) .First(x => x.ForumSubCategoryID != currentThread.ForumSubCategoryID); if (originalForumLastThread.LastActiveDateTimeUtc < currentThread.LastActiveDateTimeUtc) { currentThread.LastActiveDateTimeUtc = originalForumLastThread.LastActiveDateTimeUtc; currentThread.LastActiveForumPostId = originalForumLastThread.LastActiveForumPostId; currentThread.LastActiveUserId = originalForumLastThread.LastActiveUserId; } currentThread.ForumCategoryID = model.ForumCategoryID; } currentThread.Title = model.Title; currentThread.Description = model.Description; context.Entry(currentThread).State = EntityState.Modified; context.SaveChanges(); model.Key = currentThread.Key; model.ForumCategory = context.ForumCategory.First(forum => forum.ForumCategoryID == currentThread.ForumCategoryID); } return new RedirectResult(model.SubForumURL.ToString()); }
public ActionResult ForumPost(string key, string subKey, int pageNumber = 1) { using (var context = new DasKlubDbContext()) { ViewBag.UserID = (_mu == null) ? 0 : Convert.ToInt32(_mu.ProviderUserKey); context.Configuration.ProxyCreationEnabled = false; context.Configuration.LazyLoadingEnabled = false; ForumCategory forum = context.ForumCategory.FirstOrDefault(forumPost => forumPost.Key == key); if (forum == null || forum.ForumCategoryID == 0) return RedirectPermanent("~/forum"); // it's gone ViewBag.Forum = forum; ViewBag.MetaDescription = forum.Description; var subForum = context.ForumSubCategory.FirstOrDefault(forumPost => forumPost.Key == subKey && forumPost.ForumCategoryID == forum.ForumCategoryID); if (subForum == null || subForum.ForumSubCategoryID == 0) return RedirectPermanent("~/forum"); // it's gone // TODO: USE LINQ or store the counts in the user profiles var comm = DbAct.CreateCommand(); comm.CommandText = string.Format(@" select temp.CreatedByUserID, count(*) as 'count' from ( select CreatedByUserID, ForumSubCategoryID from ForumSubCategories with (nolock) union all select CreatedByUserID, ForumSubCategoryID from ForumPosts with (nolock) ) as temp where CreatedByUserID in (select CreatedByUserID from ForumPosts with (nolock) where ForumSubCategoryID = {0}) group by CreatedByUserID order by CreatedByUserID ", subForum.ForumSubCategoryID); comm.CommandType = CommandType.Text; var userPostCounts = DbAct.ExecuteSelectCommand(comm); Dictionary<int, int> userPostCountList = userPostCounts.Rows .Cast<DataRow>() .ToDictionary( row => Convert.ToInt32(row["CreatedByUserID"]), row => Convert.ToInt32(row["count"])); ViewBag.UserPostCounts = userPostCountList; subForum.UserAccount = new UserAccount(subForum.CreatedByUserID); ViewBag.SubForum = subForum; List<ForumPost> forumPostDisplay = context.ForumPost .Where(x => x.ForumSubCategoryID == subForum.ForumSubCategoryID) .OrderBy(x => x.CreateDate) .Skip(PageSize*(pageNumber - 1)) .Take(PageSize).ToList(); if (_mu != null) { var userId = Convert.ToInt32(_mu.ProviderUserKey); var ua = new UserAccount(userId); ViewBag.IsAdmin = ua.IsAdmin; var forumPostNotification = context.ForumPostNotification.FirstOrDefault( x => x.ForumSubCategoryID == subForum.ForumSubCategoryID && x.UserAccountID == userId); if (forumPostNotification != null) { forumPostNotification.IsRead = true; context.Entry(forumPostNotification).State = EntityState.Modified; context.SaveChanges(); } } int totalCount = subForum.PostCount - 1; ViewBag.PageCount = (totalCount + PageSize )/PageSize; ViewBag.PageNumber = pageNumber; ViewBag.PageTitle = subForum.Title; ViewBag.Title = pageNumber == 1 ? string.Format("{0}", subForum.Title) : string.Format("{0} | page {1}", subForum.Title, pageNumber); foreach (ForumPost post in forumPostDisplay) { post.UserAccount = new UserAccount(post.CreatedByUserID); } ViewBag.MetaDescription = FromString.GetFixedLengthString(subForum.Description, 160); return View(forumPostDisplay); } }
public ActionResult EditForum(ForumCategory model) { if (_mu == null) return RedirectToAction("Index"); var ua = new UserAccount(Convert.ToInt32(_mu.ProviderUserKey)); if (!ua.IsAdmin) throw new UnauthorizedAccessException(); using (var context = new DasKlubDbContext()) { if (model == null) return RedirectToAction("Index"); ForumCategory forumCategory = context.ForumCategory.First(x => x.ForumCategoryID == model.ForumCategoryID); forumCategory.CreatedByUserID = ua.UserAccountID; forumCategory.Key = FromString.UrlKey(model.Title); forumCategory.Title = model.Title.Trim(); forumCategory.Description = model.Description.Trim(); context.Entry(forumCategory).State = EntityState.Modified; context.SaveChanges(); } return RedirectToAction("Index"); }
public ActionResult EditForumPost(ForumPost model) { using (var context = new DasKlubDbContext()) { ForumPost forumPost = context.ForumPost.First( currentForumPost => currentForumPost.ForumPostID == model.ForumPostID); if (_ua.UserAccountID != forumPost.CreatedByUserID && !_ua.IsAdmin) { throw new UnauthorizedAccessException(); } forumPost.UpdatedByUserID = _ua.UserAccountID; forumPost.Detail = model.Detail; context.Entry(forumPost).State = EntityState.Modified; context.SaveChanges(); ForumSubCategory subForum = context.ForumSubCategory.First( currentForumSubCategory => currentForumSubCategory.ForumSubCategoryID == model.ForumSubCategoryID); subForum.ForumCategory = context.ForumCategory.First(forum => forum.ForumCategoryID == subForum.ForumCategoryID); return new RedirectResult(subForum.SubForumURL.ToString()); } }
public ActionResult DeleteSubForum(int forumSubCategoryID) { using (var context = new DasKlubDbContext()) { ForumSubCategory thread = context.ForumSubCategory.First(x => x.ForumSubCategoryID == forumSubCategoryID); if (Convert.ToInt32(_mu.ProviderUserKey) != thread.CreatedByUserID && !_ua.IsAdmin) return RedirectToAction("Index"); context.ForumSubCategory.Remove(thread); ForumCategory forum = context.ForumCategory.First(x => x.ForumCategoryID == thread.ForumCategoryID); var previousThread = context.ForumSubCategory .OrderByDescending(x => x.LastActiveDateTimeUtc) .Where(x => x.ForumSubCategoryID != forumSubCategoryID && x.ForumCategoryID == forum.ForumCategoryID) .FirstOrDefault(); if (previousThread != null) { forum.LastActiveSubCategoryId = previousThread.ForumSubCategoryID; } else { forum.LastActiveSubCategoryId = 0; } forum.PostCount -= thread.PostCount; forum.ThreadCount -= 1; context.Entry(forum).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("Index"); } }
public ActionResult DeleteForumPost(int forumPostID) { using (var context = new DasKlubDbContext()) { ForumPost forumPost = context.ForumPost.First(x => x.ForumPostID == forumPostID); if (Convert.ToInt32(_mu.ProviderUserKey) != forumPost.CreatedByUserID && !_ua.IsAdmin) return RedirectToAction("Index"); context.ForumPost.Remove(forumPost); var previousPost = context.ForumPost .OrderByDescending(x => x.CreateDate) .Where(x => x.ForumPostID != forumPostID && x.ForumSubCategoryID == forumPost.ForumSubCategoryID) .FirstOrDefault(); var subForum = context.ForumSubCategory.First(x => x.ForumSubCategoryID == forumPost.ForumSubCategoryID); subForum.PostCount -= 1; if (previousPost != null) { subForum.LastActiveDateTimeUtc = previousPost.CreateDate; subForum.LastActiveForumPostId = previousPost.ForumPostID; subForum.LastActiveUserId = previousPost.CreatedByUserID; } else { subForum.LastActiveDateTimeUtc = subForum.CreateDate; subForum.LastActiveForumPostId = 0; subForum.LastActiveUserId = subForum.CreatedByUserID; } context.Entry(subForum).State = EntityState.Modified; var forum = context.ForumCategory.First(x => x.ForumCategoryID == subForum.ForumCategoryID); forum.PostCount -= 1; context.Entry(forum).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("Index"); } }
public ActionResult CreateSubCategory(ForumSubCategory model) { if (_mu == null) return new EmptyResult(); using (var context = new DasKlubDbContext()) { if (model == null) return new EmptyResult(); ForumCategory forum = context.ForumCategory.First(x => x.Key == model.Key); ViewBag.Forum = forum; if (!ModelState.IsValid) { return View(model); } if (forum.ThreadsRequirePermission && ((_ua == null) || !_ua.IsAdmin)) { return new EmptyResult(); } model.LastActiveDateTimeUtc = DateTime.UtcNow; // nulls are allowed, change when modifying model.LastActiveUserId = _ua.UserAccountID; model.ForumCategoryID = forum.ForumCategoryID; model.CreatedByUserID = _ua.UserAccountID; model.Key = FromString.UrlKey(model.Title); model.Title = model.Title.Trim(); model.Description = model.Description.Trim(); model.PostCount += 1; var existingThreadInForum = context.ForumSubCategory.FirstOrDefault( forumSubCategory => forumSubCategory.ForumCategoryID == model.ForumCategoryID && forumSubCategory.Key == model.Key); if (existingThreadInForum != null) { // TODO: LOCALIZE existingThreadInForum.ForumCategory = forum; ModelState.AddModelError("Title", string.Format(@"The title '{0}' is already taken, change your title.", model.Title)); return View(model); } context.ForumSubCategory.Add(model); //update the forum category forum.PostCount += 1; forum.ThreadCount += 1; context.Entry(forum).State = EntityState.Modified; var notification = new ForumPostNotification { CreatedByUserID = Convert.ToInt32(_mu.ProviderUserKey), IsRead = true, UserAccountID = Convert.ToInt32(_mu.ProviderUserKey), ForumSubCategoryID = model.ForumSubCategoryID }; context.ForumPostNotification.Add(notification); context.SaveChanges(); // update to the last active forum.LastActiveSubCategoryId = model.ForumSubCategoryID; context.Entry(forum).State = EntityState.Modified; model.LastActiveDateTimeUtc = model.CreateDate; context.Entry(model); context.SaveChanges(); return RedirectToAction("ForumPost", "Forum", new {subkey = model.Key, key = forum.Key}); } }
public ActionResult CreateForumPost(ForumPost model, int forumSubCategoryID) { using (var context = new DasKlubDbContext()) { string currentLang = Utilities.GetCurrentLanguageCode(); ForumSubCategory subForum = context.ForumSubCategory .First(x => x.ForumSubCategoryID == forumSubCategoryID); if (!ModelState.IsValid) { ViewBag.SubForum = subForum; return View(model); } var ua = new UserAccount(Convert.ToInt32(_mu.ProviderUserKey)); model.ForumSubCategoryID = forumSubCategoryID; model.CreatedByUserID = ua.UserAccountID; context.ForumPost.Add(model); ForumPostNotification currentUserNotification = context.ForumPostNotification .FirstOrDefault( x => x.ForumSubCategoryID == forumSubCategoryID && x.UserAccountID == ua.UserAccountID); if (currentUserNotification == null || currentUserNotification.ForumPostNotificationID == 0) { var notification = new ForumPostNotification { CreatedByUserID = Convert.ToInt32(_mu.ProviderUserKey), IsRead = true, UserAccountID = Convert.ToInt32(_mu.ProviderUserKey), ForumSubCategoryID = forumSubCategoryID }; context.ForumPostNotification.Add(notification); } List<ForumPostNotification> allUserNotifications = context.ForumPostNotification.Where(x => x.ForumSubCategoryID == forumSubCategoryID).ToList(); subForum.ForumCategory = context.ForumCategory.First(x => x.ForumCategoryID == subForum.ForumCategoryID); if (context.ForumPost.FirstOrDefault( x => x.ForumSubCategoryID == forumSubCategoryID && x.Detail == model.Detail && x.CreatedByUserID == ua.UserAccountID) != null) return new EmptyResult(); Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(SiteEnums.SiteLanguages.EN.ToString()); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SiteEnums.SiteLanguages.EN.ToString()); foreach (var forumPostNotification in allUserNotifications.Where( forumPostNotification => forumPostNotification.UserAccountID != ua.UserAccountID)) { forumPostNotification.IsRead = false; forumPostNotification.UpdatedByUserID = Convert.ToInt32(_mu.ProviderUserKey); context.Entry(forumPostNotification).State = EntityState.Modified; var notifiedUser = new UserAccount(forumPostNotification.UserAccountID); var notifiedUserDetails = new UserAccountDetail(); notifiedUserDetails.GetUserAccountDeailForUser(forumPostNotification.UserAccountID); if (!notifiedUserDetails.EmailMessages) continue; string title = ua.UserName + " => " + subForum.Title; var body = new StringBuilder(100); body.Append(Messages.New); body.Append(": "); body.Append(subForum.SubForumURL); body.AppendLine(); body.AppendLine(); body.Append(model.Detail); body.AppendLine(); body.AppendLine(); body.Append(Messages.Reply); body.Append(": "); body.AppendFormat("{0}/create", subForum.SubForumURL); try { _mail.SendMail(AmazonCloudConfigs.SendFromEmail, notifiedUser.EMail, title, body.ToString()); } catch (Exception ex) { Utilities.LogError(ex); } } var forum = context.ForumCategory.First(x => x.ForumCategoryID == subForum.ForumCategoryID); forum.PostCount += 1; forum.LastActiveSubCategoryId = forumSubCategoryID; context.Entry(forum).State = EntityState.Modified; try { context.SaveChanges(); subForum.PostCount += 1; subForum.LastActiveDateTimeUtc = model.CreateDate; subForum.LastActiveForumPostId = model.ForumPostID; subForum.LastActiveUserId = model.CreatedByUserID; context.Entry(subForum).State = EntityState.Modified; context.SaveChanges(); } catch (Exception ex) { Utilities.LogError(ex); } Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(currentLang); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(currentLang); Response.Redirect(subForum.SubForumURL.ToString()); return new EmptyResult(); } }
public ActionResult AdminEdit(CalendarEntryDisplayModel model) { using (var context = new DasKlubDbContext()) { var entry = context.CalendarContestEntry.First(item => item.CalendarContestEntryId == model.CalendarContestEntryId); entry.IsApproved = model.IsApproved; context.Entry(entry).State = EntityState.Modified; context.SaveChanges(); return View(model); } }