PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesView/ImplementForm.cs

112 lines
3.0 KiB
C#
Raw Normal View History

2024-05-26 13:40:22 +04:00
using Microsoft.Extensions.Logging;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.BusinessLogicsContracts;
using SewingDressesContracts.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 SewingDressesView
{
public partial class ImplementForm : Form
{
private readonly ILogger _logger;
private readonly IImplementLogic _logic;
private int? _id;
public int ID { set { _id = value; } }
public ImplementForm(ILogger<ImplementForm> logger, IImplementLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFIO.Text))
{
MessageBox.Show("Empty Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxPassword.Text))
{
MessageBox.Show("Empty Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxExp.Text))
{
MessageBox.Show("Empty Experience", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxQual.Text))
{
MessageBox.Show("Empty Qualification", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Saving Imlement");
try
{
var model = new ImplementBindingModel
{
Id = _id ?? 0,
Password = textBoxPassword.Text,
ImplementFIO = textBoxFIO.Text,
WorkExperience = Convert.ToInt32(textBoxExp.Text),
Qualification = Convert.ToInt32(textBoxQual.Text)
};
var result = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!result)
throw new Exception("Error on saving implement");
MessageBox.Show("Saving success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving implement");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void ImplementForm_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
_logger.LogInformation("Init implement");
var view = _logic.ReadElement(new ImplementSearchModel
{
Id = _id.Value,
});
if (view != null)
{
textBoxPassword.Text = view.Password;
textBoxFIO.Text = view.ImplementFIO;
textBoxExp.Text = view.WorkExperience.ToString();
textBoxQual.Text = view.Qualification.ToString();
}
}
catch(Exception ex)
{
_logger.LogError(ex, "Init implement error");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}