этап с формами сделан

This commit is contained in:
ArtemEmelyanov 2023-05-01 19:05:49 +04:00
parent ac85a743c1
commit 47aa318592
23 changed files with 754 additions and 75 deletions

View File

@ -35,37 +35,43 @@
//
// textBoxTopic
//
this.textBoxTopic.Location = new System.Drawing.Point(20, 12);
this.textBoxTopic.Location = new System.Drawing.Point(18, 9);
this.textBoxTopic.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.textBoxTopic.Name = "textBoxTopic";
this.textBoxTopic.Size = new System.Drawing.Size(218, 27);
this.textBoxTopic.Size = new System.Drawing.Size(191, 23);
this.textBoxTopic.TabIndex = 0;
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(20, 45);
this.buttonSave.Location = new System.Drawing.Point(18, 34);
this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(94, 29);
this.buttonSave.Size = new System.Drawing.Size(82, 22);
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(144, 45);
this.buttonCancel.Location = new System.Drawing.Point(126, 34);
this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(94, 29);
this.buttonCancel.Size = new System.Drawing.Size(82, 22);
this.buttonCancel.TabIndex = 2;
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// FormAddTopic
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(251, 84);
this.ClientSize = new System.Drawing.Size(220, 63);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.textBoxTopic);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "FormAddTopic";
this.Text = "Тема";
this.ResumeLayout(false);

View File

@ -1,4 +1,6 @@
using System;
using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +14,54 @@ namespace Forum
{
public partial class FormAddTopic : Form
{
public FormAddTopic()
private readonly ITopicLogic _topicLogic;
private readonly ICategoryLogic _categoryLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormAddTopic(ITopicLogic topicLogic, ICategoryLogic categoryLogic)
{
InitializeComponent();
_topicLogic = topicLogic;
_categoryLogic = categoryLogic;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxTopic.Text))
{
MessageBox.Show("Название темы не может быть пустой", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
var model = new TopicBindingModel
{
Name = textBoxTopic.Text,
CategoryId = _id ?? 0,
};
var operationResult = _topicLogic.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();
}
}
}

View File

