예제 #1
0
 private void OnClose()
 {
     AdminDashboardForm.LoadShopItemList();
     AdminDashboardForm.Show();
     Hide();
     Dispose();
 }
 private void OnClose()
 {
     AdminDashboardForm.LoadComputerActive();
     AdminDashboardForm.LoadComputerItemList();
     AdminDashboardForm.Show();
     Hide();
     Dispose();
 }
        private async void ButtonLogin_Click(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
            {
                control.Enabled = false;
            }

            var userUsername = TextBoxUsername.Text;
            var userPassword = TextBoxPassword.Text;

            try
            {
                var user = await $"{Constant.URL}/api/user/auth"
                           .PostJsonAsync(new
                {
                    Username = userUsername,
                    Password = userPassword
                })
                           .ReceiveJson <User>();

                switch (ComboBoxAccess.Text)
                {
                case "User":
                {
                    if (user.Duration <= 0)
                    {
                        MessageBox.Show("You do not have any duration to use the computer");
                    }
                    else
                    {
                        var PCID          = Int64.Parse(TextBoxPCID.Text);
                        var formDashboard = new UserDashboardForm(userUsername, userPassword, PCID, this);
                        formDashboard.Show(this);
                        Hide();
                    }
                }
                break;

                case "Staff":
                {
                    if (user.Role != User.Roles.Staff)
                    {
                        MessageBox.Show($"{user.Role} can not login as staff!");
                    }
                    else
                    {
                        var formDashboard = new StaffDashboardForm(userUsername, userPassword, this);
                        formDashboard.Show();
                        Hide();
                    }
                }
                break;

                case "Admin":
                {
                    if (user.Role != User.Roles.Admin)
                    {
                        MessageBox.Show($"{user.Role} can not login as admin!");
                    }
                    else
                    {
                        var formDashboard = new AdminDashboardForm(userUsername, userPassword, this);
                        formDashboard.Show();
                        Hide();
                    }
                }
                break;

                default:
                    MessageBox.Show("No such roles exists!");
                    break;
                }
            }
            catch (FlurlHttpException ex)
            {
                if (ex.Call.HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    MessageBox.Show("Wrong username or password");
                }
                else
                {
                    var errorResp = await ex.Call.Response.GetStringAsync();

                    MessageBox.Show($"{ex.Message}\n{errorResp}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            foreach (Control control in Controls)
            {
                control.Enabled = true;
            }
        }