SUBD/CarRent/FormCreateCar.cs

147 lines
4.2 KiB
C#
Raw Normal View History

using CarRentBusinessLogic.BusinessLogics;
using CarRentContracts.BindingModels;
using CarRentContracts.BusinessLogicContracts;
using CarRentContracts.SearchModels;
using ClientRentBusinessLogic.BusinessLogics;
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 CarRent
{
public partial class FormCreateCar : Form
{
private readonly ICarLogic _carLogic;
private readonly IBranchLogic _branchLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormCreateCar(ICarLogic carLogic,
IBranchLogic branchLogic)
{
InitializeComponent();
_carLogic = carLogic;
_branchLogic = branchLogic;
}
private void LoadData()
{
try
{
var list = _branchLogic.ReadList(null);
if (list != null)
{
comboBoxBranch.DisplayMember = "Name";
comboBoxBranch.ValueMember = "Id";
comboBoxBranch.DataSource = list;
comboBoxBranch.SelectedItem = null;
}
}
catch (Exception ex)
{ }
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxBrand.Text))
{
MessageBox.Show("Введите производителя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(textBoxColor.Text))
{
MessageBox.Show("Введите цвет автомобиля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(textBoxLicensePlate.Text))
{
MessageBox.Show("Введите номер автомобиля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(textBoxModel.Text))
{
MessageBox.Show("Введите модель автомобиля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(textBoxRentalCost.Text))
{
MessageBox.Show("Введите стоимость часовой аренды автомобиля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(comboBoxBranch.Text))
{
MessageBox.Show("Выберите филиал, в котором находится автомобиль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
var model = new CarBindingModel
{
Id = _id ?? 0,
Brand = textBoxBrand.Text,
Model = textBoxModel.Text,
YearOfManuf = DateTime.SpecifyKind(dateTimePicker.Value, DateTimeKind.Utc),
Color = textBoxColor.Text,
LicensePlate = textBoxLicensePlate.Text,
RentalCostPerHour = Convert.ToDouble(textBoxRentalCost.Text),
BranchId = Convert.ToInt32(comboBoxBranch.SelectedValue),
};
var operationResult = _id.HasValue ? _carLogic.Update(model) : _carLogic.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();
}
private void FormCreateCar_Load(object sender, EventArgs e)
{
LoadData();
if (_id.HasValue)
{
try
{
var view = _carLogic.ReadElement(new CarSearchModel { Id = _id.Value });
if (view != null)
{
textBoxBrand.Text = view.Brand;
textBoxColor.Text = view.Color;
textBoxLicensePlate.Text = view.LicensePlate;
textBoxModel.Text = view.Model;
textBoxRentalCost.Text = view.RentalCostPerHour.ToString();
dateTimePicker.Value = view.YearOfManuf;
comboBoxBranch.SelectedIndex = view.BranchId;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}