From cd3a375b6284fc55a3c5610e3996810271b92741 Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Fri, 31 Mar 2023 01:49:56 +0200 Subject: [PATCH] winforms users crud --- .../HouseData/Models/User.cs | 4 + .../HouseData/Repositories/UserRepository.cs | 2 +- .../WinForms/Dashboard.Designer.cs | 124 +++++++++++++++++ StudentHouseDashboard/WinForms/Dashboard.cs | 85 ++++++++++++ StudentHouseDashboard/WinForms/Dashboard.resx | 60 ++++++++ StudentHouseDashboard/WinForms/Login.cs | 6 +- .../WinForms/UserForm.Designer.cs | 128 ++++++++++++++++++ StudentHouseDashboard/WinForms/UserForm.cs | 63 +++++++++ StudentHouseDashboard/WinForms/UserForm.resx | 60 ++++++++ 9 files changed, 530 insertions(+), 2 deletions(-) create mode 100644 StudentHouseDashboard/WinForms/Dashboard.Designer.cs create mode 100644 StudentHouseDashboard/WinForms/Dashboard.cs create mode 100644 StudentHouseDashboard/WinForms/Dashboard.resx create mode 100644 StudentHouseDashboard/WinForms/UserForm.Designer.cs create mode 100644 StudentHouseDashboard/WinForms/UserForm.cs create mode 100644 StudentHouseDashboard/WinForms/UserForm.resx diff --git a/StudentHouseDashboard/HouseData/Models/User.cs b/StudentHouseDashboard/HouseData/Models/User.cs index 89a529d..36c563d 100644 --- a/StudentHouseDashboard/HouseData/Models/User.cs +++ b/StudentHouseDashboard/HouseData/Models/User.cs @@ -45,5 +45,9 @@ namespace StudentHouseDashboard.Models get => role; set => role = value; } + public override string ToString() + { + return $"{ID}: {Name} ({Role})"; + } } } \ No newline at end of file diff --git a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs b/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs index c3168ff..8b87abe 100644 --- a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs +++ b/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs @@ -141,7 +141,7 @@ namespace StudentHouseDashboard.Repositories using (SqlConnection conn = CreateConnection()) { string sql = "UPDATE Users " + - "SET Name = 'Deleted User @id', Password = '0', Role = @role " + + "SET Name = 'Deleted User @id', Password = '0'" + "WHERE ID = @id;"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@id", id); diff --git a/StudentHouseDashboard/WinForms/Dashboard.Designer.cs b/StudentHouseDashboard/WinForms/Dashboard.Designer.cs new file mode 100644 index 0000000..5837ec5 --- /dev/null +++ b/StudentHouseDashboard/WinForms/Dashboard.Designer.cs @@ -0,0 +1,124 @@ +namespace WinForms +{ + partial class Dashboard + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + lblUserStatus = new Label(); + lbUsers = new ListBox(); + btnCreateUser = new Button(); + btnDeleteUser = new Button(); + btnUpdateUser = new Button(); + btnViewUser = new Button(); + SuspendLayout(); + // + // lblUserStatus + // + lblUserStatus.AutoSize = true; + lblUserStatus.Location = new Point(13, 18); + lblUserStatus.Name = "lblUserStatus"; + lblUserStatus.Size = new Size(80, 15); + lblUserStatus.TabIndex = 0; + lblUserStatus.Text = "Logged in as: "; + // + // lbUsers + // + lbUsers.FormattingEnabled = true; + lbUsers.ItemHeight = 15; + lbUsers.Location = new Point(13, 86); + lbUsers.Name = "lbUsers"; + lbUsers.Size = new Size(318, 154); + lbUsers.TabIndex = 1; + // + // btnCreateUser + // + btnCreateUser.Location = new Point(13, 257); + btnCreateUser.Name = "btnCreateUser"; + btnCreateUser.Size = new Size(75, 23); + btnCreateUser.TabIndex = 2; + btnCreateUser.Text = "New User"; + btnCreateUser.UseVisualStyleBackColor = true; + btnCreateUser.Click += btnCreateUser_Click; + // + // btnDeleteUser + // + btnDeleteUser.Location = new Point(94, 257); + btnDeleteUser.Name = "btnDeleteUser"; + btnDeleteUser.Size = new Size(75, 23); + btnDeleteUser.TabIndex = 3; + btnDeleteUser.Text = "Delete User"; + btnDeleteUser.UseVisualStyleBackColor = true; + btnDeleteUser.Click += btnDeleteUser_Click; + // + // btnUpdateUser + // + btnUpdateUser.Location = new Point(175, 257); + btnUpdateUser.Name = "btnUpdateUser"; + btnUpdateUser.Size = new Size(75, 23); + btnUpdateUser.TabIndex = 4; + btnUpdateUser.Text = "Edit User"; + btnUpdateUser.UseVisualStyleBackColor = true; + btnUpdateUser.Click += btnUpdateUser_Click; + // + // btnViewUser + // + btnViewUser.Location = new Point(256, 257); + btnViewUser.Name = "btnViewUser"; + btnViewUser.Size = new Size(75, 23); + btnViewUser.TabIndex = 5; + btnViewUser.Text = "View User"; + btnViewUser.UseVisualStyleBackColor = true; + btnViewUser.Click += btnViewUser_Click; + // + // Dashboard + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(359, 302); + Controls.Add(btnViewUser); + Controls.Add(btnUpdateUser); + Controls.Add(btnDeleteUser); + Controls.Add(btnCreateUser); + Controls.Add(lbUsers); + Controls.Add(lblUserStatus); + Name = "Dashboard"; + Text = "Dashboard"; + FormClosed += Dashboard_FormClosed; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label lblUserStatus; + private ListBox lbUsers; + private Button btnCreateUser; + private Button btnDeleteUser; + private Button btnUpdateUser; + private Button btnViewUser; + } +} \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/Dashboard.cs b/StudentHouseDashboard/WinForms/Dashboard.cs new file mode 100644 index 0000000..6db7277 --- /dev/null +++ b/StudentHouseDashboard/WinForms/Dashboard.cs @@ -0,0 +1,85 @@ +using StudentHouseDashboard.Managers; +using StudentHouseDashboard.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace WinForms +{ + public partial class Dashboard : Form + { + Login loginForm; + public Dashboard(Login loginForm, User user) + { + this.loginForm = loginForm; + InitializeComponent(); + lblUserStatus.Text = $"Logged in as: {user.Role} {user.Name}"; + if (user.Role == UserRole.ADMIN || user.Role == UserRole.MANAGER) + { + btnCreateUser.Enabled = true; + btnDeleteUser.Enabled = true; + btnUpdateUser.Enabled = true; + } + else + { + btnCreateUser.Enabled = false; + btnDeleteUser.Enabled = false; + btnUpdateUser.Enabled = false; + } + RefreshLists(); + } + + private void btnCreateUser_Click(object sender, EventArgs e) + { + UserForm userForm = new UserForm(null, false); + userForm.ShowDialog(); + RefreshLists(); + } + + private void btnDeleteUser_Click(object sender, EventArgs e) + { + User currentUser = (User)lbUsers.SelectedItem; + if (MessageBox.Show($"Are you sure you want to delete\n{currentUser.Name}\n{currentUser.Role}", "Delete user", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + UserManager userManager = new UserManager(); + userManager.DisableUser(currentUser.ID); + } + RefreshLists(); + } + + private void btnUpdateUser_Click(object sender, EventArgs e) + { + UserForm userForm = new UserForm((User)lbUsers.SelectedItem, false); + userForm.ShowDialog(); + RefreshLists(); + } + + private void btnViewUser_Click(object sender, EventArgs e) + { + UserForm userForm = new UserForm((User)lbUsers.SelectedItem, true); + userForm.ShowDialog(); + RefreshLists(); + } + + private void RefreshLists() + { + UserManager userManager = new UserManager(); + lbUsers.Items.Clear(); + foreach (User _user in userManager.GetAllUsers()) + { + lbUsers.Items.Add(_user); + } + } + + private void Dashboard_FormClosed(object sender, FormClosedEventArgs e) + { + loginForm.Show(); + } + } +} diff --git a/StudentHouseDashboard/WinForms/Dashboard.resx b/StudentHouseDashboard/WinForms/Dashboard.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/StudentHouseDashboard/WinForms/Dashboard.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/Login.cs b/StudentHouseDashboard/WinForms/Login.cs index c55e30e..641dbd5 100644 --- a/StudentHouseDashboard/WinForms/Login.cs +++ b/StudentHouseDashboard/WinForms/Login.cs @@ -20,8 +20,12 @@ namespace WinForms } else { - MessageBox.Show($"Welcome, {user.Name}", "Login successful", MessageBoxButtons.OK, MessageBoxIcon.Information); + Dashboard dashboard = new Dashboard(this, user); + this.Hide(); + dashboard.Show(); } + tbUsername.Text = ""; + tbPassword.Text = ""; } } } \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/UserForm.Designer.cs b/StudentHouseDashboard/WinForms/UserForm.Designer.cs new file mode 100644 index 0000000..4b88715 --- /dev/null +++ b/StudentHouseDashboard/WinForms/UserForm.Designer.cs @@ -0,0 +1,128 @@ +namespace WinForms +{ + partial class UserForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + lblUsername = new Label(); + tbUsername = new TextBox(); + lblPassword = new Label(); + tbPassword = new TextBox(); + lblUserRole = new Label(); + cbUserRole = new ComboBox(); + btnSave = new Button(); + SuspendLayout(); + // + // lblUsername + // + lblUsername.AutoSize = true; + lblUsername.Location = new Point(12, 9); + lblUsername.Name = "lblUsername"; + lblUsername.Size = new Size(63, 15); + lblUsername.TabIndex = 0; + lblUsername.Text = "Username:"; + // + // tbUsername + // + tbUsername.Location = new Point(84, 6); + tbUsername.Name = "tbUsername"; + tbUsername.Size = new Size(121, 23); + tbUsername.TabIndex = 1; + // + // lblPassword + // + lblPassword.AutoSize = true; + lblPassword.Location = new Point(12, 38); + lblPassword.Name = "lblPassword"; + lblPassword.Size = new Size(60, 15); + lblPassword.TabIndex = 2; + lblPassword.Text = "Password:"; + // + // tbPassword + // + tbPassword.Location = new Point(84, 35); + tbPassword.Name = "tbPassword"; + tbPassword.Size = new Size(121, 23); + tbPassword.TabIndex = 3; + // + // lblUserRole + // + lblUserRole.AutoSize = true; + lblUserRole.Location = new Point(12, 67); + lblUserRole.Name = "lblUserRole"; + lblUserRole.Size = new Size(33, 15); + lblUserRole.TabIndex = 4; + lblUserRole.Text = "Role:"; + // + // cbUserRole + // + cbUserRole.DropDownStyle = ComboBoxStyle.DropDownList; + cbUserRole.FormattingEnabled = true; + cbUserRole.Location = new Point(84, 64); + cbUserRole.Name = "cbUserRole"; + cbUserRole.Size = new Size(121, 23); + cbUserRole.TabIndex = 5; + // + // btnSave + // + btnSave.Location = new Point(130, 93); + btnSave.Name = "btnSave"; + btnSave.Size = new Size(75, 23); + btnSave.TabIndex = 6; + btnSave.Text = "Save changes"; + btnSave.UseVisualStyleBackColor = true; + btnSave.Click += btnSave_Click; + // + // UserForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(230, 135); + Controls.Add(btnSave); + Controls.Add(cbUserRole); + Controls.Add(lblUserRole); + Controls.Add(tbPassword); + Controls.Add(lblPassword); + Controls.Add(tbUsername); + Controls.Add(lblUsername); + Name = "UserForm"; + Text = "UserForm"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label lblUsername; + private TextBox tbUsername; + private Label lblPassword; + private TextBox tbPassword; + private Label lblUserRole; + private ComboBox cbUserRole; + private Button btnSave; + } +} \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/UserForm.cs b/StudentHouseDashboard/WinForms/UserForm.cs new file mode 100644 index 0000000..07c9ed8 --- /dev/null +++ b/StudentHouseDashboard/WinForms/UserForm.cs @@ -0,0 +1,63 @@ +using StudentHouseDashboard.Managers; +using StudentHouseDashboard.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace WinForms +{ + public partial class UserForm : Form + { + User user; + public UserForm(User? user, bool readOnly) + { + InitializeComponent(); + this.user = user; + if (readOnly) + { + btnSave.Enabled = false; + tbUsername.Enabled = false; + tbPassword.Enabled = false; + cbUserRole.Enabled = false; + } + cbUserRole.Items.Clear(); + foreach (var item in Enum.GetValues(typeof(UserRole))) + { + cbUserRole.Items.Add(item); + } + if (user != null) + { + tbUsername.Text = user.Name; + tbPassword.Text = user.Password; + cbUserRole.SelectedIndex = (int)user.Role; + } + + } + + private void btnSave_Click(object sender, EventArgs e) + { + UserManager userManager = new UserManager(); + if (string.IsNullOrEmpty(tbUsername.Text) || string.IsNullOrEmpty(tbPassword.Text) || cbUserRole.SelectedIndex == -1) + { + MessageBox.Show("Please enter data in all fields"); + return; + } + + if (this.user == null) + { + userManager.CreateUser(tbUsername.Text, BCrypt.Net.BCrypt.HashPassword(tbPassword.Text), (UserRole)cbUserRole.SelectedItem); + } + else + { + userManager.UpdateUser(this.user.ID, tbUsername.Text, BCrypt.Net.BCrypt.HashPassword(tbPassword.Text), (UserRole)cbUserRole.SelectedItem); + } + this.DialogResult = DialogResult.OK; + } + } +} diff --git a/StudentHouseDashboard/WinForms/UserForm.resx b/StudentHouseDashboard/WinForms/UserForm.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/StudentHouseDashboard/WinForms/UserForm.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file