/// <summary> /// This method adds user to DbSet and saves changes to database. /// </summary> /// <param name="userToAdd">User to add.</param> /// <param name="id">User id.</param> /// <returns>True if added, false if not.</returns> public bool CreateUser(vwUser userToAdd, out int id) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { tblUser newUser = new tblUser { DateOfBirth = userToAdd.DateOfBirth, FirstName = userToAdd.FirstName, LastName = userToAdd.LastName, Gender = userToAdd.Gender, Location = userToAdd.Location, MarriageStatus = userToAdd.MarriageStatus, Password = Encryption.EncryptPassword(userToAdd.Password), PhoneNumber = userToAdd.PhoneNumber, Profession = userToAdd.Profession, Username = userToAdd.Username }; context.tblUsers.Add(newUser); context.SaveChanges(); id = newUser.UserId; return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); id = 0; return(false); } }
/// <summary> /// This method increment number of likes of post. /// </summary> /// <param name="post">Post to be liked.</param> public void LikePost(vwFriendPost post, vwUser userWhoLikePost) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { int id = context.vwFriendPosts.Where(x => x.PostId == post.PostId).Select(x => x.PostId).FirstOrDefault(); tblPost postToLike = context.tblPosts.Where(x => x.PostId == id).FirstOrDefault(); if (postToLike != null) { postToLike.NumberOfLikes++; context.SaveChanges(); } tblLikedPost likedPost = new tblLikedPost { UserId = userWhoLikePost.UserId, PostId = postToLike.PostId }; context.tblLikedPosts.Add(likedPost); context.SaveChanges(); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
public List <vwUser> UsersWhoLikedPost(vwFriendPost post) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { List <int> usersId = context.tblLikedPosts.Where(x => x.PostId == post.PostId).Select(x => x.UserId).ToList(); List <vwUser> users = new List <vwUser>(); foreach (var item in usersId) { vwUser user = context.vwUsers.Where(x => x.UserId == item).FirstOrDefault(); if (user != null) { users.Add(user); } } return(users); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method creates a list of posts of other users which are not friends with forwarded user. /// </summary> /// <param name="user">User.</param> /// <returns>List of posts.</returns> public List <vwPost> GetOtherUsersPost(vwUser user) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { var friendPost = GetFriendsPost(user); List <vwPost> postOfFriends = new List <vwPost>(); foreach (var item in friendPost) { var post = context.vwPosts.Where(x => x.PostId == item.PostId).FirstOrDefault(); if (post != null) { postOfFriends.Add(post); } } List <vwPost> posts = context.vwPosts.ToList(); foreach (var item in postOfFriends) { if (posts.Contains(item)) { posts.Remove(item); } } return(posts.Where(x => x.UserId != user.UserId).ToList()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method changes status of request to declined. /// </summary> /// <param name="requestToDecline">Request to decline.</param> /// <returns>True if declined, false if not.</returns> public bool DeclineRequest(vwRequest requestToDecline) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { var request = context.tblRequests.Where(x => x.RequestId == requestToDecline.RequestId).FirstOrDefault(); if (request != null) { request.Status = "Declined"; context.SaveChanges(); return(true); } else { return(false); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method creates a list of data from table of all users. /// </summary> /// <returns>List of users.</returns> public List <tblUser> GetUsers() { try { using (BetweenUsEntities context = new BetweenUsEntities()) { return(context.tblUsers.ToList()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method creates a list of user friends posts. /// </summary> /// <param name="user">User.</param> /// <returns>List of friends posts.</returns> public List <vwFriendPost> GetFriendsPost(vwUser user) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { return(context.vwFriendPosts.Where(x => x.RecipientId == user.UserId || x.SenderId == user.UserId).Where(x => x.UserId != user.UserId).ToList()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This methods finds user based on forwarded username and password. /// </summary> /// <param name="username">User username.</param> /// <param name="password">User password.</param> /// <returns>User if finded, null if not.</returns> public vwUser FindUser(string username, string password) { string encryptedPassword = Encryption.EncryptPassword(password); try { using (BetweenUsEntities context = new BetweenUsEntities()) { return(context.vwUsers.Where(x => x.Username == username && x.Password == encryptedPassword).FirstOrDefault()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method creates a list of users which are not friends. /// </summary> /// <param name="user">User to check.</param> /// <returns>List of users.</returns> public List <vwUser> UsersNotInFriends(vwUser user) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { //find all users in relationship with this user List <int> recipient = context.vwRequests.Where(x => x.SenderId == user.UserId).Select(x => x.RecipientId).ToList(); List <int> sender = context.vwRequests.Where(x => x.RecipientId == user.UserId).Select(x => x.SenderId).ToList(); List <vwUser> users = new List <vwUser>(); foreach (var item in recipient) { var userToAdd = context.vwUsers.Where(x => x.UserId == item).FirstOrDefault(); if (userToAdd != null) { users.Add(userToAdd); } } foreach (var item in sender) { var userToAdd = context.vwUsers.Where(x => x.UserId == item).FirstOrDefault(); if (userToAdd != null) { users.Add(userToAdd); } } List <vwUser> distinct = users.Distinct().ToList(); //removing friends from list of all users List <vwUser> listOfAllUser = context.vwUsers.ToList(); foreach (var item in distinct) { if (listOfAllUser.Contains(item)) { listOfAllUser.Remove(item); } } return(listOfAllUser.Where(x => x.UserId != user.UserId).ToList()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public bool IsUserLikedPost(vwFriendPost post, vwUser user) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { tblLikedPost likedPost = context.tblLikedPosts.Where(x => x.PostId == post.PostId && x.UserId == user.UserId).FirstOrDefault(); if (likedPost != null) { return(true); } else { return(false); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method adds request to DbSet and saves changes to database. /// </summary> /// <param name="sender">Sender of request.</param> /// <param name="recipient">Recipient of request.</param> /// <returns>True if added, false if not.</returns> public bool SendRequest(vwUser sender, vwUser recipient) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { tblRequest request = new tblRequest { RecipientId = recipient.UserId, SenderId = sender.UserId, Status = "Pending" }; context.tblRequests.Add(request); context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method adds post to DbSet and saves changes to database. /// </summary> /// <param name="postToAdd">Post to be added.</param> /// <returns>True if post is added, false if not.</returns> public bool CreatePost(vwPost postToAdd) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { tblPost newPost = new tblPost { DateOfPost = DateTime.Now.Date, PostContent = postToAdd.PostContent, UserId = postToAdd.UserId, NumberOfLikes = 0 }; context.tblPosts.Add(newPost); context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }