PIbd-21_Rodionov_I.A._SUBD/ElectronicDiary/ElectronicDiaryView/FormUser.cs

122 lines
4.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ElectronicDiaryAbstractions.Models;
using ElectronicDiaryAbstractions.WorkAbstractions;
namespace ElectronicDiaryView
{
public partial class FormUser : Form
{
private readonly IUserWork _logic;
public FormUser(IUserWork logic)
{
InitializeComponent();
_logic = logic;
}
private void FormUser_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
var users = _logic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Login", "Логин");
dataGridView.Columns.Add("Email", "E-mail");
dataGridView.Columns.Add("Password", "Пароль");
dataGridView.Columns.Add("PhoneNumber", "Номер телефона");
dataGridView.Columns.Add("AccessLevel", "Уровень доступа");
}
dataGridView.Columns["Login"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Password"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["PhoneNumber"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["AccessLevel"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
foreach (var user in users)
{
dataGridView.Rows.Add(user.Id, user.Login, user.Email, user.Password, user.PhoneNumber, user.AccessLevel);
}
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
User newUser = new()
{
Login = textBoxLogin.Text,
Email = textBoxEmail.Text,
Password = textBoxPassword.Text,
PhoneNumber = textBoxPhone.Text,
AccessLevel = comboBoxAccessLevel.SelectedItem.ToString() ?? "Ученик",
};
_logic.Create(newUser);
LoadData();
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int userId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
User updatedUser = new()
{
Id = userId,
Email = textBoxEmail.Text,
Password = textBoxPassword.Text,
PhoneNumber = textBoxPhone.Text,
AccessLevel = comboBoxAccessLevel.SelectedItem.ToString() ?? "Ученик",
};
_logic.Update(updatedUser);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите пользователя, информацию о котором необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int userId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_logic.Delete(userId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите пользователя, которого необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxLogin.Text = row.Cells["Login"].Value.ToString();
textBoxEmail.Text = row.Cells["Email"].Value.ToString();
textBoxPassword.Text = row.Cells["Password"].Value.ToString();
textBoxPhone.Text = row.Cells["PhoneNumber"].Value.ToString();
comboBoxAccessLevel.SelectedValue = row.Cells["AccessLevel"].Value.ToString();
}
}
}
}