/* * 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 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")); }
/* * 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 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")); }