@ -28,37 +28,41 @@
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.dataGridView = new System.Windows.Forms.DataGridView();
this.textBoxCategory = new System.Windows.Forms.TextBox();
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.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
// dataGridView
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 54);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 29;
this.dataGridView1.Size = new System.Drawing.Size(408, 188);
this.dataGridView1.TabIndex = 0;
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Location = new System.Drawing.Point(10, 40);
this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowHeadersWidth = 51;
this.dataGridView.RowTemplate.Height = 29;
this.dataGridView.Size = new System.Drawing.Size(357, 141);
this.dataGridView.TabIndex = 0;
//
// textBoxCategory
//
this.textBoxCategory.Location = new System.Drawing.Point(12, 12);
this.textBoxCategory.Location = new System.Drawing.Point(10, 9);
this.textBoxCategory.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.textBoxCategory.Name = "textBoxCategory";
this.textBoxCategory.Size = new System.Drawing.Size(125, 27);
this.textBoxCategory.ReadOnly = true;
this.textBoxCategory.Size = new System.Drawing.Size(110, 23);
this.textBoxCategory.TabIndex = 1;
//
// buttonCreate
//
this.buttonCreate.Location = new System.Drawing.Point(435, 54);
this.buttonCreate.Location = new System.Drawing.Point(381, 40);
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(94, 29);
this.buttonCreate.Size = new System.Drawing.Size(82, 22);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
@ -66,9 +70,10 @@
//
// buttonChange
//
this.buttonChange.Location = new System.Drawing.Point(435, 89);
this.buttonChange.Location = new System.Drawing.Point(381, 67);
this.buttonChange.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonChange.Name = "buttonChange";
this.buttonChange.Size = new System.Drawing.Size(94, 29);
this.buttonChange.Size = new System.Drawing.Size(82, 22);
this.buttonChange.TabIndex = 3;
this.buttonChange.Text = "Изменить";
this.buttonChange.UseVisualStyleBackColor = true;
@ -76,9 +81,10 @@
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(435, 124);
this.buttonDelete.Location = new System.Drawing.Point(381, 93);
this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(94, 29);
this.buttonDelete.Size = new System.Drawing.Size(82, 22);
this.buttonDelete.TabIndex = 4;
this.buttonDelete.Text = "Удалить";
this.buttonDelete.UseVisualStyleBackColor = true;
@ -86,29 +92,31 @@
//
// buttonUpdate
//
this.buttonUpdate.Location = new System.Drawing.Point(435, 159);
this.buttonUpdate.Location = new System.Drawing.Point(381, 119);
this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUpdate.Name = "buttonUpdate";
this.buttonUpdate.Size = new System.Drawing.Size(94, 29);
this.buttonUpdate.Size = new System.Drawing.Size(82, 22);
this.buttonUpdate.TabIndex = 5;
this.buttonUpdate.Text = "Обновить";
this.buttonUpdate.UseVisualStyleBackColor = true;
this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);
//
// FormAddTopic
// FormAddTopics
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(536, 253);
this.ClientSize = new System.Drawing.Size(469, 190);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.buttonDelete);
this.Controls.Add(this.buttonChange);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.textBoxCategory);
this.Controls.Add(this.dataGridView1);
this.Name = "FormAddTopic";
this.Controls.Add(this.dataGridView);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "FormAddTopics";
this.Text = "Добавление тем";
this.Load += new System.EventHandler(this.FormAddTopic_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -116,7 +124,7 @@
#endregion
private DataGridView dataGridView1;
private DataGridView dataGridView;
private TextBox textBoxCategory;
private Button buttonCreate;
private Button buttonChange;

View File

@ -1,4 +1,5 @@
using ForumContracts.BusinessLogicContracts;
using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using ForumContracts.SearchModels;
using System;
using System.Collections.Generic;
@ -27,6 +28,7 @@ namespace Forum
private void FormAddTopic_Load(object sender, EventArgs e)
{
LoadData();
try
{
var view = _categoryLogic.ReadElement(new CategorySearchModel { Id = _id.Value });
@ -48,6 +50,7 @@ namespace Forum
if (service is FormAddTopic form)
{
form.Id = _id ?? 0;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
@ -57,7 +60,20 @@ namespace Forum
private void LoadData()
{
throw new NotImplementedException();
try
{
var list = _topicLogic.ReadList(new TopicSearchModel { CategoryId = _id });
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["CategoryId"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception)
{ }
}
private void buttonChange_Click(object sender, EventArgs e)
@ -67,12 +83,35 @@ namespace Forum
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 (!_topicLogic.Delete(new TopicBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
LoadData();
}
}
}

156
Forum/Forum/FormCreateMessage.Designer.cs generated Normal file
View File

@ -0,0 +1,156 @@
namespace Forum
{
partial class FormCreateMessage
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBoxCategory = new System.Windows.Forms.ComboBox();
this.comboBoxTopic = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.comboBoxUser = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.textBoxMessage = new System.Windows.Forms.TextBox();
this.buttonSave = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBoxCategory
//
this.comboBoxCategory.FormattingEnabled = true;
this.comboBoxCategory.Location = new System.Drawing.Point(141, 6);
this.comboBoxCategory.Name = "comboBoxCategory";
this.comboBoxCategory.Size = new System.Drawing.Size(147, 23);
this.comboBoxCategory.TabIndex = 0;
this.comboBoxCategory.SelectedIndexChanged += new System.EventHandler(this.comboBoxCategory_SelectedIndexChanged);
//
// comboBoxTopic
//
this.comboBoxTopic.FormattingEnabled = true;
this.comboBoxTopic.Location = new System.Drawing.Point(141, 35);
this.comboBoxTopic.Name = "comboBoxTopic";
this.comboBoxTopic.Size = new System.Drawing.Size(147, 23);
this.comboBoxTopic.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(123, 15);
this.label1.TabIndex = 2;
this.label1.Text = "Выберите категорию";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(90, 15);
this.label2.TabIndex = 3;
this.label2.Text = "Выберите тему";
//
// comboBoxUser
//
this.comboBoxUser.FormattingEnabled = true;
this.comboBoxUser.Location = new System.Drawing.Point(141, 64);
this.comboBoxUser.Name = "comboBoxUser";
this.comboBoxUser.Size = new System.Drawing.Size(147, 23);
this.comboBoxUser.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 67);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(84, 15);
this.label3.TabIndex = 5;
this.label3.Text = "Пользователь";
//
// textBoxMessage
//
this.textBoxMessage.Location = new System.Drawing.Point(12, 93);
this.textBoxMessage.Multiline = true;
this.textBoxMessage.Name = "textBoxMessage";
this.textBoxMessage.Size = new System.Drawing.Size(276, 70);
this.textBoxMessage.TabIndex = 6;
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(132, 169);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(75, 23);
this.buttonSave.TabIndex = 7;
this.buttonSave.Text = "Сохранить";
this.buttonSave.UseVisualStyleBackColor = true;
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// buttonCancel
//
this.buttonCancel.Location = new System.Drawing.Point(213, 169);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 8;
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// FormCreateMessage
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(296, 198);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.textBoxMessage);
this.Controls.Add(this.label3);
this.Controls.Add(this.comboBoxUser);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBoxTopic);
this.Controls.Add(this.comboBoxCategory);
this.Name = "FormCreateMessage";
this.Text = "Написать сообщение";
this.Load += new System.EventHandler(this.FormCreateMessage_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private ComboBox comboBoxCategory;
private ComboBox comboBoxTopic;
private Label label1;
private Label label2;
private ComboBox comboBoxUser;
private Label label3;
private TextBox textBoxMessage;
private Button buttonSave;
private Button buttonCancel;
}
}

View File

@ -0,0 +1,139 @@
using ForumBusinessLogic;
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 FormCreateMessage : Form
{
private readonly IMessageLogic _messageLogic;
private readonly ICategoryLogic _categoryLogic;
private readonly ITopicLogic _topicLogic;
private readonly IUserLogic _userLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormCreateMessage(ICategoryLogic categoryLogic, ITopicLogic topicLogic, IUserLogic userLogic, IMessageLogic messageLogic)
{
InitializeComponent();
_categoryLogic = categoryLogic;
_topicLogic = topicLogic;
_userLogic = userLogic;
_messageLogic = messageLogic;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (comboBoxCategory.SelectedValue == null)
{
MessageBox.Show("Выберите категорию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxTopic.SelectedValue == null)
{
MessageBox.Show("Выберите тему", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxUser.SelectedValue == null)
{
MessageBox.Show("Выберите пользователя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxMessage.Text))
{
MessageBox.Show("Напишите сообщение", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new MessageBindingModel
{
Id = _id ?? 0,
Date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Text = textBoxMessage.Text,
TopicId = Convert.ToInt32(comboBoxTopic.SelectedValue),
UserId = Convert.ToInt32(comboBoxUser.SelectedValue),
};
var operationResult = _id.HasValue ? _messageLogic.Update(model) : _messageLogic.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)
{
}
private void FormCreateMessage_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var user = _userLogic.ReadList(null);
if (user != null)
{
comboBoxUser.DisplayMember = "Username";
comboBoxUser.ValueMember = "Id";
comboBoxUser.DataSource = user;
comboBoxUser.SelectedItem = null;
}
var category = _categoryLogic.ReadList(null);
if (category != null)
{
comboBoxCategory.DisplayMember = "Name";
comboBoxCategory.ValueMember = "Id";
comboBoxCategory.DataSource = category;
comboBoxCategory.SelectedItem = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
var topics = _topicLogic.ReadList(new TopicSearchModel { CategoryId = Convert.ToInt32(comboBoxCategory.SelectedValue), });
if (topics != null)
{
comboBoxTopic.DisplayMember = "Name";
comboBoxTopic.ValueMember = "Id";
comboBoxTopic.DataSource = topics;
comboBoxTopic.SelectedItem = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -28,24 +28,28 @@
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.dataGridView = 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();
this.категорииToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.buttonCreate = 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.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// dataGridView1
// dataGridView
//
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;
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Location = new System.Drawing.Point(10, 32);
this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowHeadersWidth = 51;
this.dataGridView.RowTemplate.Height = 29;
this.dataGridView.Size = new System.Drawing.Size(554, 296);
this.dataGridView.TabIndex = 0;
//
// menuStrip1
//
@ -56,42 +60,78 @@
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.Padding = new System.Windows.Forms.Padding(5, 2, 0, 2);
this.menuStrip1.Size = new System.Drawing.Size(653, 24);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// ролиToolStripMenuItem
//
this.ролиToolStripMenuItem.Name = "ролиToolStripMenuItem";
this.ролиToolStripMenuItem.Size = new System.Drawing.Size(57, 24);
this.ролиToolStripMenuItem.Size = new System.Drawing.Size(47, 20);
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.Size = new System.Drawing.Size(97, 20);
this.пользователиToolStripMenuItem.Text = "Пользователи";
this.пользователиToolStripMenuItem.Click += new System.EventHandler(this.пользователиToolStripMenuItem_Click);
//
// категорииToolStripMenuItem
//
this.категорииToolStripMenuItem.Name = атегорииToolStripMenuItem";
this.категорииToolStripMenuItem.Size = new System.Drawing.Size(96, 24);
this.категорииToolStripMenuItem.Size = new System.Drawing.Size(76, 20);
this.категорииToolStripMenuItem.Text = "Категории";
this.категорииToolStripMenuItem.Click += new System.EventHandler(this.категорииToolStripMenuItem_Click);
//
// buttonCreate
//
this.buttonCreate.Location = new System.Drawing.Point(570, 32);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(570, 61);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(75, 23);
this.buttonDelete.TabIndex = 4;
this.buttonDelete.Text = "Удалить";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
//
// buttonUpdate
//
this.buttonUpdate.Location = new System.Drawing.Point(570, 90);
this.buttonUpdate.Name = "buttonUpdate";
this.buttonUpdate.Size = new System.Drawing.Size(75, 23);
this.buttonUpdate.TabIndex = 5;
this.buttonUpdate.Text = "Обновить";
this.buttonUpdate.UseVisualStyleBackColor = true;
this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.dataGridView1);
this.ClientSize = new System.Drawing.Size(653, 338);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.buttonDelete);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.dataGridView);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "FormMain";
this.Text = "Форум";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.Load += new System.EventHandler(this.FormMain_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
@ -101,10 +141,13 @@
#endregion
private DataGridView dataGridView1;
private DataGridView dataGridView;
private MenuStrip menuStrip1;
private ToolStripMenuItem ролиToolStripMenuItem;
private ToolStripMenuItem пользователиToolStripMenuItem;
private ToolStripMenuItem категорииToolStripMenuItem;
private Button buttonCreate;
private Button buttonDelete;
private Button buttonUpdate;
}
}

View File

@ -1,10 +1,17 @@
using ForumBusinessLogic;
using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using System.Windows.Forms;
namespace Forum
{
public partial class FormMain : Form
{
public FormMain()
private readonly IMessageLogic _messageLogic;
public FormMain(IMessageLogic messageLogic)
{
InitializeComponent();
_messageLogic = messageLogic;
}
private void ðîëèToolStripMenuItem_Click(object sender, EventArgs e)
@ -36,5 +43,95 @@ namespace Forum
form.ShowDialog();
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage));
if (service is FormCreateMessage form)
{
form.ShowDialog();
LoadData();
}
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _messageLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["UserId"].Visible = false;
dataGridView.Columns["TopicId"].Visible = false;
dataGridView.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["TopicName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Text"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["Date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
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 (!_messageLogic.Delete(new MessageBindingModel
{
Id = id
}))
{
throw new Exception("Îøèáêà ïðè óäàëåíèè.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
LoadData();
}
private void buttonChange_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateMessage));
if (service is FormCreateMessage form)
{
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
}
}

View File

@ -48,6 +48,7 @@ namespace Forum
services.AddTransient<FormCategory>();
services.AddTransient<FormAddTopics>();
services.AddTransient<FormAddTopic>();
services.AddTransient<FormCreateMessage>();
}
}
}

View File

@ -94,10 +94,6 @@ namespace ForumBusinessLogic
Name = model.Name
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Тема с таким названием уже есть");
}
}
}
}

