// // GET: /Account/UserExtendedProfile public ActionResult UserExtendedProfile() { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile user = dbContext.UserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name); ViewBag.TagList = Util.TagList; ViewBag.ProfessionList = new SelectList(Util.ListOfProfessions); if (user == null || user.UserExtendedProfile == null) { return(View()); } UserExtendedProfileModel model = new UserExtendedProfileModel { AlmaMater = user.UserExtendedProfile.AlmaMater, City = user.UserExtendedProfile.City, DOB = user.UserExtendedProfile.DOB, Profession = user.UserExtendedProfile.Profession, Qualifications = user.UserExtendedProfile.Qualifications, ImageUrl = user.UserExtendedProfile.ImageUrl, Description = user.UserExtendedProfile.Description }; model.Tags = new List <int>(); foreach (Tag tag in user.UserExtendedProfile.Tags) { model.Tags.Add(tag.Id); } return(View(model)); } }
public JsonResult IsUserUnique(string UserName) { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { return(Json(!dbContext.UserProfiles.Any(x => x.UserName == UserName), JsonRequestBehavior.AllowGet)); } }
public JsonResult IsEmailUnique(string emailId) { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { return(Json(!dbContext.UserProfiles.Any(x => x.EmailId == emailId), JsonRequestBehavior.AllowGet)); } }
public JsonResult ProcessRequest(RequestPostModel requestPageData) { bool success = true; try { RequestState state = (RequestState)Enum.Parse(typeof(RequestState), requestPageData.State, true); if (state == RequestState.Pending) { return(Json(new JavaScriptSerializer().Serialize(new { success }))); } using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { Requests request = dbContext.Requests.Where(r => r.Id == requestPageData.RequestId).FirstOrDefault(); Requests newRequest = new Requests { Id = request.Id, From = request.From, To = request.To, Time = request.Time, State = (RequestState)Enum.Parse(typeof(RequestState), requestPageData.State, true) }; newRequest.Update(dbContext); dbContext.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.Write("Failed with following ex : {0}", ex.ToString()); success = false; } return(Json(new JavaScriptSerializer().Serialize(new { success }))); }
private bool TrySaveRequest(int profileId, UserProfile fromUser, UserProfileDatabaseContext dbContext, out int?socialId) { socialId = -1; try { UserProfile toUser = dbContext.UserProfiles.FirstOrDefault(p => p.Id == profileId); bool dublicateRequest = dbContext.Requests.Where(r => r.To.Id == toUser.Id && r.From.Id == fromUser.Id).Any(); if (dublicateRequest) { return(false); } Requests request = new Requests { To = toUser, From = fromUser, Time = DateTime.UtcNow, State = RequestState.Pending }; dbContext.Requests.Add(request); dbContext.SaveChanges(); socialId = request.Id; } catch (Exception ex) { System.Diagnostics.Debug.Write("Faild with following ex : {0}", ex.ToString()); return(false); } return(true); }
public SimpleMembershipInitializer() { using (var context = new UserProfileDatabaseContext()) context.UserProfiles.Find(1); if (!WebSecurity.Initialized) { WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "Id", "UserName", autoCreateTables: true); } }
private bool TrySaveMessage(int profileId, UserProfile fromUser, out int?socialId) { socialId = -1; using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile toUser = dbContext.UserProfiles.FirstOrDefault(p => p.Id == profileId); } return(true); }
public JsonResult TagSearch(int[] data) { // Last element of data contains configuration instead of tags, tread carefully! int configuration = data[data.Length - 1]; int[] tags = new int[data.Length - 1]; Array.Copy(data, tags, data.Length - 1); if (tags.Length <= 0) { return(null); } using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { Dictionary <int, Tag> tagMap = dbContext.Tag.ToDictionary(x => x.Id, x => x); List <TagSearchResult> userList = new List <TagSearchResult>(); UserProfile currentUser = dbContext.UserProfiles.FirstOrDefault(x => x.UserName == User.Identity.Name); IList <int> requestedUsers = dbContext.Requests.Where(r => r.From.Id == currentUser.Id).Select(x => x.To.Id).ToList(); Dictionary <int, int> uniqueUsers = new Dictionary <int, int>(); foreach (int id in tags) { Tag tag; if (tagMap.TryGetValue(id, out tag)) { foreach (UserExtendedProfile user in tag.Users) { if (!uniqueUsers.ContainsKey(user.UserProfile.Id) && currentUser.Id != user.UserProfile.Id) { userList.Add(new TagSearchResult(user.UserProfile.Id, user.UserProfile.UserName, user.ImageUrl, requestSent: requestedUsers.Contains(user.UserProfile.Id))); uniqueUsers[user.UserProfile.Id] = 1; } else { if (uniqueUsers.ContainsKey(user.UserProfile.Id)) { uniqueUsers[user.UserProfile.Id]++; } } } } } // Configuration 0 : Union ; Configuration 1 : Intersection if (configuration == 1) { // Intersection return(Json(new JavaScriptSerializer().Serialize(userList.Where(x => uniqueUsers[x.ProfileId] == tags.Length)))); } // Union return(Json(new JavaScriptSerializer().Serialize(userList))); } }
public ViewResult GetRequests() { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile currentUser = dbContext.UserProfiles.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); IEnumerable <RequestPageViewModel> pendingUserRequests = dbContext.Requests.Where(r => r.To.Id == currentUser.Id && r.State == RequestState.Pending).Select(x => new RequestPageViewModel { Profession = x.From.UserExtendedProfile.Profession, UserName = x.From.UserName, RequestId = x.Id, State = x.State, TimeStamp = x.Time, Tags = x.From.UserExtendedProfile.Tags.Select(t => t.TagName).ToList() }); return(View("~/Views/Generic/RequestsPage.cshtml", pendingUserRequests.ToList())); } }
public PartialViewResult NavbarData() { if (!Request.IsAuthenticated) { return(PartialView("~/Views/Shared/_NavbarPartial.cshtml")); } using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile currentUser = dbContext.UserProfiles.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); int requestCount = dbContext.Requests.Where(r => r.To.Id == currentUser.Id && r.State == RequestState.Pending).Count(); int messageCount = dbContext.Messages.Where(r => r.To.Id == currentUser.Id).Count(); NavbarViewModel navbarModel = new NavbarViewModel { NumberOfMessages = messageCount, NumberOfRequests = requestCount }; return(PartialView("~/Views/Shared/_NavbarPartial.cshtml", navbarModel)); } }
public ActionResult SignUp(SignUpModel model) { if (ModelState.IsValid) { // Attempt to register the user try { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile user = new UserProfile { UserName = model.UserName, Country = model.Country, EmailId = model.EmailId, MobilePhone = model.MobilePhone }; dbContext.UserProfiles.Add(user); dbContext.SaveChanges(); } WebSecurity.CreateAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); //return RedirectToAction("UserExtendedProfile", "Account"); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } ViewBag.Countries = new SelectList(Util.ListOfCountries()); // If we got this far, something failed, redisplay form return(View(model)); }
public ViewResult Connections() { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile currentUser = dbContext.UserProfiles.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); IEnumerable <CompleteUserProfileViewModel> connectedUsers = dbContext.Requests.Where(r => (r.To.Id == currentUser.Id || r.From.Id == currentUser.Id) && r.State == RequestState.Accepted).Select(x => new CompleteUserProfileViewModel { AlmaMater = x.From.UserExtendedProfile.AlmaMater, City = x.From.UserExtendedProfile.City, Country = x.From.Country, Description = x.From.UserExtendedProfile.Description, DOB = x.From.UserExtendedProfile.DOB, EmailId = x.From.EmailId, ImageUrl = x.From.UserExtendedProfile.ImageUrl, MobilePhone = x.From.MobilePhone, Profession = x.From.UserExtendedProfile.Profession, Qualifications = x.From.UserExtendedProfile.Qualifications, Tags = x.From.UserExtendedProfile.Tags.Select(t => t.TagName).ToList(), UserName = x.From.UserName }); return(View(connectedUsers.ToList())); } }
public ActionResult UploadImage(HttpPostedFileBase file) { if (file != null) { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { try { string imageName = Path.GetFileName(file.FileName); if (imageName == null) { return(RedirectToAction("UserExtendedProfile", "Account")); } // TODO : Image name needs to be hashed. string imageExtension = imageName.Substring(imageName.IndexOf('.')); imageName = imageName.Substring(0, imageName.IndexOf('.')) + "_" + User.Identity.Name + imageExtension; string physicalPath = Server.MapPath("~/Images/ProfilePic"); physicalPath = Path.Combine(physicalPath, imageName); // Saves the image to file system. file.SaveAs(physicalPath); UserProfile user = dbContext.UserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name); if (user == null) { throw new Exception("User not found."); } UserExtendedProfile profile = new UserExtendedProfile { Id = user.Id, UserProfile = user, ImageUrl = imageName }; if (user.UserExtendedProfile != null) { profile.UserProfile = user; profile.Id = user.UserExtendedProfile.Id; profile.AlmaMater = user.UserExtendedProfile.AlmaMater; profile.City = user.UserExtendedProfile.City; profile.DOB = user.UserExtendedProfile.DOB; profile.Profession = user.UserExtendedProfile.Profession; profile.Qualifications = user.UserExtendedProfile.Qualifications; profile.ImageUrl = imageName; profile.Update(dbContext); } else { dbContext.UserExtendedProfile.Add(profile); } dbContext.SaveChanges(); } // TODO : Exception printing stacktrace needs to be removed. catch (Exception e) { ModelState.AddModelError("", e.StackTrace); } } } return(RedirectToAction("UserExtendedProfile", "Account")); }
public ActionResult UserExtendedProfile(UserExtendedProfileModel model) { if (ModelState.IsValid) { // Extend the user's profile try { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { UserProfile user = dbContext.UserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name); Dictionary <int, Tag> tagMap = dbContext.Tag.ToDictionary(x => x.Id, x => x); if (user == null) { // Log throw new Exception("User not found"); } UserExtendedProfile profile = new UserExtendedProfile { Id = user.Id, UserProfile = user, AlmaMater = model.AlmaMater, City = model.City, DOB = model.DOB, Profession = model.Profession, Qualifications = model.Qualifications, Description = model.Description, ImageUrl = string.Empty, Tags = new List <Tag>() }; foreach (int tagId in model.Tags) { Tag selectedTag = tagMap[tagId]; profile.Tags.Add(selectedTag); selectedTag.Users.Add(profile); } if (user.UserExtendedProfile != null) { foreach (Tag tag in user.UserExtendedProfile.Tags) { // This updates the tag } profile.ImageUrl = user.UserExtendedProfile.ImageUrl; profile.Update(dbContext); } else { dbContext.UserExtendedProfile.Add(profile); } UserProfile userProfile = new UserProfile { Id = user.Id, EmailId = user.EmailId, MobilePhone = user.MobilePhone, Country = user.Country, UserName = user.UserName, UserExtendedProfile = profile }; userProfile.Update(dbContext); dbContext.SaveChanges(); } return(RedirectToAction("Index", "Home")); } catch (Exception e) { // TODO : Exception printing stacktrace needs to be removed. ModelState.AddModelError("", e.StackTrace); } } ViewBag.TagList = Util.TagList; ViewBag.ProfessionList = new SelectList(Util.ListOfProfessions); // If we got this far, something failed, redisplay form return(View(model)); }
private bool TrySaveSocialType(int profileId, SocialTypes socialType) { bool success = false; try { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { // Get the SocialManager handle first UserProfile currentUser = dbContext.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name); SocialManager socialRecord = dbContext.SocialManager.Where(x => x.SocialType == socialType && x.User.Id == currentUser.Id).FirstOrDefault(); DateTime currentTime = DateTime.UtcNow; int days = socialRecord != null ? ((TimeSpan)currentTime.Subtract((DateTime)socialRecord.TimeStamp)).Days : 0; int? socialId; if (days <= ThresholdDays && socialRecord != null) { if (socialRecord.Count < ThresholdSocialTypeCount) { success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId); if (!success) { return(success); } socialRecord.Count++; socialRecord.Update(dbContext); } else { // No. of counts exceeded for the given period. success = false; } } else { success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId); if (!success) { return(success); } if (socialRecord != null) { socialRecord.TimeStamp = DateTime.UtcNow; socialRecord.SocialId = socialId.GetValueOrDefault(); socialRecord.Count = 1; socialRecord.Update(dbContext); } else { SocialManager newSocialRecord = new SocialManager { User = currentUser, SocialId = socialId.GetValueOrDefault(), SocialType = socialType, TimeStamp = DateTime.UtcNow, Count = 1, }; dbContext.SocialManager.Add(newSocialRecord); } } dbContext.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.Write("Operation failed with following ex : {0}", ex.ToString()); return(false); } return(success); }