From cbc5e7f3d77b0e5174869aa963e65831bb80adcc Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Sun, 30 Apr 2023 13:45:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=80=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=B9,=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B4=D0=BE=D1=87=D0=B5=D1=82:=20=D0=BD=D0=B5=20=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=80=D0=BE?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=83=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0,=20=D0=BD=D0=BE=20id=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D1=8F=D0=B5=D1=82=D1=81=D1=8F=20=D0=BA=D0=B0=D0=BA=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B4=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Forum/Forum/Form1.Designer.cs | 39 ----- Forum/Forum/Form1.cs | 10 -- Forum/Forum/FormMain.Designer.cs | 100 +++++++++++ Forum/Forum/FormMain.cs | 30 ++++ Forum/Forum/FormMain.resx | 63 +++++++ Forum/Forum/FormRole.Designer.cs | 85 +++++++++ Forum/Forum/FormRole.cs | 88 ++++++++++ Forum/Forum/FormRole.resx | 60 +++++++ Forum/Forum/FormRoles.Designer.cs | 115 ++++++++++++ Forum/Forum/FormRoles.cs | 111 ++++++++++++ Forum/Forum/FormRoles.resx | 60 +++++++ Forum/Forum/FormUser.Designer.cs | 164 ++++++++++++++++++ Forum/Forum/FormUser.cs | 97 +++++++++++ Forum/Forum/FormUser.resx | 60 +++++++ Forum/Forum/FormUsers.Designer.cs | 115 ++++++++++++ Forum/Forum/FormUsers.cs | 75 ++++++++ Forum/Forum/FormUsers.resx | 60 +++++++ Forum/Forum/Forum.csproj | 3 + Forum/Forum/Program.cs | 34 +++- .../BindingModels/UserBindingModel.cs | 4 +- .../SearchModels/RoleSearchModel.cs | 2 +- .../SearchModels/UserSearchModel.cs | 2 +- .../ViewModels/UserViewModel.cs | 11 +- Forum/ForumDataModels/IUserModel.cs | 1 + Forum/ForumDatabase/Implements/RoleStorage.cs | 58 ++++++- Forum/ForumDatabase/Implements/UserStorage.cs | 58 ++++++- Forum/ForumDatabase/Models/User.cs | 9 +- 27 files changed, 1444 insertions(+), 70 deletions(-) delete mode 100644 Forum/Forum/Form1.Designer.cs delete mode 100644 Forum/Forum/Form1.cs create mode 100644 Forum/Forum/FormMain.Designer.cs create mode 100644 Forum/Forum/FormMain.cs create mode 100644 Forum/Forum/FormMain.resx create mode 100644 Forum/Forum/FormRole.Designer.cs create mode 100644 Forum/Forum/FormRole.cs create mode 100644 Forum/Forum/FormRole.resx create mode 100644 Forum/Forum/FormRoles.Designer.cs create mode 100644 Forum/Forum/FormRoles.cs create mode 100644 Forum/Forum/FormRoles.resx create mode 100644 Forum/Forum/FormUser.Designer.cs create mode 100644 Forum/Forum/FormUser.cs create mode 100644 Forum/Forum/FormUser.resx create mode 100644 Forum/Forum/FormUsers.Designer.cs create mode 100644 Forum/Forum/FormUsers.cs create mode 100644 Forum/Forum/FormUsers.resx diff --git a/Forum/Forum/Form1.Designer.cs b/Forum/Forum/Form1.Designer.cs deleted file mode 100644 index 580ea59..0000000 --- a/Forum/Forum/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace Forum -{ - partial class Form1 - { - /// - /// 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() - { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; - } - - #endregion - } -} \ No newline at end of file diff --git a/Forum/Forum/Form1.cs b/Forum/Forum/Form1.cs deleted file mode 100644 index 58aee19..0000000 --- a/Forum/Forum/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Forum -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/Forum/Forum/FormMain.Designer.cs b/Forum/Forum/FormMain.Designer.cs new file mode 100644 index 0000000..87d0cda --- /dev/null +++ b/Forum/Forum/FormMain.Designer.cs @@ -0,0 +1,100 @@ +namespace Forum +{ + partial class FormMain + { + /// + /// 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() + { + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.ролиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.пользователиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + this.menuStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // dataGridView1 + // + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView1.Location = new System.Drawing.Point(12, 43); + this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.RowHeadersWidth = 51; + this.dataGridView1.RowTemplate.Height = 29; + this.dataGridView1.Size = new System.Drawing.Size(776, 395); + this.dataGridView1.TabIndex = 0; + // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ролиToolStripMenuItem, + this.пользователиToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(800, 28); + this.menuStrip1.TabIndex = 1; + this.menuStrip1.Text = "menuStrip1"; + // + // ролиToolStripMenuItem + // + this.ролиToolStripMenuItem.Name = "ролиToolStripMenuItem"; + this.ролиToolStripMenuItem.Size = new System.Drawing.Size(57, 24); + this.ролиToolStripMenuItem.Text = "Роли"; + this.ролиToolStripMenuItem.Click += new System.EventHandler(this.ролиToolStripMenuItem_Click); + // + // пользователиToolStripMenuItem + // + this.пользователиToolStripMenuItem.Name = "пользователиToolStripMenuItem"; + this.пользователиToolStripMenuItem.Size = new System.Drawing.Size(122, 24); + this.пользователиToolStripMenuItem.Text = "Пользователи"; + this.пользователиToolStripMenuItem.Click += new System.EventHandler(this.пользователиToolStripMenuItem_Click); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.dataGridView1); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Name = "FormMain"; + this.Text = "Форум"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DataGridView dataGridView1; + private MenuStrip menuStrip1; + private ToolStripMenuItem ролиToolStripMenuItem; + private ToolStripMenuItem пользователиToolStripMenuItem; + } +} \ No newline at end of file diff --git a/Forum/Forum/FormMain.cs b/Forum/Forum/FormMain.cs new file mode 100644 index 0000000..f25e000 --- /dev/null +++ b/Forum/Forum/FormMain.cs @@ -0,0 +1,30 @@ +namespace Forum +{ + public partial class FormMain : Form + { + public FormMain() + { + InitializeComponent(); + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRoles)); + + if (service is FormRoles form) + { + form.ShowDialog(); + } + } + + private void ToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormUsers)); + + if (service is FormUsers form) + { + form.ShowDialog(); + } + } + } +} \ No newline at end of file diff --git a/Forum/Forum/FormMain.resx b/Forum/Forum/FormMain.resx new file mode 100644 index 0000000..938108a --- /dev/null +++ b/Forum/Forum/FormMain.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + \ No newline at end of file diff --git a/Forum/Forum/FormRole.Designer.cs b/Forum/Forum/FormRole.Designer.cs new file mode 100644 index 0000000..b3df60b --- /dev/null +++ b/Forum/Forum/FormRole.Designer.cs @@ -0,0 +1,85 @@ +namespace Forum +{ + partial class FormRole + { + /// + /// 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() + { + this.textBoxRoleName = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // textBoxRoleName + // + this.textBoxRoleName.Location = new System.Drawing.Point(12, 12); + this.textBoxRoleName.Name = "textBoxRoleName"; + this.textBoxRoleName.Size = new System.Drawing.Size(268, 27); + this.textBoxRoleName.TabIndex = 0; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(12, 45); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.TabIndex = 1; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(186, 45); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 2; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormRole + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(292, 81); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxRoleName); + this.Name = "FormRole"; + this.Text = "Роль"; + this.Load += new System.EventHandler(this.FormRole_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxRoleName; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Forum/Forum/FormRole.cs b/Forum/Forum/FormRole.cs new file mode 100644 index 0000000..822522e --- /dev/null +++ b/Forum/Forum/FormRole.cs @@ -0,0 +1,88 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.SearchModels; +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 Forum +{ + public partial class FormRole : Form + { + private readonly IRoleLogic _roleLogic; + + private int? _id; + public int Id { set { _id = value; } } + public FormRole(IRoleLogic roleLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxRoleName.Text)) + { + MessageBox.Show("Введите название роли", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + try + { + var model = new RoleBindingModel + { + Id = _id ?? 0, + Name = textBoxRoleName.Text + }; + + var operationResult = _id.HasValue ? _roleLogic.Update(model) : _roleLogic.Create(model); + + if (!operationResult) + { + throw new Exception("Ошибка при сохранеии."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormRole_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _roleLogic.ReadElement(new RoleSearchModel { Id = _id.Value }); + + if (view != null) + { + textBoxRoleName.Text = view.Name; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } +} diff --git a/Forum/Forum/FormRole.resx b/Forum/Forum/FormRole.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Forum/Forum/FormRole.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/Forum/Forum/FormRoles.Designer.cs b/Forum/Forum/FormRoles.Designer.cs new file mode 100644 index 0000000..a0fbea4 --- /dev/null +++ b/Forum/Forum/FormRoles.Designer.cs @@ -0,0 +1,115 @@ +namespace Forum +{ + partial class FormRoles + { + /// + /// 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() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(371, 268); + this.dataGridView.TabIndex = 0; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(389, 12); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 1; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(389, 47); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(94, 29); + this.buttonDelete.TabIndex = 2; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(389, 82); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(94, 29); + this.buttonChange.TabIndex = 3; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(389, 117); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormRoles + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(495, 292); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Name = "FormRoles"; + this.Text = "Роли"; + this.Load += new System.EventHandler(this.FormRoles_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonDelete; + private Button buttonChange; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/Forum/Forum/FormRoles.cs b/Forum/Forum/FormRoles.cs new file mode 100644 index 0000000..7c969c1 --- /dev/null +++ b/Forum/Forum/FormRoles.cs @@ -0,0 +1,111 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +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 Forum +{ + public partial class FormRoles : Form + { + private readonly IRoleLogic _roleLogic; + public FormRoles(IRoleLogic roleLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + } + + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRole)); + + if (service is FormRole form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void LoadData() + { + try + { + var list = _roleLogic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception) + { } + } + + private void FormRoles_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + try + { + if (!_roleLogic.Delete(new RoleBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRole)); + + if (service is FormRole form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Forum/Forum/FormRoles.resx b/Forum/Forum/FormRoles.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Forum/Forum/FormRoles.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/Forum/Forum/FormUser.Designer.cs b/Forum/Forum/FormUser.Designer.cs new file mode 100644 index 0000000..44bc943 --- /dev/null +++ b/Forum/Forum/FormUser.Designer.cs @@ -0,0 +1,164 @@ +namespace Forum +{ + partial class FormUser + { + /// + /// 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() + { + this.textBoxUsername = new System.Windows.Forms.TextBox(); + this.textBoxLogin = new System.Windows.Forms.TextBox(); + this.textBoxPassword = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.comboBoxSelectRole = new System.Windows.Forms.ComboBox(); + this.label4 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // textBoxUsername + // + this.textBoxUsername.Location = new System.Drawing.Point(92, 12); + this.textBoxUsername.Name = "textBoxUsername"; + this.textBoxUsername.Size = new System.Drawing.Size(151, 27); + this.textBoxUsername.TabIndex = 0; + // + // textBoxLogin + // + this.textBoxLogin.Location = new System.Drawing.Point(92, 45); + this.textBoxLogin.Name = "textBoxLogin"; + this.textBoxLogin.Size = new System.Drawing.Size(151, 27); + this.textBoxLogin.TabIndex = 1; + // + // textBoxPassword + // + this.textBoxPassword.Location = new System.Drawing.Point(92, 78); + this.textBoxPassword.Name = "textBoxPassword"; + this.textBoxPassword.Size = new System.Drawing.Size(151, 27); + this.textBoxPassword.TabIndex = 2; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(11, 19); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(75, 20); + this.label1.TabIndex = 3; + this.label1.Text = "Username"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 52); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(46, 20); + this.label2.TabIndex = 4; + this.label2.Text = "Login"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(11, 85); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(70, 20); + this.label3.TabIndex = 5; + this.label3.Text = "Password"; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(12, 160); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(149, 160); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // comboBoxSelectRole + // + this.comboBoxSelectRole.FormattingEnabled = true; + this.comboBoxSelectRole.Location = new System.Drawing.Point(92, 117); + this.comboBoxSelectRole.Name = "comboBoxSelectRole"; + this.comboBoxSelectRole.Size = new System.Drawing.Size(151, 28); + this.comboBoxSelectRole.TabIndex = 8; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(11, 120); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(39, 20); + this.label4.TabIndex = 9; + this.label4.Text = "Role"; + // + // FormUser + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(262, 201); + this.Controls.Add(this.label4); + this.Controls.Add(this.comboBoxSelectRole); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBoxPassword); + this.Controls.Add(this.textBoxLogin); + this.Controls.Add(this.textBoxUsername); + this.Name = "FormUser"; + this.Text = "Пользователь"; + this.Load += new System.EventHandler(this.FormUser_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxUsername; + private TextBox textBoxLogin; + private TextBox textBoxPassword; + private Label label1; + private Label label2; + private Label label3; + private Button buttonSave; + private Button buttonCancel; + private ComboBox comboBoxSelectRole; + private Label label4; + } +} \ No newline at end of file diff --git a/Forum/Forum/FormUser.cs b/Forum/Forum/FormUser.cs new file mode 100644 index 0000000..2b061b6 --- /dev/null +++ b/Forum/Forum/FormUser.cs @@ -0,0 +1,97 @@ +using ForumContracts.BindingModels; +using ForumContracts.BusinessLogicContracts; +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 Forum +{ + public partial class FormUser : Form + { + private readonly IUserLogic _userLogic; + private readonly IRoleLogic _roleLogic; + public FormUser(IRoleLogic roleLogic, IUserLogic userLogic) + { + InitializeComponent(); + _roleLogic = roleLogic; + _userLogic = userLogic; + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxUsername.Text)) + { + MessageBox.Show("Заполните поле Username", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxLogin.Text)) + { + MessageBox.Show("Заполните поле Login", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPassword.Text)) + { + MessageBox.Show("Заполните поле Password", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxSelectRole.SelectedValue == null) + { + MessageBox.Show("Выберите Role", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var operationResult = _userLogic.Create(new UserBindingModel + { + Username = textBoxUsername.Text, + Email = textBoxLogin.Text, + Password = textBoxPassword.Text, + RegistrationDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + RoleId = Convert.ToInt32(comboBoxSelectRole.SelectedValue), + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании пользователя."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormUser_Load(object sender, EventArgs e) + { + try + { + var role = _roleLogic.ReadList(null); + if (role != null) + { + comboBoxSelectRole.DisplayMember = "Name"; + comboBoxSelectRole.ValueMember = "Id"; + comboBoxSelectRole.DataSource = role; + comboBoxSelectRole.SelectedItem = null; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/Forum/Forum/FormUser.resx b/Forum/Forum/FormUser.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Forum/Forum/FormUser.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/Forum/Forum/FormUsers.Designer.cs b/Forum/Forum/FormUsers.Designer.cs new file mode 100644 index 0000000..c4fcc3b --- /dev/null +++ b/Forum/Forum/FormUsers.Designer.cs @@ -0,0 +1,115 @@ +namespace Forum +{ + partial class FormUsers + { + /// + /// 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() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(676, 291); + this.dataGridView.TabIndex = 0; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(694, 22); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 1; + this.buttonCreate.Text = "Добавить"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(694, 57); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(94, 29); + this.buttonChange.TabIndex = 2; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(694, 92); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(94, 29); + this.buttonDelete.TabIndex = 3; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(694, 127); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormUsers + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 316); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.dataGridView); + this.Name = "FormUsers"; + this.Text = "Пользователи"; + this.Load += new System.EventHandler(this.FormUsers_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonChange; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/Forum/Forum/FormUsers.cs b/Forum/Forum/FormUsers.cs new file mode 100644 index 0000000..5f6d6cc --- /dev/null +++ b/Forum/Forum/FormUsers.cs @@ -0,0 +1,75 @@ +using ForumContracts.BusinessLogicContracts; +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 Forum +{ + public partial class FormUsers : Form + { + private readonly IUserLogic _userLogic; + public FormUsers(IUserLogic userLogic) + { + InitializeComponent(); + _userLogic = userLogic; + } + + private void FormUsers_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _userLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["RoleId"].Visible = false; + dataGridView.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Password"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["RoleName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormUser)); + if (service is FormUser form) + { + form.ShowDialog(); + LoadData(); + } + } + + private void buttonChange_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Forum/Forum/FormUsers.resx b/Forum/Forum/FormUsers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Forum/Forum/FormUsers.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/Forum/Forum/Forum.csproj b/Forum/Forum/Forum.csproj index 998eaef..e1d3cf6 100644 --- a/Forum/Forum/Forum.csproj +++ b/Forum/Forum/Forum.csproj @@ -16,7 +16,10 @@ + + + \ No newline at end of file diff --git a/Forum/Forum/Program.cs b/Forum/Forum/Program.cs index 099e27f..f8b9029 100644 --- a/Forum/Forum/Program.cs +++ b/Forum/Forum/Program.cs @@ -1,7 +1,16 @@ +using ForumBusinessLogic; +using ForumContracts.BusinessLogicContracts; +using ForumContracts.StoragesContracts; +using ForumDatabase.Implements; +using Microsoft.Extensions.DependencyInjection; +using System; + namespace Forum { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +20,30 @@ namespace Forum // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Forum/ForumContracts/BindingModels/UserBindingModel.cs b/Forum/ForumContracts/BindingModels/UserBindingModel.cs index aaa2b0f..c62881c 100644 --- a/Forum/ForumContracts/BindingModels/UserBindingModel.cs +++ b/Forum/ForumContracts/BindingModels/UserBindingModel.cs @@ -15,8 +15,10 @@ namespace ForumContracts.BindingModels public string Password { get; set; } = string.Empty; - public DateTime RegistrationDate { get; set; } + public DateTime RegistrationDate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public int Id { get; set; } + + public int RoleId { get; set; } } } diff --git a/Forum/ForumContracts/SearchModels/RoleSearchModel.cs b/Forum/ForumContracts/SearchModels/RoleSearchModel.cs index f8d1f0f..4a2f05a 100644 --- a/Forum/ForumContracts/SearchModels/RoleSearchModel.cs +++ b/Forum/ForumContracts/SearchModels/RoleSearchModel.cs @@ -8,7 +8,7 @@ namespace ForumContracts.SearchModels { public class RoleSearchModel { - public int Id { get; set; } + public int? Id { get; set; } public string? Name { get; set; } } } diff --git a/Forum/ForumContracts/SearchModels/UserSearchModel.cs b/Forum/ForumContracts/SearchModels/UserSearchModel.cs index de72083..00f1e74 100644 --- a/Forum/ForumContracts/SearchModels/UserSearchModel.cs +++ b/Forum/ForumContracts/SearchModels/UserSearchModel.cs @@ -8,7 +8,7 @@ namespace ForumContracts.SearchModels { public class UserSearchModel { - public int Id { get; set; } + public int? Id { get; set; } public string? Username { get; set; } public string? Email { get; set; } diff --git a/Forum/ForumContracts/ViewModels/UserViewModel.cs b/Forum/ForumContracts/ViewModels/UserViewModel.cs index 7b9901e..28ff39a 100644 --- a/Forum/ForumContracts/ViewModels/UserViewModel.cs +++ b/Forum/ForumContracts/ViewModels/UserViewModel.cs @@ -10,18 +10,23 @@ namespace ForumContracts.ViewModels { public class UserViewModel : IUserModel { - [DisplayName("Имя пользователя")] + [DisplayName("Имя")] public string Username { get; set; } = string.Empty; - [DisplayName("Почта пользователя")] + [DisplayName("Почта")] public string Email { get; set; } = string.Empty; - [DisplayName("Пароль пользователя")] + [DisplayName("Пароль")] public string Password { get; set; } = string.Empty; [DisplayName("Дата регистрации")] public DateTime RegistrationDate { get; set; } public int Id { get; set; } + + public int RoleId { get; set; } + + [DisplayName("Роль")] + public string RoleName { get; set; } = string.Empty; } } diff --git a/Forum/ForumDataModels/IUserModel.cs b/Forum/ForumDataModels/IUserModel.cs index f79625b..063e21b 100644 --- a/Forum/ForumDataModels/IUserModel.cs +++ b/Forum/ForumDataModels/IUserModel.cs @@ -12,5 +12,6 @@ namespace ForumDataModels string Email { get; } string Password { get; } DateTime RegistrationDate { get; } + int RoleId { get; } } } diff --git a/Forum/ForumDatabase/Implements/RoleStorage.cs b/Forum/ForumDatabase/Implements/RoleStorage.cs index 8ad0d90..7c3b8f1 100644 --- a/Forum/ForumDatabase/Implements/RoleStorage.cs +++ b/Forum/ForumDatabase/Implements/RoleStorage.cs @@ -2,6 +2,7 @@ using ForumContracts.SearchModels; using ForumContracts.StoragesContracts; using ForumContracts.ViewModels; +using ForumDatabase.Models; using System; using System.Collections.Generic; using System.Linq; @@ -14,32 +15,77 @@ namespace ForumDatabase.Implements { public RoleViewModel? Delete(RoleBindingModel model) { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + var element = context.Roles.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Roles.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public RoleViewModel? GetElement(RoleSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ForumDatabase(); + return context.Roles + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public List GetFilteredList(RoleSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ForumDatabase(); + return context.Roles + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + return context.Roles + .Select(x => x.GetViewModel) + .ToList(); } public RoleViewModel? Insert(RoleBindingModel model) { - throw new NotImplementedException(); + var newRole = Role.Create(model); + if (newRole == null) + { + return null; + } + using var context = new ForumDatabase(); + context.Roles.Add(newRole); + context.SaveChanges(); + return newRole.GetViewModel; } public RoleViewModel? Update(RoleBindingModel model) { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + var component = context.Roles.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; } } } diff --git a/Forum/ForumDatabase/Implements/UserStorage.cs b/Forum/ForumDatabase/Implements/UserStorage.cs index 693280e..6d98bb9 100644 --- a/Forum/ForumDatabase/Implements/UserStorage.cs +++ b/Forum/ForumDatabase/Implements/UserStorage.cs @@ -2,6 +2,7 @@ using ForumContracts.SearchModels; using ForumContracts.StoragesContracts; using ForumContracts.ViewModels; +using ForumDatabase.Models; using System; using System.Collections.Generic; using System.Linq; @@ -14,32 +15,77 @@ namespace ForumDatabase.Implements { public UserViewModel? Delete(UserBindingModel model) { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public UserViewModel? GetElement(UserSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Username) && !model.Id.HasValue && string.IsNullOrEmpty(model.Email)) + { + return null; + } + using var context = new ForumDatabase(); + return context.Users + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Username) && x.Username == + model.Username) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public List GetFilteredList(UserSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new ForumDatabase(); + return context.Users + .Where(x => x.Username.Contains(model.Username)) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + return context.Users + .Select(x => x.GetViewModel) + .ToList(); } public UserViewModel? Insert(UserBindingModel model) { - throw new NotImplementedException(); + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new ForumDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; } public UserViewModel? Update(UserBindingModel model) { - throw new NotImplementedException(); + using var context = new ForumDatabase(); + var component = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; } } } diff --git a/Forum/ForumDatabase/Models/User.cs b/Forum/ForumDatabase/Models/User.cs index cc79867..bca7d26 100644 --- a/Forum/ForumDatabase/Models/User.cs +++ b/Forum/ForumDatabase/Models/User.cs @@ -23,9 +23,9 @@ namespace ForumDatabase.Models [Required] public string Password { get; set; } = string.Empty; [Required] - public DateTime RegistrationDate { get; set; } = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); + public DateTime RegistrationDate { get; set; } - public int Id { get; private set; } + public int Id { get; set; } [Required] public int RoleId { get; set; } @@ -39,6 +39,8 @@ namespace ForumDatabase.Models [ForeignKey("MessageId")] List Messages { get; set; } = new(); + public virtual Role Role { get; set; } + public static User? Create(UserBindingModel model) { if (model == null) @@ -52,6 +54,7 @@ namespace ForumDatabase.Models Email = model.Email, Password = model.Password, RegistrationDate = model.RegistrationDate, + RoleId = model.RoleId, }; } public void Update(UserBindingModel model) @@ -72,6 +75,8 @@ namespace ForumDatabase.Models Email = Email, Password = Password, RegistrationDate = RegistrationDate, + RoleId = RoleId, + RoleName = Role == null ? string.Empty : Role.Name, }; } }