Рефакторинг

This commit is contained in:
user 2024-12-15 02:05:24 +04:00
parent 31367a7ba2
commit 6989711277
3 changed files with 66 additions and 66 deletions

View File

@ -50,47 +50,49 @@ public partial class FormAppointment : Form
dateTimePickerEnd.ValueChanged += DateTimePickerEnd_ValueChanged;
}
private void DateTimePickerEnd_ValueChanged(object sender, EventArgs e)
}
private void DateTimePickerEnd_ValueChanged(object sender, EventArgs e)
{
if (dateTimePickerEnd.CustomFormat == "В процессе")
{
if (dateTimePickerEnd.CustomFormat == "В процессе")
{
dateTimePickerEnd.CustomFormat = "dd.MM.yyyy HH:mm";
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
try
{
if (comboBoxEmployee.SelectedIndex < 0 || comboBoxOrder.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненные поля");
}
if (_appointmentId.HasValue)
{
_appointmentRepository.UpdateAppointment(CreateAppointment(_appointmentId.Value));
}
else
{
_appointmentRepository.CreateAppointment(CreateAppointment(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Appointment CreateAppointment(int id)
{
DateTime? endDate = dateTimePickerEnd.CustomFormat == "В процессе" ? (DateTime?)null : dateTimePickerEnd.Value;
return Appointment.CreateOperation(id, (int)comboBoxOrder.SelectedValue!, (int)comboBoxEmployee.SelectedValue!, Convert.ToDateTime(dateTimePickerStart.Value),
endDate);
dateTimePickerEnd.CustomFormat = "dd.MM.yyyy HH:mm";
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
try
{
if (comboBoxEmployee.SelectedIndex < 0 || comboBoxOrder.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненные поля");
}
if (_appointmentId.HasValue)
{
_appointmentRepository.UpdateAppointment(CreateAppointment(_appointmentId.Value));
}
else
{
_appointmentRepository.CreateAppointment(CreateAppointment(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Appointment CreateAppointment(int id)
{
DateTime? endDate = dateTimePickerEnd.CustomFormat == "В процессе" ? (DateTime?)null : dateTimePickerEnd.Value;
return Appointment.CreateOperation(id, (int)comboBoxOrder.SelectedValue!, (int)comboBoxEmployee.SelectedValue!, Convert.ToDateTime(dateTimePickerStart.Value),
endDate);
}
}

View File

@ -80,7 +80,6 @@
buttonRemove.Size = new Size(86, 101);
buttonRemove.TabIndex = 3;
buttonRemove.UseVisualStyleBackColor = true;
buttonRemove.Click += buttonRemove_Click;
//
// buttonUpdate
//

View File

@ -22,34 +22,33 @@ internal static class Program
Application.Run(CreateContainer().Resolve<FormItCompany>());
}
private static IUnityContainer CreateContainer()
{
var container = new UnityContainer();
private static IUnityContainer CreateContainer()
{
var container = new UnityContainer();
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
container.RegisterType<IOrderRepository, OrderRepository>();
container.RegisterType<IAppointmentRepository, AppointmentRepository>();
container.RegisterType<IServiceRepository, ServiceRepository>();
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
container.RegisterType<ICompanyRepository, CompanyRepository>();
container.RegisterType<IOrderRepository, OrderRepository>();
container.RegisterType<IAppointmentRepository, AppointmentRepository>();
container.RegisterType<IServiceRepository, ServiceRepository>();
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
container.RegisterType<ICompanyRepository, CompanyRepository>();
container.RegisterType<IConnectionString, ConnectionString>();
return container;
}
private static LoggerFactory CreateLoggerFactory()
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build())
.CreateLogger());
return loggerFactory;
}
return container;
}
}
private static LoggerFactory CreateLoggerFactory()
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build())
.CreateLogger());
return loggerFactory;
}
}