diff --git a/TravelCompany/TravelCompany/FormCreateOrder.Designer.cs b/TravelCompany/TravelCompany/FormCreateOrder.Designer.cs index 1ec37f3..cd34044 100644 --- a/TravelCompany/TravelCompany/FormCreateOrder.Designer.cs +++ b/TravelCompany/TravelCompany/FormCreateOrder.Designer.cs @@ -71,7 +71,6 @@ textBoxCount.Name = "textBoxCount"; textBoxCount.Size = new Size(246, 23); textBoxCount.TabIndex = 3; - textBoxCount.TextChanged += textBoxCount_TextChanged; // // comboBoxTravel // @@ -98,7 +97,6 @@ buttonSave.TabIndex = 6; buttonSave.Text = "Сохранить"; buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; // // buttonCancel // @@ -108,7 +106,6 @@ buttonCancel.TabIndex = 7; buttonCancel.Text = "Отмена"; buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; // // FormCreateOrder // diff --git a/TravelCompany/TravelCompany/FormCreateOrder.cs b/TravelCompany/TravelCompany/FormCreateOrder.cs index 8cff27f..099b516 100644 --- a/TravelCompany/TravelCompany/FormCreateOrder.cs +++ b/TravelCompany/TravelCompany/FormCreateOrder.cs @@ -1,16 +1,8 @@ -using TravelCompanyContracts.BindingModels; +using Microsoft.Extensions.Logging; +using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.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; +using TravelCompanyContracts.ViewModels; namespace TravelCompany.Forms { @@ -19,6 +11,8 @@ namespace TravelCompany.Forms private readonly ILogger _logger; private readonly ITravelLogic _logicP; private readonly IOrderLogic _logicO; + private List? _list; + public FormCreateOrder(ILogger logger, ITravelLogic logicP, IOrderLogic logicO) { InitializeComponent(); @@ -29,23 +23,17 @@ namespace TravelCompany.Forms private void FormCreateOrder_Load(object sender, EventArgs e) { - try + _list = _logicP.ReadList(null); + if (_list != null) { - var list = _logicP.ReadList(null); - if (list != null) - { - comboBoxTravel.DisplayMember = "TravelName"; - comboBoxTravel.ValueMember = "Id"; - comboBoxTravel.DataSource = list; - comboBoxTravel.SelectedItem = null; - _logger.LogInformation("Загрузка изделий для заказа"); - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки списка изделий"); + comboBoxTravel.DisplayMember = "TravelName"; + comboBoxTravel.ValueMember = "Id"; + comboBoxTravel.DataSource = _list; + comboBoxTravel.SelectedItem = null; + _logger.LogInformation("Загрузка пиццы для заказа"); } } + private void CalcSum() { if (comboBoxTravel.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) @@ -68,7 +56,8 @@ namespace TravelCompany.Forms } } } - private void textBoxCount_TextChanged(object sender, EventArgs e) + + private void TextBoxCount_TextChanged(object sender, EventArgs e) { CalcSum(); } @@ -78,7 +67,7 @@ namespace TravelCompany.Forms CalcSum(); } - private void buttonSave_Click(object sender, EventArgs e) + private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxCount.Text)) { @@ -87,7 +76,7 @@ namespace TravelCompany.Forms } if (comboBoxTravel.SelectedValue == null) { - MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Создание заказа"); @@ -109,15 +98,15 @@ namespace TravelCompany.Forms } catch (Exception ex) { - _logger.LogError(ex, "Ошибка создания заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError(ex, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void buttonCancel_Click(object sender, EventArgs e) + private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } - } } diff --git a/TravelCompany/TravelCompany/FormTravels.Designer.cs b/TravelCompany/TravelCompany/FormTravels.Designer.cs index 053d8b3..2c27ec8 100644 --- a/TravelCompany/TravelCompany/FormTravels.Designer.cs +++ b/TravelCompany/TravelCompany/FormTravels.Designer.cs @@ -44,7 +44,6 @@ buttonRef.TabIndex = 9; buttonRef.Text = "Обновить"; buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; // // buttonDel // @@ -54,7 +53,6 @@ buttonDel.TabIndex = 8; buttonDel.Text = "Удалить"; buttonDel.UseVisualStyleBackColor = true; - buttonDel.Click += buttonDel_Click; // // buttonUpd // @@ -64,7 +62,6 @@ buttonUpd.TabIndex = 7; buttonUpd.Text = "Изменить"; buttonUpd.UseVisualStyleBackColor = true; - buttonUpd.Click += buttonUpd_Click; // // buttonAdd // @@ -74,7 +71,6 @@ buttonAdd.TabIndex = 6; buttonAdd.Text = "Добавить"; buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; // // dataGridView // @@ -100,7 +96,6 @@ Margin = new Padding(3, 2, 3, 2); Name = "FormTravels"; Text = "Список туристических путёвок"; - Load += FormTravels_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); } diff --git a/TravelCompany/TravelCompany/FormTravels.cs b/TravelCompany/TravelCompany/FormTravels.cs index b772ffd..e83d830 100644 --- a/TravelCompany/TravelCompany/FormTravels.cs +++ b/TravelCompany/TravelCompany/FormTravels.cs @@ -1,16 +1,6 @@ -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; -using TravelCompany; +using Microsoft.Extensions.Logging; using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.BusinessLogicsContracts; -using Microsoft.Extensions.Logging; namespace TravelCompany.Forms { @@ -18,6 +8,7 @@ namespace TravelCompany.Forms { private readonly ILogger _logger; private readonly ITravelLogic _logic; + public FormTravels(ILogger logger, ITravelLogic logic) { InitializeComponent(); @@ -25,7 +16,7 @@ namespace TravelCompany.Forms _logic = logic; } - private void FormTravels_Load(object sender, EventArgs e) + private void FormTravel_Load(object sender, EventArgs e) { LoadData(); } @@ -40,17 +31,19 @@ namespace TravelCompany.Forms dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["TravelComponents"].Visible = false; - dataGridView.Columns["TravelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["TravelName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; } - _logger.LogInformation("Загрузка путёвок"); + _logger.LogInformation("Загрузка пиццы"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки компонентов"); + _logger.LogError(ex, "Ошибка загрузки пиццы"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void buttonAdd_Click(object sender, EventArgs e) + private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormTravel)); if (service is FormTravel form) @@ -62,7 +55,7 @@ namespace TravelCompany.Forms } } - private void buttonUpd_Click(object sender, EventArgs e) + private void ButtonUpd_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { @@ -78,15 +71,14 @@ namespace TravelCompany.Forms } } - private void buttonDel_Click(object sender, EventArgs e) + private void ButtonDel_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - int id = - Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление путёвок"); + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление пиццы"); try { if (!_logic.Delete(new TravelBindingModel @@ -100,14 +92,14 @@ namespace TravelCompany.Forms } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления путевки"); + _logger.LogError(ex, "Ошибка удаления пиццы"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } - private void buttonRef_Click(object sender, EventArgs e) + private void ButtonRef_Click(object sender, EventArgs e) { LoadData(); } diff --git a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/OrderLogic.cs b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/OrderLogic.cs index e7c1295..2f2a393 100644 --- a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/OrderLogic.cs @@ -1,33 +1,25 @@ -using TravelCompanyContracts.BindingModels; +using Microsoft.Extensions.Logging; +using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.ViewModels; using TravelCompanyDataModels.Enums; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace TravelCompanyBusinessLogic.BusinessLogic +namespace TravelCompanyBusinessLogic.BusinessLogics { public class OrderLogic : IOrderLogic { - //Класс с логикой для заказов будет отвечать за получение списка заказов, - //создания заказа и смены его статусов. Следует учитывать, что у заказа можно - //менять статус на новый, если его текущий статус предшествует новому - private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; } - public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); @@ -40,18 +32,14 @@ namespace TravelCompanyBusinessLogic.BusinessLogic _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } - public bool CreateOrder(OrderBindingModel model) { CheckModel(model); - if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) { - model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } @@ -60,57 +48,34 @@ namespace TravelCompanyBusinessLogic.BusinessLogic public bool TakeOrderInWork(OrderBindingModel model) { - return ToNextStatus(model, OrderStatus.Выполняется); + return ChangeStatus(model, OrderStatus.Выполняется); } public bool FinishOrder(OrderBindingModel model) { - return ToNextStatus(model, OrderStatus.Готов); + return ChangeStatus(model, OrderStatus.Готов); } public bool DeliveryOrder(OrderBindingModel model) { - return ToNextStatus(model, OrderStatus.Выдан); - } - - public bool ToNextStatus(OrderBindingModel model, OrderStatus orderStatus) - { - CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel() + var order = _orderStorage.GetElement(new OrderSearchModel { - Id = model.Id + Id = model.Id, }); - if (element == null) + if (order == null) { - throw new ArgumentNullException(nameof(element)); + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + TravelId = order.TravelId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); } - model.TravelId = element.TravelId; - model.DateCreate = element.DateCreate; - model.DateImplement = element.DateImplement; - model.Status = element.Status; - model.Count = element.Count; - model.Sum = element.Sum; - - if (model.Status != orderStatus - 1) - { - _logger.LogWarning("Status update to " + orderStatus + " operation failed"); - return false; - } - model.Status = orderStatus; - - if (model.Status == OrderStatus.Выдан) - { - model.DateImplement = DateTime.Now; - } - - if (_orderStorage.Update(model) == null) - { - model.Status--; - _logger.LogWarning("Changing status operation faled"); - return false; - } - return true; + return ChangeStatus(model, OrderStatus.Выдан); } private void CheckModel(OrderBindingModel model, bool withParams = true) @@ -125,17 +90,51 @@ namespace TravelCompanyBusinessLogic.BusinessLogic } if (model.Count <= 0) { - throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); + throw new ArgumentException("Колличество пиццы в заказе не может быть меньше 1", nameof(model.Count)); } if (model.Sum <= 0) { - throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); + throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum)); } if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) { - throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} должна быть позже даты его создания {model.DateCreate}"); + throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); } - _logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.TravelId, model.Count, model.Sum, model.Id); + _logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}", + model.TravelId, model.Count, model.Sum, model.Id); + } + + private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel() + { + Id = model.Id + }); + if (element == null) + { + throw new ArgumentNullException(nameof(element)); + } + model.DateCreate = element.DateCreate; + model.TravelId = element.TravelId; + model.DateImplement = element.DateImplement; + model.Status = element.Status; + model.Count = element.Count; + model.Sum = element.Sum; + if (requiredStatus - model.Status == 1) + { + model.Status = requiredStatus; + if (model.Status == OrderStatus.Выдан) + model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); + throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); } } } diff --git a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ShopLogic.cs b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ShopLogic.cs index 82e6c71..1021630 100644 --- a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ShopLogic.cs @@ -1,14 +1,14 @@ using Microsoft.Extensions.Logging; -using TravelCompanyContracts.BindingModels; -using TravelCompanyContracts.BusinessLogicsContracts; -using TravelCompanyContracts.SearchModels; -using TravelCompanyContracts.StoragesContracts; -using TravelCompanyContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.BusinessLogicsContracts; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; namespace TravelCompanyBusinessLogic.BusinessLogics { @@ -125,6 +125,17 @@ namespace TravelCompanyBusinessLogic.BusinessLogics } shop.ShopTravels.Add(model.TravelId, (Travel, model.Count)); } + + _shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + ShopTravels = shop.ShopTravels, + TravelMaxCount = shop.TravelMaxCount, + }); + return true; } diff --git a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/TravelLogic.cs b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/TravelLogic.cs index 69719fe..595b82b 100644 --- a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/TravelLogic.cs +++ b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/TravelLogic.cs @@ -16,8 +16,7 @@ namespace TravelCompanyBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly ITravelStorage _TravelStorage; - public TravelLogic(ILogger logger, ITravelStorage - TravelStorage) + public TravelLogic(ILogger logger, ITravelStorage TravelStorage) { _logger = logger; _TravelStorage = TravelStorage;