diff --git a/.gitignore b/.gitignore index ca1c7a3..6192267 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/AutomobilePlant/ImplementationExtensions \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant.sln b/AutomobilePlant/AutomobilePlant.sln index 7e4bb3f..93ebfba 100644 --- a/AutomobilePlant/AutomobilePlant.sln +++ b/AutomobilePlant/AutomobilePlant.sln @@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlant", "AutomobilePlant\AutomobilePlant.csproj", "{970E66D6-3147-419D-98DC-9E05B10232B5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlant", "AutomobilePlant\AutomobilePlant.csproj", "{970E66D6-3147-419D-98DC-9E05B10232B5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDataModels", "AutomobilePlantDataModels\AutomobilePlantDataModels.csproj", "{27B6E4A1-BB53-4AF6-AACA-099A40B64D60}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantDataModels", "AutomobilePlantDataModels\AutomobilePlantDataModels.csproj", "{27B6E4A1-BB53-4AF6-AACA-099A40B64D60}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantContracts", "AutomobilePlantContracts\AutomobilePlantContracts.csproj", "{2F1D0A55-2722-48B4-87D1-0440E74D6E0A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantContracts", "AutomobilePlantContracts\AutomobilePlantContracts.csproj", "{2F1D0A55-2722-48B4-87D1-0440E74D6E0A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantBusinessLogic", "AutomobilePlantBusinessLogic\AutomobilePlantBusinessLogic.csproj", "{CC4E3C20-ABA4-4A54-B992-B68D39A1236E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantBusinessLogic", "AutomobilePlantBusinessLogic\AutomobilePlantBusinessLogic.csproj", "{CC4E3C20-ABA4-4A54-B992-B68D39A1236E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantListImplements", "AutomobilePlantListImplements\AutomobilePlantListImplements.csproj", "{B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantListImplements", "AutomobilePlantListImplements\AutomobilePlantListImplements.csproj", "{B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantFileImplement", "AutomobilePlantFileImplement\AutomobilePlantFileImplement.csproj", "{10818526-654C-4928-985F-9C02A2A02B15}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Debug|Any CPU.Build.0 = Debug|Any CPU {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Release|Any CPU.ActiveCfg = Release|Any CPU {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Release|Any CPU.Build.0 = Release|Any CPU + {10818526-654C-4928-985F-9C02A2A02B15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10818526-654C-4928-985F-9C02A2A02B15}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10818526-654C-4928-985F-9C02A2A02B15}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10818526-654C-4928-985F-9C02A2A02B15}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj index a64875e..18528cf 100644 --- a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj +++ b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj @@ -17,6 +17,7 @@ + diff --git a/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs b/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs index 4903a7a..c364e1e 100644 --- a/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs +++ b/AutomobilePlant/AutomobilePlant/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.buttonSetToDone = new System.Windows.Forms.Button(); this.buttonSetToFinish = new System.Windows.Forms.Button(); this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonSellCars = new System.Windows.Forms.Button(); this.menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -156,11 +157,22 @@ this.buttonUpdate.UseVisualStyleBackColor = true; this.buttonUpdate.Click += new System.EventHandler(this.ButtonUpdate_Click); // + // buttonSellCars + // + this.buttonSellCars.Location = new System.Drawing.Point(969, 218); + this.buttonSellCars.Name = "buttonSellCars"; + this.buttonSellCars.Size = new System.Drawing.Size(194, 29); + this.buttonSellCars.TabIndex = 7; + this.buttonSellCars.Text = "Продажа автомобилей"; + this.buttonSellCars.UseVisualStyleBackColor = true; + this.buttonSellCars.Click += new System.EventHandler(this.buttonSellCars_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1176, 450); + this.Controls.Add(this.buttonSellCars); this.Controls.Add(this.buttonSupplyShop); this.Controls.Add(this.buttonUpdate); this.Controls.Add(this.buttonSetToFinish); @@ -195,5 +207,6 @@ private Button buttonUpdate; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonSupplyShop; + private Button buttonSellCars; } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormMain.cs b/AutomobilePlant/AutomobilePlant/FormMain.cs index 1059b13..e846d6f 100644 --- a/AutomobilePlant/AutomobilePlant/FormMain.cs +++ b/AutomobilePlant/AutomobilePlant/FormMain.cs @@ -182,5 +182,14 @@ namespace AutomobilePlant form.ShowDialog(); } } + + private void buttonSellCars_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopSell)); + if (service is FormShopSell form) + { + form.ShowDialog(); + } + } } } diff --git a/AutomobilePlant/AutomobilePlant/FormShop.Designer.cs b/AutomobilePlant/AutomobilePlant/FormShop.Designer.cs index c0b43fa..67f4e9d 100644 --- a/AutomobilePlant/AutomobilePlant/FormShop.Designer.cs +++ b/AutomobilePlant/AutomobilePlant/FormShop.Designer.cs @@ -36,10 +36,12 @@ this.ColumnId = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnCarName = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.buttonCancel = new System.Windows.Forms.Button(); - this.buttonSave = new System.Windows.Forms.Button(); this.openingDateLabel = new System.Windows.Forms.Label(); this.openingDateTimePicker = new System.Windows.Forms.DateTimePicker(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.labelMaxCount = new System.Windows.Forms.Label(); + this.textBoxMaxCountCars = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); // @@ -82,11 +84,11 @@ this.ColumnId, this.ColumnCarName, this.ColumnCount}); - this.dataGridView.Location = new System.Drawing.Point(14, 127); + this.dataGridView.Location = new System.Drawing.Point(14, 173); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(401, 289); + this.dataGridView.Size = new System.Drawing.Size(401, 265); this.dataGridView.TabIndex = 10; // // ColumnId @@ -147,11 +149,28 @@ this.openingDateTimePicker.Size = new System.Drawing.Size(274, 27); this.openingDateTimePicker.TabIndex = 14; // + // labelMaxCount + // + this.labelMaxCount.AutoSize = true; + this.labelMaxCount.Location = new System.Drawing.Point(18, 128); + this.labelMaxCount.Name = "labelMaxCount"; + this.labelMaxCount.Size = new System.Drawing.Size(216, 20); + this.labelMaxCount.TabIndex = 13; + this.labelMaxCount.Text = "Макс. количество авто"; + // + // textBoxMaxCountCars + // + this.textBoxMaxCountCars.Location = new System.Drawing.Point(240, 128); + this.textBoxMaxCountCars.Name = "textBoxMaxCountCars"; + this.textBoxMaxCountCars.Size = new System.Drawing.Size(175, 27); + this.textBoxMaxCountCars.TabIndex = 14; // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(445, 489); + this.Controls.Add(this.textBoxMaxCountCars); + this.Controls.Add(this.labelMaxCount); this.Controls.Add(this.openingDateTimePicker); this.Controls.Add(this.openingDateLabel); this.Controls.Add(this.buttonCancel); @@ -184,5 +203,7 @@ private DataGridViewTextBoxColumn ColumnCount; private Label openingDateLabel; private DateTimePicker openingDateTimePicker; + private Label labelMaxCount; + private TextBox textBoxMaxCountCars; } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormShop.cs b/AutomobilePlant/AutomobilePlant/FormShop.cs index 51324b9..206e949 100644 --- a/AutomobilePlant/AutomobilePlant/FormShop.cs +++ b/AutomobilePlant/AutomobilePlant/FormShop.cs @@ -47,6 +47,7 @@ namespace AutomobilePlant textBoxAddress.Text = view.Address; openingDateTimePicker.Value = view.OpeningDate; _shopCars = view.ShopCars ?? new Dictionary(); + textBoxMaxCountCars.Text = view.MaxCountCars.ToString(); LoadData(); } } @@ -94,6 +95,12 @@ namespace AutomobilePlant return; } + if (string.IsNullOrEmpty(textBoxMaxCountCars.Text)) + { + MessageBox.Show("Заполните макс. количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); try @@ -104,7 +111,8 @@ namespace AutomobilePlant Name = textBoxName.Text, Address = textBoxAddress.Text, OpeningDate = openingDateTimePicker.Value.Date, - ShopCars= _shopCars + MaxCountCars = Convert.ToInt32(textBoxMaxCountCars.Text), + ShopCars = _shopCars }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs b/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs new file mode 100644 index 0000000..b8b0539 --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.Designer.cs @@ -0,0 +1,119 @@ +namespace AutomobilePlant +{ + partial class FormShopSell + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelCount = new System.Windows.Forms.Label(); + this.labelCar = new System.Windows.Forms.Label(); + this.comboBoxCar = new System.Windows.Forms.ComboBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(23, 76); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(93, 20); + this.labelCount.TabIndex = 1; + this.labelCount.Text = "Количество:"; + // + // labelCar + // + this.labelCar.AutoSize = true; + this.labelCar.Location = new System.Drawing.Point(23, 24); + this.labelCar.Name = "labelCar"; + this.labelCar.Size = new System.Drawing.Size(79, 20); + this.labelCar.TabIndex = 2; + this.labelCar.Text = "Автомобиль:"; + // + // comboBoxCar + // + this.comboBoxCar.FormattingEnabled = true; + this.comboBoxCar.Location = new System.Drawing.Point(124, 24); + this.comboBoxCar.Name = "comboBoxCar"; + this.comboBoxCar.Size = new System.Drawing.Size(278, 28); + this.comboBoxCar.TabIndex = 4; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(124, 73); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(158, 27); + this.textBoxCount.TabIndex = 5; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(188, 142); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(94, 29); + this.buttonSell.TabIndex = 6; + this.buttonSell.Text = "Продажа"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(308, 142); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormShopSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(459, 198); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxCar); + this.Controls.Add(this.labelCar); + this.Controls.Add(this.labelCount); + this.Name = "FormShopSell"; + this.Text = "Продажа из магазина"; + this.Load += new System.EventHandler(this.FormShopSell_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + private Label labelCount; + private Label labelCar; + private ComboBox comboBoxCar; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.cs b/AutomobilePlant/AutomobilePlant/FormShopSell.cs new file mode 100644 index 0000000..4f0c0a3 --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.cs @@ -0,0 +1,95 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.BusinessLogicContracts; +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 AutomobilePlant +{ + public partial class FormShopSell : Form + { + private readonly ILogger _logger; + private readonly ICarLogic _logicCar; + private readonly IShopLogic _logicShop; + + public FormShopSell(ILogger logger, ICarLogic logicCar, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicCar = logicCar; + _logicShop = logicShop; + } + + private void FormShopSell_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка авто для продажи"); + try + { + var list = _logicCar.ReadList(null); + if (list != null) + { + comboBoxCar.DisplayMember = "CarName"; + comboBoxCar.ValueMember = "Id"; + comboBoxCar.DataSource = list; + comboBoxCar.SelectedItem = null; + } + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка авто"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxCar.SelectedValue == null) + { + MessageBox.Show("Выберите авто", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание продажи"); + try + { + var operationResult = _logicShop.SellCar( + new CarBindingModel + { + Id = Convert.ToInt32(comboBoxCar.SelectedValue) + }, + Convert.ToInt32(textBoxCount.Text) + ); + if (!operationResult) + { + throw new Exception("Ошибка при создании продажи. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания продажи"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/AutomobilePlant/AutomobilePlant/FormShopSell.resx b/AutomobilePlant/AutomobilePlant/FormShopSell.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/AutomobilePlant/AutomobilePlant/FormShopSell.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index 1acc6fd..40761df 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -1,7 +1,7 @@ using AutomobilePlantBusinessLogic.BusinessLogics; using AutomobilePlantContracts.BusinessLogicContracts; using AutomobilePlantContracts.StorageContracts; -using AutomobilePlantListImplements.Implements; +using AutomobilePlantFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; @@ -48,6 +48,7 @@ namespace AutomobilePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs index f351853..55094d6 100644 --- a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using AutomobilePlantContracts.SearchModels; using AutomobilePlantContracts.StorageContracts; using AutomobilePlantContracts.ViewModels; using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -17,11 +18,17 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly ICarStorage _carStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, ICarStorage carStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _carStorage = carStorage; + _shopStorage = shopStorage; } public bool CreateOrder(OrderBindingModel model) @@ -72,6 +79,20 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } + if (newStatus == OrderStatus.Готов) + { + var car = _carStorage.GetElement(new CarSearchModel() { Id = model.CarId }); + if (car == null) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Car not found."); + return false; + } + if (CheckThenSupplyMany(car, model.Count) == false) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + return false; + } + } model.Status = newStatus; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; if (_orderStorage.Update(model) == null) @@ -83,6 +104,67 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics return true; } + public bool CheckThenSupplyMany(ICarModel car, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Car count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountCars; + foreach (var c in shop.ShopCars) + { + freeSpace -= c.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountCars; + foreach (var c in shop.ShopCars) + { + freeSpace -= c.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyCars(new() { Id = shop.Id }, car, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyCars(new() { Id = shop.Id }, car, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + public bool TakeOrderInWork(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Выполняется); diff --git a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ShopLogic.cs b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ShopLogic.cs index 6294354..4cdf6ad 100644 --- a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ShopLogic.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -88,26 +89,39 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - if (shopElement.ShopCars.TryGetValue(car.Id, out var sameCar)) + var countCars = 0; + foreach (var c in shopElement.ShopCars) { - shopElement.ShopCars[car.Id] = (car, sameCar.Item2 + count); - _logger.LogInformation("Same car found by supply. Added {0} of {1} in {2} shop", count, car.CarName, shopElement.Name); + countCars += c.Value.Item2; + } + if (shopElement.MaxCountCars - countCars >= count) + { + if (shopElement.ShopCars.TryGetValue(car.Id, out var sameCar)) + { + shopElement.ShopCars[car.Id] = (car, sameCar.Item2 + count); + _logger.LogInformation("Same car found by supply. Added {0} of {1} in {2} shop", count, car.CarName, shopElement.Name); + } + else + { + shopElement.ShopCars[car.Id] = (car, count); + _logger.LogInformation("New car added by supply. Added {0} of {1} in {2} shop", count, car.CarName, shopElement.Name); + } + _shopStorage.Update(new() + { + Id = shopElement.Id, + Name = shopElement.Name, + Address = shopElement.Address, + OpeningDate = shopElement.OpeningDate, + ShopCars = shopElement.ShopCars, + MaxCountCars = shopElement.MaxCountCars + }); } else { - shopElement.ShopCars[car.Id] = (car, count); - _logger.LogInformation("New car added by supply. Added {0} of {1} in {2} shop", count, car.CarName, shopElement.Name); + _logger.LogWarning("Required shop is overflowed"); + return false; } - _shopStorage.Update(new() - { - Id = shopElement.Id, - Name = shopElement.Name, - Address = shopElement.Address, - OpeningDate = shopElement.OpeningDate, - ShopCars = shopElement.ShopCars - }); - return true; } @@ -124,6 +138,11 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics return true; } + public bool SellCar(ICarModel car, int count) + { + return _shopStorage.SellCar(car, count); + } + public bool Update(ShopBindingModel model) { CheckModel(model); @@ -167,6 +186,11 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics throw new ArgumentNullException("Нет названия магазина!", nameof(model.Name)); } + if (model.MaxCountCars < 0) + { + throw new InvalidOperationException("Магазин с отрицательным количеством максимального количества авто!"); + } + _logger.LogInformation("Shop. Name: {0}, Address: {1}, ID: {2}", model.Name, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel diff --git a/AutomobilePlant/AutomobilePlantContracts/BindingModels/ShopBindingModel.cs b/AutomobilePlant/AutomobilePlantContracts/BindingModels/ShopBindingModel.cs index 0d06140..4b998dd 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BindingModels/ShopBindingModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BindingModels/ShopBindingModel.cs @@ -18,5 +18,6 @@ namespace AutomobilePlantContracts.BindingModels public DateTime OpeningDate { get; set; } public Dictionary ShopCars { get; set; } + public int MaxCountCars { get; set; } } } diff --git a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs index 1f3f790..5c4501a 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IShopLogic.cs @@ -18,5 +18,6 @@ namespace AutomobilePlantContracts.BusinessLogicContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyCars(ShopSearchModel model, ICarModel car, int count); + bool SellCar(ICarModel car, int count); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IShopStorage.cs b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IShopStorage.cs index 86f1fbb..24f5fdd 100644 --- a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IShopStorage.cs +++ b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.SearchModels; using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace AutomobilePlantContracts.StorageContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellCar(ICarModel model, int count); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/ViewModels/ShopViewModel.cs b/AutomobilePlant/AutomobilePlantContracts/ViewModels/ShopViewModel.cs index 9566b0d..b04bff8 100644 --- a/AutomobilePlant/AutomobilePlantContracts/ViewModels/ShopViewModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/ViewModels/ShopViewModel.cs @@ -22,5 +22,8 @@ namespace AutomobilePlantContracts.ViewModels public DateTime OpeningDate { get; set; } public Dictionary ShopCars { get; set; } = new(); + + [DisplayName("Макс. автомобилей в магазине")] + public int MaxCountCars { get; set; } } } diff --git a/AutomobilePlant/AutomobilePlantDataModels/Models/IShopModel.cs b/AutomobilePlant/AutomobilePlantDataModels/Models/IShopModel.cs index 8ee298b..a290ba7 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/Models/IShopModel.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace AutomobilePlantDataModels.Models string Address { get; } DateTime OpeningDate { get; } Dictionary ShopCars { get; } + int MaxCountCars { get; } } } diff --git a/AutomobilePlant/AutomobilePlantFileImplement/AutomobilePlantFileImplement.csproj b/AutomobilePlant/AutomobilePlantFileImplement/AutomobilePlantFileImplement.csproj new file mode 100644 index 0000000..a6820c2 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/AutomobilePlantFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/AutomobilePlant/AutomobilePlantFileImplement/DataFileSingleton.cs b/AutomobilePlant/AutomobilePlantFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..7db8d49 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/DataFileSingleton.cs @@ -0,0 +1,61 @@ +using AutomobilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string CarFileName = "Car.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Cars { get; private set; } + public List Shops { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveCars() => SaveData(Cars, CarFileName, "Cars", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Cars = LoadData(CarFileName, "Car", x => Car.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantFileImplement/Implements/CarStorage.cs new file mode 100644 index 0000000..42b3d4a --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Implements/CarStorage.cs @@ -0,0 +1,88 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement.Implements +{ + public class CarStorage : ICarStorage + { + private readonly DataFileSingleton source; + + public CarStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + return source.Cars + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarName) && x.CarName == model.CarName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName)) + { + return new(); + } + return source.Cars + .Where(x => x.CarName.Contains(model.CarName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Cars.Select(x => x.GetViewModel).ToList(); + } + + public CarViewModel? Insert(CarBindingModel model) + { + model.Id = source.Cars.Count > 0 ? source.Cars.Max(x => x.Id) + 1 : 1; + var newCar = Car.Create(model); + if (newCar == null) + { + return null; + } + source.Cars.Add(newCar); + source.SaveCars(); + return newCar.GetViewModel; + } + + public CarViewModel? Update(CarBindingModel model) + { + var car = source.Cars.FirstOrDefault(x => x.Id == model.Id); + if (car == null) + { + return null; + } + car.Update(model); + source.SaveCars(); + return car.GetViewModel; + } + public CarViewModel? Delete(CarBindingModel model) + { + var car = source.Cars.FirstOrDefault(x => x.Id == model.Id); + if (car == null) + { + return null; + } + source.Cars.Remove(car); + source.SaveCars(); + return car.GetViewModel; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Implements/ComponentStorage.cs b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..fffc665 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,87 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Components.Remove(component); + source.SaveComponents(); + return component.GetViewModel; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Implements/OrderStorage.cs b/AutomobilePlant/AutomobilePlantFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..96f2011 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,98 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + source.Orders.Remove(order); + source.SaveOrders(); + return GetViewModel(order); + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var car = source.Cars.FirstOrDefault(x => x.Id == order.CarId); + if (car != null) + { + viewModel.CarName = car.CarName; + } + return viewModel; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..c5ce93f --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,148 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using AutomobilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(shop => shop.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + + public bool SellCar(ICarModel model, int count) + { + var car = source.Cars.FirstOrDefault(x => x.Id == model.Id); + + if (car == null) + { + return false; + } + + var countStore = count; + + var shopCars = source.Shops.SelectMany(shop => shop.ShopCars.Where(c => c.Value.Item1.Id == car.Id)); + + foreach (var c in shopCars) + { + count -= c.Value.Item2; + + if (count <= 0) + { + break; + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + foreach (var shop in source.Shops) + { + var cars = shop.ShopCars; + + foreach (var c in cars.Where(x => x.Value.Item1.Id == car.Id)) + { + var min = Math.Min(c.Value.Item2, count); + cars[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Address = shop.Address, + OpeningDate = shop.OpeningDate, + MaxCountCars = shop.MaxCountCars, + ShopCars = cars + }); + + source.SaveShops(); + + if (count <= 0) break; + } + + return count <= 0; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Models/Car.cs b/AutomobilePlant/AutomobilePlantFileImplement/Models/Car.cs new file mode 100644 index 0000000..47c09a5 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Models/Car.cs @@ -0,0 +1,106 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement.Models +{ + public class Car : ICarModel + { + public string CarName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public int Id { get; private set; } + + public Dictionary Components { get; private set; } = new(); + + private Dictionary? _carComponents = null; + public Dictionary CarComponents + { + get + { + if (_carComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _carComponents = Components.ToDictionary( + x => x.Key, + y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value) + ); + } + return _carComponents; + } + } + + + public static Car? Create(CarBindingModel? model) + { + if (model == null) + { + return null; + } + return new Car() + { + Id = model.Id, + CarName = model.CarName, + Price = model.Price, + Components = model.CarComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Car? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Car() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CarName = element.Element("CarName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("CarComponents")!.Elements("CarComponent").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + + public void Update(CarBindingModel? model) + { + if (model == null) + { + return; + } + CarName = model.CarName; + Price = model.Price; + Components = model.CarComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _carComponents = null; + } + + public CarViewModel GetViewModel => new() + { + Id = Id, + CarName = CarName, + Price = Price, + CarComponents = CarComponents + }; + + public XElement GetXElement => new( + "Car", + new XAttribute("Id", Id), + new XElement("CarName", CarName), + new XElement("Price", Price.ToString()), + new XElement("CarComponents", Components.Select(x => + new XElement("CarComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Models/Component.cs b/AutomobilePlant/AutomobilePlantFileImplement/Models/Component.cs new file mode 100644 index 0000000..84604fc --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Models/Component.cs @@ -0,0 +1,71 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement.Models +{ + public class Component : IComponentModel + { + public string ComponentName { get; private set; } = String.Empty; + public double Cost { get; set; } + public int Id { get; private set; } + + public static Component? Create(ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + + public void Update(ComponentBindingModel? model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + + public XElement GetXElement => new( + "Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString()) + ); + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Models/Order.cs b/AutomobilePlant/AutomobilePlantFileImplement/Models/Order.cs new file mode 100644 index 0000000..0c3bd01 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Models/Order.cs @@ -0,0 +1,98 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement.Models +{ + public class Order : IOrderModel + { + public int CarId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + CarId = model.CarId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CarId = Convert.ToInt32(element.Element("CarId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + CarId = CarId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("CarId", CarId.ToString()), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} diff --git a/AutomobilePlant/AutomobilePlantFileImplement/Models/Shop.cs b/AutomobilePlant/AutomobilePlantFileImplement/Models/Shop.cs new file mode 100644 index 0000000..a8d84c9 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantFileImplement/Models/Shop.cs @@ -0,0 +1,122 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AutomobilePlantFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public int MaxCountCars { get; private set; } + + public DateTime OpeningDate { get; private set; } + + public Dictionary Cars { get; private set; } = new(); + + private Dictionary? _shopCars = null; + + public Dictionary ShopCars + { + get + { + if (_shopCars == null) + { + var source = DataFileSingleton.GetInstance(); + _shopCars = Cars.ToDictionary( + x => x.Key, + y => ((source.Cars.FirstOrDefault(z => z.Id == y.Key) as ICarModel)!, y.Value) + ); + } + return _shopCars; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + MaxCountCars = model.MaxCountCars, + OpeningDate = model.OpeningDate, + Cars = model.ShopCars.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Address = element.Element("Address")!.Value, + MaxCountCars = Convert.ToInt32(element.Element("MaxCountCars")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Cars = element.Element("ShopCars")!.Elements("ShopCar").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + OpeningDate = model.OpeningDate; + MaxCountCars = model.MaxCountCars; + if (model.ShopCars.Count > 0) + { + Cars = model.ShopCars.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopCars = null; + } + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + OpeningDate = OpeningDate, + ShopCars = ShopCars, + MaxCountCars = MaxCountCars + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Address", Address), + new XElement("MaxCountCars", MaxCountCars), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopCars", Cars.Select(x => + new XElement("ShopCar", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} diff --git a/AutomobilePlant/AutomobilePlantListImplements/Implements/ShopStorage.cs b/AutomobilePlant/AutomobilePlantListImplements/Implements/ShopStorage.cs index f295093..e3e72a1 100644 --- a/AutomobilePlant/AutomobilePlantListImplements/Implements/ShopStorage.cs +++ b/AutomobilePlant/AutomobilePlantListImplements/Implements/ShopStorage.cs @@ -3,6 +3,7 @@ using AutomobilePlantContracts.SearchModels; using AutomobilePlantContracts.StorageContracts; using AutomobilePlantContracts.ViewModels; using AutomobilePlantListImplements.Models; +using AutomobilePlantDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -122,5 +123,10 @@ namespace AutomobilePlantListImplements.Implements return null; } + + public bool SellCar(ICarModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/AutomobilePlant/AutomobilePlantListImplements/Models/Shop.cs b/AutomobilePlant/AutomobilePlantListImplements/Models/Shop.cs index 6e39fdb..253bf2d 100644 --- a/AutomobilePlant/AutomobilePlantListImplements/Models/Shop.cs +++ b/AutomobilePlant/AutomobilePlantListImplements/Models/Shop.cs @@ -19,6 +19,8 @@ namespace AutomobilePlantListImplements.Models public DateTime OpeningDate { get; private set; } + public int MaxCountCars { get; private set; } + public Dictionary ShopCars { get; private set; } = new(); public static Shop? Create(ShopBindingModel model) @@ -34,6 +36,7 @@ namespace AutomobilePlantListImplements.Models Name = model.Name, Address = model.Address, OpeningDate = model.OpeningDate, + MaxCountCars = model.MaxCountCars, ShopCars = new() }; } @@ -49,6 +52,7 @@ namespace AutomobilePlantListImplements.Models Address = model.Address; OpeningDate = model.OpeningDate; ShopCars = model.ShopCars; + MaxCountCars= model.MaxCountCars; } public ShopViewModel GetViewModel => new() @@ -57,7 +61,8 @@ namespace AutomobilePlantListImplements.Models Name = Name, Address = Address, OpeningDate = OpeningDate, - ShopCars = ShopCars + ShopCars = ShopCars, + MaxCountCars = MaxCountCars }; } }