Beispiel #1
0
        /// <summary>
        /// 异步的执行数据库操作
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="operation">操作内容</param>
        /// <param name="parameter">操作附带的参数</param>
        public static async Task <RequestResult> ExecuteDBAction(User user, string operation, string parameter = "")
        {
            if (user != null && (!operation?.Equals(string.Empty)) == true && parameter != null)
            {
                RequestResult result = new RequestResult();
                switch (operation)
                {
                case "Signup":
                    switch (await DatabaseActions.CheckUserExistsAsync(user.UserName))
                    {
                    case true:
                        result.Success = false;
                        break;

                    case false:
                        if (await DatabaseActions.UserSignupAsync(user))
                        {
                            result.Success = true;
                        }
                        break;
                    }
                    break;

                case "Login":
                    switch (await DatabaseActions.CheckUserExistsAsync(user.UserName, user.Password))
                    {
                    case true:
                        if (await DatabaseActions.GetUserStateAsync(user.UserName) == 0)
                        {
                            if (await DatabaseActions.UserOnlineAsync(user.UserName))
                            {
                                result.Success = true;
                            }
                        }
                        break;

                    case false:
                        result.Success = false;
                        break;
                    }
                    break;

                case "AlterInfo":
                    if (await DatabaseActions.AlterUserInfoAsync(user) == true)
                    {
                        result.Success = true;
                    }
                    break;

                case "AlterPassword":
                    result.Success = await DatabaseActions.AlterUserPasswordAsync(user.UserName, user.Password, parameter);

                    break;

                case "GetUserInfo":
                    User newUser = await DatabaseActions.GetUserInfoAsync(user.UserName);

                    if (newUser != null)
                    {
                        result.Success = true;
                        result.Object  = newUser;
                    }
                    break;

                case "GetFriends":
                    User[] newUsers = await DatabaseActions.GetUserFriendsAsync(user.UserName);

                    if (newUsers != null)
                    {
                        result.Success = true;
                        result.Object  = newUsers;
                    }
                    break;

                case "AddFriend":
                    result.Success = await DatabaseActions.UserAddFriendAsync(user.UserName, parameter);

                    break;

                case "RemoveFriend":
                    result.Success = await DatabaseActions.UserRemoveFriendAsync(user.UserName, parameter);

                    break;
                }
                return(result);
            }
            return(null);
        }
 private async void MiniServer_ClientOffline(object sender, ServerNotifyEventArgs e)
 {
     await DatabaseActions.UserOfflineAsync(e.Message);
 }