public ActionResult GetAdminUserProfile(string id=null) { ViewBag.ViewUserId = id; ViewBag.PrimaryTagName = String.Empty; ViewBag.SecondaryTagNames = new List<string>(); DataContext cxt = new DataContext(); var q = (from up in cxt.UserProfiles join t in cxt.Tags on up.PrimaryTagId equals t.Id where up.UserId.ToString() == id select t.TagName); if (q.Any()) { ViewBag.PrimaryTagName = q.First(); } List<string> secondaryTagNames = new List<string>(); ViewBag.SecondaryTagNames = (from ut in cxt.UserTags join t in cxt.Tags on ut.TagID equals t.Id where ut.UserID.ToString() == id select t.TagName).ToList(); return View((object)cxt.UserProfiles.Where(x=>x.UserId.ToString()==id).First()); }
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 ActionResult Upload() { ViewBag.OperationMessages = new List<string>(); DataContext cxt = new DataContext(); if (Request.Files.Count > 0) { int maxFileSizeKbytes = 512; int.TryParse(ConfigurationManager.AppSettings["Commons.Hub.MaxUserImageSize"], out maxFileSizeKbytes); if (Request.Files[0].InputStream.Length > (maxFileSizeKbytes * 1024)) { ViewBag.OperationMessages.Add("The file was too large. Maximum size for an upload is " + maxFileSizeKbytes.ToString() + " KBytes"); } else { byte[] dataBuffer = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(dataBuffer, 0, dataBuffer.Length); string sql = "delete from UserImages where userid={0}"; cxt.Database.ExecuteSqlCommand(sql, ViewBag.UserProfile.UserId); sql = "insert into UserImages (UserID,ImageData,FileExtension) values ({0},{1},{2})"; cxt.Database.ExecuteSqlCommand(sql, ViewBag.UserProfile.UserId, dataBuffer, Path.GetExtension(Request.Files[0].FileName)); return RedirectToAction("UserProfile", "Account"); } } return View(); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (HttpContext.Current.User.Identity.IsAuthenticated) { DataContext cxt = new DataContext(); var q = cxt.UserProfiles.Where(x => x.UserName == HttpContext.Current.User.Identity.Name); if (q.Any()) { filterContext.Controller.ViewBag.UserProfile = q.First(); int userID = q.First().UserId; var q2 = cxt.UserTags.Where(x => x.UserID == userID); if (q2.Any()) { filterContext.Controller.ViewBag.UserTags = q2.ToList(); } else { filterContext.Controller.ViewBag.UserTags = new List<UserTag>(); } } } }
public ActionResult Display(string id) { int _id = 1; if (!String.IsNullOrEmpty(id)) { int.TryParse(id, out _id); } if (_id == -1) { throw new HttpException(404,"Profile not found"); } DataContext cxt = new DataContext(); string sql = "select imagedata from UserImages where userid={0}"; List<UserImage> i = cxt.Database.SqlQuery(typeof(UserImage), sql, _id).OfType<UserImage>().ToList(); if (i.Any()) { return File(i.First().ImageData, System.Web.MimeMapping.GetMimeMapping("userimage" + i.First().FileExtension), "userimage" + i.First().FileExtension); } else { return File(Server.MapPath("~/images/no-picture.png"), System.Web.MimeMapping.GetMimeMapping("no-picture.png")); } }
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 UserProfile GetUserByUserId(int userId) { var q = new DataContext().UserProfiles.Where(x => x.UserId == userId); if (q.Any()) { return q.First(); } else { return null;} }
public static void DeletePost(int postID) { DataContext cxt = new DataContext(); cxt.Database.ExecuteSqlCommand("delete from dbo.PostFeedback where PostID=@p0", postID); cxt.Database.ExecuteSqlCommand("delete from dbo.PostViews where postID =@p0", postID); cxt.Database.ExecuteSqlCommand("delete from dbo.PostTags where postid= @p0", postID); cxt.Database.ExecuteSqlCommand("delete from dbo.Posts where postid=@p0", postID); }
public static void SaveUserTags(int userID,List<UserTag> userTags) { DataContext cxt = new DataContext(); cxt.Database.ExecuteSqlCommand("delete from dbo.usertags where userid=@p0", userID); foreach (UserTag userTag in userTags) { cxt.Database.ExecuteSqlCommand("insert into dbo.usertags(userid, tagid) values (@p0, @p1)", userTag.UserID, userTag.TagID); } }
public static void TagPost(int postID, List<int> tagIDs) { DataContext cxt = new DataContext(); cxt.Database.ExecuteSqlCommand("delete from dbo.posttags where PostId=@p0", postID); foreach (int tagID in tagIDs) { cxt.Database.ExecuteSqlCommand("insert into dbo.posttags (PostId, TagId) values (@p0, @p1)", postID, tagID); } }
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 List<Post> GetHomeFeed(int userID) { DataContext cxt = new DataContext(); var q1 = from p in cxt.Posts join us in cxt.UserUserSubscriptions on p.PostUserId equals us.SubscribedUserId where us.UserId == userID select p; return q1.ToList(); }
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 List<Tag> GetTags() { DataContext cxt = new DataContext(); List<Tag> tags = cxt.Tags.ToList(); if (!tags.Any()) { tags.Add(new Tag() { Id = -1, TagName = "No tags defined" }); } return tags; }
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 ActionResult ViewProfile(string id = null) { ViewBag.ViewUserId = id; ViewBag.PrimaryTagName = String.Empty; ViewBag.SecondaryTagNames = new List<string>(); DataContext cxt = new DataContext(); var q = (from up in cxt.UserProfiles join t in cxt.Tags on up.PrimaryTagId equals t.Id where up.UserId.ToString() == id select t.TagName); if (q.Any()) { ViewBag.PrimaryTagName = q.First(); } List<string> secondaryTagNames = new List<string>(); ViewBag.SecondaryTagNames = (from ut in cxt.UserTags join t in cxt.Tags on ut.TagID equals t.Id where ut.UserID.ToString() == id select t.TagName).ToList(); int currentUserID = ViewBag.UserProfile.UserId; var s = cxt.UserUserSubscriptions.Where(x => x.UserId == currentUserID && x.SubscribedUserId.ToString() == id); if (s.Any()) { ViewBag.SubscriptionId = s.First().Id; } return View((object)cxt.UserProfiles.Where(x => x.UserId.ToString() == id).First()); }
public SimpleMembershipInitializer() { Database.SetInitializer<DataContext>(null); try { using (var context = new DataContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
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 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 UserProfile() { DataContext cxt = new DataContext(); int? selected = null; if (ViewBag.UserProfile.PrimaryTagId != null) { int primaryTagId = ViewBag.UserProfile.PrimaryTagId; var q = cxt.Tags.Where(x => x.Id == primaryTagId); if (q.Any()) { selected = cxt.Tags.Where(x => x.Id == primaryTagId).First().Id; } else { selected = -1; } } cxt.Tags.Add(new Tag() { Id=-1, TagName="Select One" }); ViewBag.profileTags = new SelectList(cxt.Tags,"Id","TagName",selected); return View((object)ViewBag.UserProfile); }
public static bool UserHasImage(int userID) { DataContext cxt = new DataContext(); int cnt = cxt.Database.SqlQuery<int>( "select count(1) from userimages where imagedata is not null and userid=@p0", userID).First(); return (cnt > 0); }
public ActionResult GetUserPosts(string id=null) { int _id = -1; if (!String.IsNullOrEmpty(id)) { int.TryParse(id, out _id); } else { _id = ViewBag.UserProfile.UserId; } DataContext cxt = new DataContext(); var q1 = cxt.Posts.Where(x=>x.PostUserId == _id); var q2 = cxt.UserProfiles.Where(x => x.UserId == _id); if (!q2.Any()) { throw new HttpException(404, "User not found"); } ViewBag.ViewUserName = q2.First().UserName; if (q1.Any()) { return View((object)q1.ToList()); } else { return View((object) new List<Post>()); } }
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"); }
public ActionResult Post(string id = null) { int _id = -1; if (!String.IsNullOrEmpty(id)) { int.TryParse(id, out _id); } DataContext cxt = new DataContext(); if (_id != -1) { // Load existing post ViewBag.PostId = _id; int currentUserId = ViewBag.UserProfile.UserId; DataContext contCxt = new DataContext(); var q = contCxt.Posts.Where(x => x.PostId == _id && x.PostUserId == currentUserId); if (q.Any()) { var q1 = cxt.PostTags.Where(x => x.PostId == _id); if (q1.Any()) { ViewBag.PostTags = q1.ToList(); } return View((object)q.ToList().First()); } return RedirectToAction("Post"); } else { // Create a new post. return View(); } }
public ActionResult Remove() { DataContext cxt = new DataContext(); cxt.Database.ExecuteSqlCommand("delete from UserImages where userid={0}", ViewBag.UserProfile.UserId); return RedirectToAction("UserProfile", "Account"); }
public ActionResult Tags() { ViewBag.OperationErrors = new List<string>(); ViewBag.OperationMessages = new List<string>(); ViewBag.OperationSuccess = false; DataContext cxt = new DataContext(); List<Tag> tags = cxt.Tags.ToList(); if (!tags.Any()) { tags.Add(new Tag(){ Id=-1, TagName="No tags defined"}); } ViewBag.Tags = tags; return View(); }
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 Users() { DataContext cxt = new DataContext(); ViewBag.UserList = cxt.UserProfiles.ToList(); return View(); }
public static void GetPostFeedbackStats(int postID, out int negative, out int positive) { DataContext cxt = new DataContext(); positive = cxt.PostFeedback.Where(x => x.FeedbackValue == 1 && x.PostId == postID).Count(); negative = cxt.PostFeedback.Where(x => x.FeedbackValue == -1 && x.PostId == postID).Count(); }