140 lines
5.1 KiB
C#
140 lines
5.1 KiB
C#
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|