Ejemplo n.º 1
0
        /// <summary>
        /// Create Friend Request
        /// </summary>
        /// <param name="user">The user ID we adding for friend request</param>
        /// <returns>new relationship</returns>
        public tblRelationship CreateFriendRequest(tblUser user)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    tblRelationship newRelationShip = new tblRelationship
                    {
                        RelationshipStatus = "Pending",
                        User1ID            = LoggedInUser.CurrentUser.UserID,
                        User2ID            = user.UserID,
                    };

                    context.tblRelationships.Add(newRelationShip);
                    context.SaveChanges();

                    return(newRelationShip);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds or edits a user in the database
        /// </summary>
        /// <param name="user">The user ID we are adding or editing</param>
        /// <returns>The new or edited user</returns>
        public tblUser AddUser(tblUser user)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    if (user.UserID == 0)
                    {
                        tblUser newUser = new tblUser
                        {
                            FirstName    = user.FirstName,
                            LastName     = user.LastName,
                            Email        = user.Email,
                            Gender       = user.Gender,
                            DateOfBirth  = user.DateOfBirth,
                            UserLocation = user.UserLocation,
                            Username     = user.Username,
                            UserPassword = PasswordHasher.Hash(user.UserPassword)
                        };

                        context.tblUsers.Add(newUser);
                        context.SaveChanges();
                        user.UserID = newUser.UserID;

                        return(user);
                    }
                    else
                    {
                        tblUser userToEdit = (from ss in context.tblUsers where ss.UserID == user.UserID select ss).First();

                        userToEdit.FirstName    = user.FirstName;
                        userToEdit.LastName     = user.LastName;
                        userToEdit.Email        = user.Email;
                        userToEdit.Gender       = user.Gender;
                        userToEdit.DateOfBirth  = user.DateOfBirth;
                        userToEdit.UserLocation = user.UserLocation;
                        userToEdit.Username     = user.Username;

                        // Save only if password changed
                        if (userToEdit.UserPassword != user.UserPassword)
                        {
                            userToEdit.UserPassword = PasswordHasher.Hash(user.UserPassword);
                        }

                        userToEdit.UserID = user.UserID;
                        context.SaveChanges();

                        return(user);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Deny Friend Request
 /// </summary>
 /// <param name="user">The friend request we are deleting</param>
 public void DenyFriendRequest(tblRelationship user)
 {
     try
     {
         using (BetweenUsDBEntities context = new BetweenUsDBEntities())
         {
             tblRelationship requestToDelete = (from ss in context.tblRelationships where ss.RelationshipID == user.RelationshipID select ss).First();
             context.tblRelationships.Remove(requestToDelete);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Get all post likes from the database
 /// </summary>
 /// <returns>The list of all post likes</returns>
 public List <tblPostLike> GetAllPostLikes()
 {
     try
     {
         using (BetweenUsDBEntities context = new BetweenUsDBEntities())
         {
             List <tblPostLike> list = new List <tblPostLike>();
             list = (from x in context.tblPostLikes select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Get all data about posts from the database
 /// </summary>
 /// <returns>The list of all posts</returns>
 public List <tblPost> GetAllPosts()
 {
     try
     {
         using (BetweenUsDBEntities context = new BetweenUsDBEntities())
         {
             List <tblPost> list = new List <tblPost>();
             list = (from x in context.tblPosts select x).ToList();
             return(list.OrderByDescending(x => x.DateOfPost).ToList());
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Get all relationships users
 /// </summary>
 /// <param name="user">Current User</param>
 /// <returns>The list of all relationship users</returns>
 public List <tblRelationship> GetAllRelationshipsUsers(tblUser user)
 {
     try
     {
         using (BetweenUsDBEntities context = new BetweenUsDBEntities())
         {
             List <tblRelationship> list = new List <tblRelationship>();
             list = context.tblRelationships.Where(x => x.User1ID == user.UserID || x.User2ID == user.UserID).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Add a post to the database
        /// </summary>
        /// <param name="post">The post ID we are adding or editing</param>
        /// <returns>The new post</returns>
        public tblPost AddPost(tblPost post)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    if (post.PostID == 0)
                    {
                        tblPost newPost = new tblPost
                        {
                            DateOfPost    = DateTime.Now,
                            PostText      = post.PostText,
                            NumberOfLikes = 0,
                            UserID        = LoggedInUser.CurrentUser.UserID,
                        };

                        context.tblPosts.Add(newPost);
                        context.SaveChanges();
                        post.PostID = newPost.PostID;

                        return(post);
                    }
                    else
                    {
                        tblPost postToEdit = (from ss in context.tblPosts where ss.PostID == post.PostID select ss).First();

                        // Increase number of likes in the post
                        postToEdit.NumberOfLikes = postToEdit.NumberOfLikes + 1;
                        post.NumberOfLikes       = postToEdit.NumberOfLikes + 1;
                        postToEdit.PostID        = post.PostID;
                        context.SaveChanges();

                        return(post);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Accept Friend Request
        /// </summary>
        /// <param name="user">The user we accepting for friend request</param>
        /// <returns>edited relationship</returns>
        public tblRelationship AcceptFriendRequest(tblRelationship user)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    tblRelationship requestToEdit = (from ss in context.tblRelationships where ss.RelationshipID == user.RelationshipID select ss).First();
                    requestToEdit.RelationshipStatus = "Accepted";
                    user.RelationshipStatus          = "Accepted";
                    context.SaveChanges();

                    return(user);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Add Like
        /// </summary>
        /// <param name="post">The post ID we are adding a like to</param>
        public void AddLike(tblPost post)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    tblPostLike newPostLike = new tblPostLike
                    {
                        PostID = post.PostID,
                        UserID = LoggedInUser.CurrentUser.UserID,
                    };

                    context.tblPostLikes.Add(newPostLike);
                    context.SaveChanges();

                    // Update the number of likes in the post
                    AddPost(post);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get all data about users but the current one from the database
        /// </summary>
        /// <returns>The list of all users</returns>
        public List <tblUser> GetAllUsersButPanding()
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    List <tblUser>         list             = new List <tblUser>();
                    List <tblRelationship> allRelationShips = GetAllRelationshipsUsers(LoggedInUser.CurrentUser).ToList();

                    list = (from x in context.tblUsers select x).ToList();

                    // find the current user before removing them from the list
                    tblUser userToRemove = (from r in context.tblUsers where r.UserID == LoggedInUser.CurrentUser.UserID select r).First();
                    list.Remove(userToRemove);

                    // remove users that already have a relationship with the logged in user
                    for (int i = 0; i < allRelationShips.Count; i++)
                    {
                        for (int j = 0; j < list.Count; j++)
                        {
                            if (allRelationShips[i].User1ID == list[j].UserID ||
                                allRelationShips[i].User2ID == list[j].UserID)
                            {
                                list.Remove(list[j]);
                            }
                        }
                    }
                    return(list);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Search if user with that ID exists in the user table
        /// </summary>
        /// <param name="userID">Takes the user id that we want to search for</param>
        /// <returns>The user</returns>
        public tblUser GetUser(int userID)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    tblUser result = (from x in context.tblUsers where x.UserID == userID select x).FirstOrDefault();

                    if (result != null)
                    {
                        return(result);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception " + ex.Message.ToString());
                return(null);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Search if user with that ID exists in the user table
        /// </summary>
        /// <param name="userID">Takes the user id that we want to search for</param>
        /// <returns>True if the user exists</returns>
        private bool IsUserID(int userID)
        {
            try
            {
                using (BetweenUsDBEntities context = new BetweenUsDBEntities())
                {
                    int result = (from x in context.tblUsers where x.UserID == userID select x.UserID).FirstOrDefault();

                    if (result != 0)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception " + ex.Message.ToString());
                return(false);
            }
        }