/* * Method to list and search through users */ public ActionResult Index(string firstname, string lastname) { List <ProfileModel> searchResult = new List <ProfileModel>(); if (User.Identity.IsAuthenticated) { var ctx = new ZinderUserDbContext(); var userId = User.Identity.GetUserId(); if (ctx.Profiles.FirstOrDefault(p => p.ID == userId) != null) { // When the user loads the search page or leave search empty, only 5 users will be listed searchResult = ctx.Profiles.Take(5).ToList(); if (!string.IsNullOrEmpty(firstname) || !string.IsNullOrEmpty(lastname)) { searchResult = ctx.Profiles.Where(p => p.FirstName.StartsWith(firstname) && p.LastName.StartsWith(lastname)).ToList(); } } else { return(RedirectToAction("Index", "Profile")); } } return(View(searchResult)); }
/* * Method to accept a friend request */ public ActionResult AcceptFriendRequest(int id) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); // Gets the requester friend model and sets Isfriend var friend = currentUserProfile.Friends.FirstOrDefault(f => f.ID == id); friend.IsFriend = true; var requesterProfile = ctx.Profiles.FirstOrDefault(p => p.ID == friend.RequesterId); // Updates the database requesterProfile.Friends.Add(new FriendModel { IsFriend = true, RequesterId = friend.RequesterId, RecieverId = currentUserId }); ctx.SaveChanges(); return(RedirectToAction("FriendList")); }
public PostViewModel ViewMessages(string recieverId) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var vm = new PostViewModel(); return(vm); }
public void PostMessage(string recieverId, string text) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var recieverProfile = ctx.Profiles.FirstOrDefault(p => p.ID == recieverId); recieverProfile.Posts.Add(new PostModel { Author = currentUserId, Reciever = recieverId, Message = text }); ctx.SaveChanges(); }
/* * Method to edit a profile. * This method allows a user to edit only one or all of the profile values. */ public ActionResult EditProfile(ProfileEditViewModel model) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); currentUserProfile.FirstName = model.FirstName ?? currentUserProfile.FirstName; currentUserProfile.LastName = model.LastName ?? currentUserProfile.LastName; currentUserProfile.Description = model.Description ?? currentUserProfile.Description; ctx.SaveChanges(); return(RedirectToAction("Index")); }
/* * Gets and views a full profile */ public ActionResult UserProfile(string id) { var ctx = new ZinderUserDbContext(); var profile = ctx.Profiles.FirstOrDefault(p => p.ID == id); return(View(new ProfileViewModel { ID = profile?.ID, FirstName = profile?.FirstName, LastName = profile?.LastName, DateOfBirth = profile?.DateOfBirth, Description = profile?.Description, ImageUrl = profile?.ImageUrl })); }
/* * Method to view 5 profiles on the home page */ public ActionResult ViewProfiles() { var list = new ProfileListViewModel(); if (User.Identity.IsAuthenticated) { var ctx = new ZinderUserDbContext(); list = new ProfileListViewModel { Profiles = ctx.Profiles.Take(5).ToList() }; } return(PartialView(list)); }
/* * Method to deny a friend request */ public ActionResult DenyFriendRequest(int id) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); // Requester friend model var req = currentUserProfile.Friends.FirstOrDefault(r => r.ID == id); currentUserProfile.Friends.Remove(req); // Remove request from Database ctx.Friends.Remove(req); ctx.SaveChanges(); return(RedirectToAction("FriendList")); }
public ActionResult CreateProfile(ProfileViewModel model, HttpPostedFileBase img) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); // Adds the user if current user profile is not found if (currentUserProfile == null) { // Sets a placeholder image if the user decides to not upload an image var imageUrl = "/Images/Placeholder-Zinder.jpg"; if (img != null && img.ContentLength > 0) { string imgName = Path.GetFileName(img.FileName); string url = Path.Combine(Server.MapPath("~/Images"), imgName); img.SaveAs(url); imageUrl = "/Images/" + imgName; } var dateTimeValue = model.DateOfBirth.Value; // Sets a default DateTime if the user input if below year 1753 if (dateTimeValue.Year <= 1753) { dateTimeValue = DateTime.ParseExact("20/04/1969 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture); } ctx.Profiles.Add(new ProfileModel { ID = currentUserId, FirstName = model.FirstName, LastName = model.LastName, DateOfBirth = dateTimeValue, Description = model.Description, ImageUrl = imageUrl }); } ctx.SaveChanges(); return(RedirectToAction("Index")); }
/* * Method to show the user it's friend list */ public ActionResult FriendList() { var friendList = new List <FriendViewModel>(); if (User.Identity.IsAuthenticated) { try { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); // Gets a list of profiles in which the current user is friend with var profileList = currentUserProfile.Friends.Where(f => f.IsFriend); foreach (var friend in profileList) { // Gets one of the friends full profile var friendProfile = ctx.Profiles.FirstOrDefault(p => p.ID == friend.RequesterId); var vm = new FriendViewModel { ID = friend.RequesterId, FirstName = friendProfile.FirstName, LastName = friendProfile.LastName, RequesterId = friend.ID }; // Adds the friend view model to the list of friends friendList.Add(vm); } return(View(friendList)); } catch { return(RedirectToAction("Index", "Profile")); } } return(View(friendList)); }
/* * Method to send a friend request to a user */ public ActionResult SendFriendRequest(string friendId) { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); // Gets the friends full profile var friendProfile = ctx.Profiles.FirstOrDefault(p => p.ID == friendId); // If the "if" statement fails that means the request doesnt exist and it goes to the catch block try { if (!friendProfile.Friends.FirstOrDefault().RequesterId.Equals(currentUserId) && !friendProfile.Friends.FirstOrDefault().RecieverId.Equals(friendId)) { // Adds the friend model to the ICollection of friends friendProfile.Friends.Add(new FriendModel { IsFriend = false, RequesterId = currentUserId, RecieverId = friendId }); } } // Adds the request catch { // Adds the friend model to the ICollection of friends friendProfile.Friends.Add(new FriendModel { IsFriend = false, RequesterId = currentUserId, RecieverId = friendId }); } ctx.SaveChanges(); return(RedirectToAction("FriendList")); }
/* * Method to view the current user profile */ public ActionResult Index() { var ctx = new ZinderUserDbContext(); var currentUserId = User.Identity.GetUserId(); var currentUserProfile = ctx.Profiles.FirstOrDefault(p => p.ID == currentUserId); var exists = false; if (currentUserProfile != null) { exists = true; } return(View(new ProfileViewModel { FirstName = currentUserProfile?.FirstName, LastName = currentUserProfile?.LastName, DateOfBirth = currentUserProfile?.DateOfBirth, Description = currentUserProfile?.Description, ImageUrl = currentUserProfile?.ImageUrl, Exists = exists })); }