public IHttpActionResult CheckRelationshipStatus(FriendRelationships rel)
 {
     if (ModelState.IsValid)
     {
         var existingRelationship = db.FriendRelationships.FirstOrDefault(x => (x.FromUsername.Equals(rel.FromUsername) && x.ToUsername.Equals(rel.ToUsername)) ||
                                                                          (x.ToUsername.Equals(rel.FromUsername) && x.FromUsername.Equals(rel.ToUsername)));
         if (existingRelationship != null)
         {
             return(Json(new { success = true, isFriend = true }));
         }
     }
     return(BadRequest());
 }
        public IHttpActionResult DeleteFriendRelationships(int id)
        {
            FriendRelationships friendRelationships = db.FriendRelationships.Find(id);

            if (friendRelationships == null)
            {
                return(NotFound());
            }

            db.FriendRelationships.Remove(friendRelationships);
            db.SaveChanges();

            return(Ok(friendRelationships));
        }
        public IHttpActionResult DeleteFriendRelationshipsByUsername(string username, string usernameOfFriend)
        {
            FriendRelationships friendRelationships = db.FriendRelationships.FirstOrDefault(x => x.FromUsername.Equals(username, StringComparison.OrdinalIgnoreCase) && x.ToUsername.Equals(usernameOfFriend, StringComparison.OrdinalIgnoreCase) ||
                                                                                            x.FromUsername.Equals(usernameOfFriend, StringComparison.OrdinalIgnoreCase) && x.ToUsername.Equals(username, StringComparison.OrdinalIgnoreCase));

            if (friendRelationships == null)
            {
                return(NotFound());
            }

            db.FriendRelationships.Remove(friendRelationships);
            db.SaveChanges();

            return(Ok(friendRelationships));
        }
        public async Task <IHttpActionResult> AddFriend(FriendRelationships friendRelationships)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                //always set accepted to false - confirmation pending from other user
                friendRelationships.HasAccepted = false;

                //check that the username the request is coming from exists
                var checkFromUser = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.FromUsername);
                if (checkFromUser != null)
                {
                    //check that the username the request is for exists
                    var checkToUser = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.ToUsername);
                    if (checkToUser != null)
                    {
                        //check for any matching relationship already present (could be reversed so that is also checked)
                        var existingRelationship = db.FriendRelationships.Where(x => (x.FromUsername.Equals(friendRelationships.FromUsername) && x.ToUsername.Equals(friendRelationships.ToUsername)) ||
                                                                                (x.ToUsername.Equals(friendRelationships.FromUsername) && x.FromUsername.Equals(friendRelationships.ToUsername))).ToList();

                        if (existingRelationship.Count > 0)
                        {
                            //existing relationship between the two users exists
                            return(BadRequest("A relationship already exists between these users"));
                        }
                        else
                        {
                            //else add the relationship to the database
                            db.FriendRelationships.Add(friendRelationships);
                            db.SaveChanges();
                            //send a push notification to the requested user's device
                            await PostNotification("gcm", "friend request", checkFromUser.Email, checkToUser.Email);

                            return(Json(new { success = true }));
                        }
                    }
                    return(BadRequest("Incorrect To or From Username"));
                }
                return(BadRequest("Incorrect To or From Username"));
            }
        }
Example #5
0
 public IHttpActionResult Unfollow(FriendRelationships friendRelationships)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     else
     {
         friendRelationships.HasAccepted = false;
         //check that the username the request is coming from exists
         var checkFromUser = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.FromUsername);
         var checkToUser   = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.ToUsername);
         if (checkFromUser != null && checkToUser != null)
         {
             var friend    = checkFromUser.Following.FirstOrDefault(x => x.UserName.Equals(checkToUser.UserName, StringComparison.OrdinalIgnoreCase));
             var altFriend = checkToUser.Followers.FirstOrDefault(x => x.UserName.Equals(checkFromUser.UserName, StringComparison.OrdinalIgnoreCase));
             if (friend != null && altFriend != null)
             {
                 checkFromUser.Following.Remove(friend);
                 checkToUser.Followers.Remove(altFriend);
                 try
                 {
                     db.SaveChanges();
                     return(Json(new { success = true, cause = "You unfollowed " + checkToUser.UserName }));
                 }
                 catch (Exception)
                 {
                 }
             }
             else
             {
                 return(Json(new { success = false, cause = "You are not following " + checkToUser.UserName }));
             }
         }
     }
     return(BadRequest("Incorrect To or From Username"));
 }
Example #6
0
        public async Task <IHttpActionResult> Follow(FriendRelationships friendRelationships)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                //always set accepted to false - confirmation pending from other user
                friendRelationships.HasAccepted = false;

                //check that the username the request is coming from exists
                var checkFromUser = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.FromUsername);
                var checkToUser   = db.Users.FirstOrDefault(x => x.UserName == friendRelationships.ToUsername);
                if (checkFromUser != null && checkToUser != null)
                {
                    bool existingFollowing = false;
                    foreach (ApplicationUser user in checkFromUser.Following)
                    {
                        if (user.UserName.Equals(checkToUser.UserName, StringComparison.OrdinalIgnoreCase))
                        {
                            existingFollowing = true;
                            break;
                        }
                    }
                    if (existingFollowing == true)
                    {
                        return(Json(new { success = false, cause = "You are already following " + checkToUser.UserName }));
                    }
                    else
                    {
                        checkFromUser.Following.Add(checkToUser);
                        checkToUser.Followers.Add(checkFromUser);
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception)
                        {
                        }
                        //send a push notification to the requested user's device
                        await PostNotification("gcm", String.Format("{0} is now following you", checkFromUser.UserName), checkFromUser.Email, checkToUser.Email);

                        var myMessage = new SendGridMessage();
                        myMessage.From = new MailAddress("*****@*****.**");
                        myMessage.AddTo(string.Format(@"{0} <{1}>", checkToUser.UserName, checkToUser.Email));
                        myMessage.Subject = string.Format("{0} is now following you", checkFromUser.UserName);
                        myMessage.Html    = "<p>New Follower</p>";
                        myMessage.Text    = "New Follower plain text!";
                        var apiKey       = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
                        var transportWeb = new Web(apiKey);

                        // Send the email.
                        await transportWeb.DeliverAsync(myMessage);

                        return(Json(new { success = true }));
                    }
                }
            }
            return(BadRequest("Incorrect To or From Username"));
        }