View File

@ -14,5 +14,9 @@ namespace ForumContracts.BindingModels
public DateTime Date { get; set; }
public int Id { get; set; }
public int TopicId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -12,5 +12,7 @@ namespace ForumContracts.BindingModels
public string Name { get; set; } = string.Empty;
public int Id { get; set; }
public int CategoryId { get; set; }
}
}

View File

@ -8,7 +8,7 @@ namespace ForumContracts.SearchModels
{
public class MessageSearchModel
{
public int Id { get; set; }
public int? Id { get; set; }
public DateTime Date { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace ForumContracts.SearchModels
{
public int? Id { get; set; }
public string? Name { get; set; }
public int? CategoryId { get; set; }
}
}

View File

@ -10,6 +10,16 @@ namespace ForumContracts.ViewModels
{
public class MessageViewModel : IMessageModel
{
public int UserId { get; set; }
[DisplayName("Пользователь")]
public string Username { get; set; } = string.Empty;
public int TopicId { get; set; }
[DisplayName("Название темы")]
public string TopicName { get; set; } = string.Empty;
[DisplayName("Текст сообщения")]
public string Text { get; set; } = string.Empty;

View File

@ -14,5 +14,7 @@ namespace ForumContracts.ViewModels
public string Name { get; set; } = string.Empty;
public int Id { get; set; }
public int CategoryId { get; set; }
}
}

View File

@ -9,5 +9,7 @@ namespace ForumDataModels
public interface ITopicModel : IId
{
string Name { get; }
int CategoryId { get; }
}
}

View File

@ -2,6 +2,8 @@
using ForumContracts.SearchModels;
using ForumContracts.StoragesContracts;
using ForumContracts.ViewModels;
using ForumDatabase.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,32 +16,74 @@ namespace ForumDatabase.Implements
{
public MessageViewModel? Delete(MessageBindingModel model)
{
throw new NotImplementedException();
using var context = new ForumDatabase();
var element = context.Messages.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Messages.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public MessageViewModel? GetElement(MessageSearchModel model)
{
throw new NotImplementedException();
if (!model.Id.HasValue)
{
return null;
}
using var context = new ForumDatabase();
return context.Messages
.Include(x => x.User)
.Include(x => x.Topic)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id).GetViewModel;
}
public List<MessageViewModel> GetFilteredList(MessageSearchModel model)
{
throw new NotImplementedException();
using var context = new ForumDatabase();
return context.Messages
.Include(x => x.User)
.Include(x => x.Topic)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageViewModel> GetFullList()
{
throw new NotImplementedException();
using var context = new ForumDatabase();
return context.Messages
.Include(x => x.User)
.Include(x => x.Topic)
.Select(x => x.GetViewModel)
.ToList();
}
public MessageViewModel? Insert(MessageBindingModel model)
{
throw new NotImplementedException();
var newMessage = Message.Create(model);
if (newMessage == null)
{
return null;
}
using var context = new ForumDatabase();
context.Messages.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
}
public MessageViewModel? Update(MessageBindingModel model)
{
throw new NotImplementedException();
using var context = new ForumDatabase();
var component = context.Messages.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}

View File

@ -3,6 +3,7 @@ using ForumContracts.SearchModels;
using ForumContracts.StoragesContracts;
using ForumContracts.ViewModels;
using ForumDatabase.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -43,11 +44,14 @@ namespace ForumDatabase.Implements
public List<TopicViewModel> GetFilteredList(TopicSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ForumDatabase();
if (model.CategoryId.HasValue)
{
return context.Topics
.Where(x => x.CategoryId == model.CategoryId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Topics
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)

View File

@ -3,6 +3,7 @@ using ForumContracts.SearchModels;
using ForumContracts.StoragesContracts;
using ForumContracts.ViewModels;
using ForumDatabase.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -34,6 +35,7 @@ namespace ForumDatabase.Implements
}
using var context = new ForumDatabase();
return context.Users
.Include(x => x.Role)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Username) && x.Username ==
model.Username) ||
@ -49,6 +51,7 @@ namespace ForumDatabase.Implements
}
using var context = new ForumDatabase();
return context.Users
.Include(x => x.Role)
.Where(x => x.Username.Contains(model.Username))
.Select(x => x.GetViewModel)
.ToList();
@ -58,6 +61,7 @@ namespace ForumDatabase.Implements
{
using var context = new ForumDatabase();
return context.Users
.Include(x => x.Role)
.Select(x => x.GetViewModel)
.ToList();
}

View File

@ -27,6 +27,10 @@ namespace ForumDatabase.Models
[Required]
public int TopicId { get; set; }
public virtual User? User { get; set; }
public virtual Topic? Topic { get; set; }
public static Message? Create(MessageBindingModel model)
{
if (model == null)
@ -38,6 +42,8 @@ namespace ForumDatabase.Models
Id = model.Id,
Text = model.Text,
Date = model.Date,
UserId = model.UserId,
TopicId = model.TopicId,
};
}
public void Update(MessageBindingModel model)
@ -54,6 +60,10 @@ namespace ForumDatabase.Models
Id = Id,
Text = Text,
Date = Date,
TopicId = TopicId,
UserId = UserId,
Username = User == null ? string.Empty : User.Username,
TopicName = Topic == null ? string.Empty : Topic.Name,
};
}
}

View File

@ -28,6 +28,8 @@ namespace ForumDatabase.Models
[ForeignKey("MessageId")]
List<Message> Messages { get; set; } = new();
public virtual Category? Category { get; set; }
public static Topic? Create(TopicBindingModel model)
{
if (model == null)
@ -38,6 +40,7 @@ namespace ForumDatabase.Models
{
Id = model.Id,
Name = model.Name,
CategoryId = model.CategoryId,
};
}
public void Update(TopicBindingModel model)
@ -53,6 +56,7 @@ namespace ForumDatabase.Models
{
Id = Id,
Name = Name,
CategoryId = CategoryId,
};
}
}