Пример #1
0
        public async Task <bool> CreateFriendship(string username)
        {
            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var friendUserId = await GetUserId(username);

                    var userExists = UserExists(friendUserId);
                    if (!userExists)
                    {
                        return(false);
                    }

                    var alreadyFriends = await UserIsAlreadyFriend(friendUserId);

                    if (alreadyFriends)
                    {
                        return(false);
                    }

                    var friendship = new Friendship
                    {
                        UserId   = AccountService.User.Id,
                        FriendId = friendUserId,
                        Accepted = false
                    };

                    await client.GetTable <Friendship>().InsertAsync(friendship);

                    return(true);
                }
            }
        }
Пример #2
0
        public async Task <bool> AcceptFriendship(User friend)
        {
            PendingFriends.Remove(friend);
            Friends.Add(friend);

            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var existingFriendshipList = await client.GetTable <Friendship>()
                                                 .Where(friendship => friendship.UserId == friend.Id)
                                                 .Where(friendship => friendship.FriendId == AccountService.Account.UserId).Select(user => user.Id).ToListAsync();

                    if (existingFriendshipList.Count == 0)
                    {
                        return(false);
                    }
                    else
                    {
                        var friendship = await client.GetTable <Friendship>().LookupAsync(existingFriendshipList[0]);

                        friendship.Accepted = true;
                        await client.GetTable <Friendship>().UpdateAsync(friendship);

                        return(true);
                    }
                }
            }
        }
Пример #3
0
 private async Task <User> GetCurrentUser()
 {
     using (var handler = new ZumoAuthHeaderHandler(this))
     {
         using (var client = MobileServiceClientFactory.CreateClient(handler))
         {
             return(await client.GetTable <User>().LookupAsync(Account.UserId));
         }
     }
 }
Пример #4
0
        public async Task <bool> DeleteAccount()
        {
            bool result;

            try
            {
                using (var handler = new AuthenticationHandler(Preferences, this))
                {
                    using (var client = MobileServiceClientFactory.CreateClient(handler))
                    {
                        // Account
                        var accountTable = client.GetTable <Account>();
                        await accountTable.DeleteAsync(Account);

                        // User
                        var userTable = client.GetTable <User>();
                        await userTable.DeleteAsync(User);

                        // Friendships
                        var friendships = await client.GetTable <Friendship>()
                                          .Where(friendship => friendship.UserId == User.Id).Select(friendship => friendship).ToListAsync();

                        friendships.AddRange(await client.GetTable <Friendship>()
                                             .Where(friendship => friendship.FriendId == User.Id).Select(friendship => friendship).ToListAsync());

                        var friendshipsTable = client.GetTable <Friendship>();
                        foreach (var friend in friendships)
                        {
                            await friendshipsTable.DeleteAsync(friend);
                        }

                        // Moments
                        var moments = await client.GetTable <Moment>()
                                      .Where(moment => moment.SenderUserId == User.Id).Select(moment => moment).ToListAsync();

                        moments.AddRange(await client.GetTable <Moment>()
                                         .Where(moment => moment.RecipientUserId == User.Id).Select(moment => moment).ToListAsync());

                        var momentsTable = client.GetTable <Moment>();
                        foreach (var moment in moments)
                        {
                            await momentsTable.DeleteAsync(moment);
                        }
                    }
                }

                result = true;
            }
            catch
            {
                result = false;
            }

            return(result);
        }
Пример #5
0
        public async Task Register(Account account, User user)
        {
            using (var client = MobileServiceClientFactory.CreateClient())
            {
                await client.GetTable <User>().InsertAsync(user);
            }

            account.UserId = user.Id;

            await Insert(account, false);
        }
Пример #6
0
        private async Task <Account> GetCurrentAccount(Account account)
        {
            using (var handler = new ZumoAuthHeaderHandler(this))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var currentAccount = await client.GetTable <Account>()
                                         .Where(acct => acct.Username == account.Username)
                                         .Select(acct => acct).ToListAsync();

                    return(currentAccount[0]);
                }
            }
        }
Пример #7
0
        private async Task Insert(Account account, bool isLogin)
        {
            using (var handler = new AuthenticationHandler(Preferences, this))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var table      = client.GetTable <Account>();
                    var parameters = new Dictionary <string, string> {
                        { "login", isLogin.ToString().ToLower() }
                    };

                    await table.InsertAsync(account, parameters);
                }
            }
        }
Пример #8
0
        public async Task AddTestData()
        {
            using (var handler = new ZumoAuthHeaderHandler())
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var data = new TestData {
                        DateCreated = DateTime.Now
                    };

                    var table = client.GetTable <TestData>();

                    await table.InsertAsync(data);
                }
            }
        }
Пример #9
0
        public async Task DenyFriendship(User friend)
        {
            PendingFriends.Remove(friend);

            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var existingFriendshipList = await client.GetTable <Friendship>()
                                                 .Where(friendship => friendship.UserId == friend.Id)
                                                 .Where(friendship => friendship.FriendId == AccountService.Account.UserId).ToListAsync();

                    var existingFriendship = existingFriendshipList[0];
                    await client.GetTable <Friendship>().DeleteAsync(existingFriendship);
                }
            }
        }
Пример #10
0
        public async Task <TestData> GetMostRecentItem()
        {
            using (var handler = new ZumoAuthHeaderHandler())
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var table = client.GetTable <TestData>();

                    var query = table.Take(1)
                                .OrderByDescending(d => d.DateCreated);

                    var data = await query.ToListAsync();

                    return(data.First());
                }
            }
        }
Пример #11
0
        private async Task DoInsert(string username, string password, string email, bool isLogin)
        {
            Account account = new Account {
                Username = username,
                Password = password,
                Email    = email
            };

            using (AuthenticationHandler handler = new AuthenticationHandler()) {
                using (MobileServiceClient client = MobileServiceClientFactory.CreateClient(handler)) {
                    var table = client.GetTable <Account>();
                    Dictionary <string, string> parameters = new Dictionary <string, string> {
                        { "login", isLogin.ToString().ToLower() }
                    };

                    await table.InsertAsync(account, parameters);
                }
            }
        }
Пример #12
0
        public async Task RefreshPendingFriendsList()
        {
            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var dictionary = new Dictionary <string, string>
                    {
                        { "getFriends", "false" },
                        { "userId", AccountService.Account.UserId }
                    };

                    PendingFriends.Clear();
                    var friends = await client.InvokeApiAsync <List <User> >("friends", System.Net.Http.HttpMethod.Get, dictionary);

                    var sortedFriends = friends.OrderBy(user => user.Name).ToList();
                    sortedFriends.ForEach(user => PendingFriends.Add(user));
                }
            }
        }
Пример #13
0
        private async Task <string> GetUserId(string username)
        {
            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var userId = await client.GetTable <Account>().Where(account => account.Username == username)
                                 .Select(account => account.UserId).ToListAsync();

                    if (userId.Count != 0)
                    {
                        return(userId[0]);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
Пример #14
0
        public async Task <bool> UserIsAlreadyFriend(string friendUserId)
        {
            using (var handler = new ZumoAuthHeaderHandler(AccountService))
            {
                using (var client = MobileServiceClientFactory.CreateClient(handler))
                {
                    var friendships = client.GetTable <Friendship>();
                    var friend      = await friendships.CreateQuery().Where(friendship => friendship.UserId == AccountService.Account.UserId)
                                      .Where(friendship => friendship.FriendId == friendUserId).ToListAsync();

                    if (friend.Count == 0)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
        }