public static void PostFeedback(int postID, int userID, int feedbackValue) { DataContext cxt = new DataContext(); var q1 = cxt.PostFeedback.Where(x => x.PostId == postID && x.UserId == userID); if (q1.Any()) { PostFeedback f = q1.First(); f.FeedbackValue = feedbackValue; cxt.SaveChanges(); } else { PostFeedback l = new PostFeedback() { UserId = userID, FeedbackDate = DateTime.Now, PostId = postID, FeedbackValue = feedbackValue }; cxt.PostFeedback.Add(l); cxt.SaveChanges(); } }
public Commons.Hub.Models.Post LoadViewData(int postId) { int userId = ViewBag.UserProfile.UserId; DataContext cxt = new DataContext(); var q1 = cxt.Posts.Where(x => x.PostId == postId); var q2 = cxt.PostTags.Where(x => x.PostId == postId); var q3 = cxt.PostFeedback.Where(x => x.PostId == postId && x.UserId == userId); ViewBag.postFeedbackValue = 0; Commons.Hub.Models.Post ret; if (q1.Any()) { cxt.PostViews.Add(new PostView() { PostId = postId, UserId = ViewBag.UserProfile.UserId, ViewDate = DateTime.Now }); cxt.SaveChanges(); ret = q1.FirstOrDefault(); ViewBag.PostTags = q2.ToList(); if (q3.Any()) { ViewBag.postFeedbackValue = q3.First().FeedbackValue; } } else { throw new HttpException(404, "Unknown post ID"); } return ret; }
public static void UnsubscribeUser(int subscriptionId) { DataContext cxt = new DataContext(); var s = cxt.UserUserSubscriptions.Where(x => x.Id == subscriptionId); if (s.Any()) { cxt.UserUserSubscriptions.Remove(s.First()); cxt.SaveChanges(); } }
public static void SubscribeUser(int subscriberUserId, int subscribedUserId) { DataContext cxt = new DataContext(); UserUserSubscription sub = new UserUserSubscription() { SubscribedUserId =subscriberUserId , UserId = subscribedUserId, SubscriptionDate=DateTime.Now }; cxt.UserUserSubscriptions.Add(sub); cxt.SaveChanges(); }
public static void DeleteTag(int tagId) { DataContext cxt = new DataContext(); if (CheckTagForDelete(tagId)) { var q1 = cxt.Tags.Where(x => x.Id ==tagId ); if (q1.Any()) { Tag t = q1.First(); cxt.Tags.Remove(t); cxt.SaveChanges(); } } else { throw new Exception("Could not delete requested tag"); } }
public static int AddTag(string tagName) { DataContext cxt = new DataContext(); var q1 = cxt.Tags.Where(x => x.TagName == tagName.ToLower()); if (q1.Any()) { throw new Exception(String.Format("A tag with the name \"{0}\" already exists", tagName)); } else { cxt.Tags.Add(new Tag() { TagName = tagName }); int recordsAffected = cxt.SaveChanges(); if (recordsAffected > 0) { return cxt.Tags.Where(x => x.TagName.ToLower() == tagName.ToLower()).First().Id; } else { throw new Exception("Error during tag entity creation."); } } }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (DataContext db = new DataContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
public ActionResult SaveProfile() { DataContext cxt = new DataContext(); ViewBag.OperationErrors = new List<string>(); ViewBag.OperationSuccess = false; try { var q1 = cxt.UserProfiles.Where(x => x.UserName == User.Identity.Name); if (q1.Any()) { int primaryTagId = -1; int.TryParse(Request.Form["profileTags"], out primaryTagId); bool isContentCreator = Request.Form["IsContentCreator"].Contains("true"); UserProfile currentProfile = q1.First(); currentProfile.IsContentCreator = isContentCreator; currentProfile.LastName = Request.Form["LastName"]; currentProfile.FirstName = Request.Form["FirstName"]; currentProfile.Email = Request.Form["Email"]; currentProfile.PrimaryTagId = ((primaryTagId == -1 ? (int?)null : primaryTagId)); // Content tags if (!String.IsNullOrEmpty(Request.Form["secondaryTags"])) { List<UserTag> newTags = new List<UserTag>(); string[] tagIds = Request.Form["secondaryTags"].Split(','); foreach (string tid in tagIds) { newTags.Add(new UserTag() { UserID = ViewBag.UserProfile.UserId, TagID = int.Parse(tid) }); } ContentTags.SaveUserTags(ViewBag.UserProfile.UserId, newTags); } // Save main profile cxt.SaveChanges(); ViewBag.OperationSuccess = true; } else { ViewBag.OperationErrors.Add("Unable to load profile for edit - not found"); } } catch (Exception ex) { ViewBag.OperationErrors.Add("An error occured while saving the profile: " + ex.Message); } return RedirectToAction("UserProfile"); }
public ActionResult SavePost(string id = null) { ViewBag.OperationSuccess = false; ViewBag.OperationMessages = new List<string>(); ViewBag.OperationErrors = new List<string>(); int _id = -1; int _currentUserId = ViewBag.UserProfile.UserId; if (!String.IsNullOrEmpty(id)) { int.TryParse(id, out _id); } if (Request.Form["operation"].ToLower() == "save") { DataContext cxt = new DataContext(); List<int> postTags = new List<int>(); if (!String.IsNullOrEmpty(Request.Form["chkTags"])) { string[] arTags = Request.Form["chkTags"].Split(','); if (arTags.Length > 2) { throw new HttpException(400, "Invalid Request - too many tags specified for post"); } foreach (string t in arTags) { int t_i = -1; int.TryParse(t, out t_i); if (t_i > 0) { postTags.Add(t_i); } } } Post currentPost = new Post() { Sticky = String.IsNullOrEmpty(Request.Form["Sticky"])?false:Request.Form["Sticky"].Contains("true"), Title = Request.Form["Title"], Url = Request.Form["Url"], Description = Request.Unvalidated.Form["Description"], PostUserId = ViewBag.UserProfile.UserId, PostedDate = DateTime.Now, EditDate = DateTime.Now, RealTime = String.IsNullOrEmpty(Request.Form["RealTime"])?false:Request.Form["RealTime"].Contains("true") }; if (_id > 0) { var q = cxt.Posts.Where(x => x.PostId == _id && x.PostUserId == _currentUserId); if (q.Any()) { Post editPost = q.FirstOrDefault(); editPost.Description = currentPost.Description; editPost.Sticky = currentPost.Sticky; editPost.Title = currentPost.Title; editPost.Url = currentPost.Url; editPost.EditDate = DateTime.Now; editPost.RealTime = currentPost.RealTime; cxt.SaveChanges(); Posts.TagPost(editPost.PostId, postTags); return RedirectToAction("ViewPost", routeValues: new { id = editPost.PostId }); } else { ViewBag.OperationErrors.Add("Either the post ID doesn't exist, or you do not have permission to edit it."); return View(); } } else { cxt.Posts.Add(currentPost); cxt.SaveChanges(); Posts.TagPost(currentPost.PostId, postTags); return RedirectToAction("ViewPost", routeValues: new { id = currentPost.PostId }); } } if (Request.Form["operation"].ToLower() == "delete") { Posts.DeletePost(_id); return View(); } throw new HttpException(400, "Invalid Operation"); }