Ejemplo n.º 1
0
 /// <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 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);
     }
 }
Ejemplo n.º 6
0
 public List <vwSong> GetUserSongs(vwUser user)
 {
     try
     {
         using (AudioPlayerEntities context = new AudioPlayerEntities())
         {
             return(context.vwSongs.Where(x => x.UserId == user.UserId).ToList());
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Ejemplo n.º 7
0
 /// <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);
     }
 }
Ejemplo n.º 9
0
 /// <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);
     }
 }
Ejemplo n.º 10
0
 public bool CreateSong(vwUser user, vwSong song)
 {
     try
     {
         using (AudioPlayerEntities context = new AudioPlayerEntities())
         {
             tblSong newSong = new tblSong
             {
                 Author   = song.Author,
                 Duration = song.Duration,
                 Name     = song.Name,
                 UserId   = user.UserId
             };
             context.tblSongs.Add(newSong);
             context.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }