115 lines
4.1 KiB
C#

using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierContracts.SearchModels;
using Microsoft.Extensions.Logging;
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 SewingDresses
{
public partial class FormEmployee : Form
{
private readonly ILogger _logger;
private readonly IEmployeeLogic _logic;
private int? _id;
public int ID { set { _id = value; } }
public FormEmployee(ILogger<FormEmployees> logger, IEmployeeLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void buttonSaveEmployee_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(textBoxPassword.Text))
{
MessageBox.Show("Fill password field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxFullName.Text))
{
MessageBox.Show("Fill fullname field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxExperience.Text))
{
MessageBox.Show("Fill experience field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxQualification.Text))
{
MessageBox.Show("Fill qualification field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Saving employee");
try
{
var model = new EmployeeBindingModel
{
ID = _id ?? 0,
Password = textBoxPassword.Text,
FullName = textBoxFullName.Text,
WorkExperience = Convert.ToInt32(textBoxExperience.Text),
Qualification = Convert.ToInt32(textBoxQualification.Text),
};
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if(!operationResult)
{
throw new Exception("Error during saving. Extra info in logs.");
}
MessageBox.Show("Saving was succesful", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch(Exception ex)
{
_logger.LogError(ex, "Error saving employee");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void FormEmployee_Load(object sender, EventArgs e)
{
if(_id.HasValue)
{
try
{
_logger.LogInformation("Receiving employee");
var view = _logic.ReadElement(new EmployeeSearchModel
{
ID = _id.Value
});
if (view != null)
{
textBoxPassword.Text = view.Password;
textBoxFullName.Text = view.FullName;
textBoxExperience.Text = view.WorkExperience.ToString();
textBoxQualification.Text = view.Qualification.ToString();
}
}
catch(Exception ex)
{
_logger.LogError(ex, "Error receiving employee");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}