From a413897992e790af978e82d16bfb232dc344c602 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Wed, 14 Feb 2024 13:47:06 +0400 Subject: [PATCH] save --- .../BlacksmithWorkshop/FormShop.Designer.cs | 159 ++++++++++++++++++ .../BlacksmithWorkshop/FormShop.cs | 122 ++++++++++++++ .../BlacksmithWorkshop/FormShop.resx | 132 +++++++++++++++ .../BusinessLogics/ShopLogic.cs | 152 +++++++++++++++++ .../BindingModels/ShopBindingModel.cs | 18 ++ .../BusinessLogicsContracts/IShopLogic.cs | 22 +++ .../SearchModels/ShopSearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 21 +++ .../ViewModels/ShopViewModel.cs | 22 +++ .../Models/IShopModel.cs | 16 ++ .../DataListSingleton.cs | 2 + .../Implements/ShopStorage.cs | 107 ++++++++++++ .../Models/Shop.cs | 54 ++++++ 13 files changed, 841 insertions(+) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs new file mode 100644 index 0000000..e9c3004 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs @@ -0,0 +1,159 @@ +namespace BlacksmithWorkshop +{ + partial class FormShop + { + /// + /// 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() + { + DataGridView = new DataGridView(); + dateTimePicker1 = new DateTimePicker(); + labelName = new Label(); + label2 = new Label(); + labelDate = new Label(); + textBox1 = new TextBox(); + textBox2 = new TextBox(); + ColumnId = new DataGridViewTextBoxColumn(); + ColumnName = new DataGridViewTextBoxColumn(); + ColumnPrice = new DataGridViewTextBoxColumn(); + ColumnCount = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnName, ColumnPrice, ColumnCount }); + DataGridView.Location = new Point(12, 101); + DataGridView.Name = "DataGridView"; + DataGridView.RowTemplate.Height = 25; + DataGridView.Size = new Size(479, 281); + DataGridView.TabIndex = 0; + // + // dateTimePicker1 + // + dateTimePicker1.Location = new Point(150, 63); + dateTimePicker1.Name = "dateTimePicker1"; + dateTimePicker1.Size = new Size(166, 23); + dateTimePicker1.TabIndex = 1; + // + // labelName + // + labelName.AutoSize = true; + labelName.Location = new Point(12, 9); + labelName.Name = "labelName"; + labelName.Size = new Size(62, 15); + labelName.TabIndex = 2; + labelName.Text = "Название:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(12, 38); + label2.Name = "label2"; + label2.Size = new Size(43, 15); + label2.TabIndex = 3; + label2.Text = "Адрес:"; + // + // labelDate + // + labelDate.AutoSize = true; + labelDate.Location = new Point(12, 69); + labelDate.Name = "labelDate"; + labelDate.Size = new Size(90, 15); + labelDate.TabIndex = 4; + labelDate.Text = "Дата открытия:"; + // + // textBox1 + // + textBox1.Location = new Point(150, 6); + textBox1.Name = "textBox1"; + textBox1.Size = new Size(166, 23); + textBox1.TabIndex = 5; + // + // textBox2 + // + textBox2.Location = new Point(150, 35); + textBox2.Name = "textBox2"; + textBox2.Size = new Size(166, 23); + textBox2.TabIndex = 6; + // + // ColumnId + // + ColumnId.HeaderText = ""; + ColumnId.Name = "ColumnId"; + ColumnId.Visible = false; + // + // ColumnName + // + ColumnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + ColumnName.HeaderText = "Изделие"; + ColumnName.Name = "ColumnName"; + // + // ColumnPrice + // + ColumnPrice.HeaderText = "Цена"; + ColumnPrice.Name = "ColumnPrice"; + ColumnPrice.Width = 236; + // + // ColumnCount + // + ColumnCount.HeaderText = "Количество"; + ColumnCount.Name = "ColumnCount"; + // + // FormShop + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(textBox2); + Controls.Add(textBox1); + Controls.Add(labelDate); + Controls.Add(label2); + Controls.Add(labelName); + Controls.Add(dateTimePicker1); + Controls.Add(DataGridView); + Name = "FormShop"; + Text = "FormShop"; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private DataGridView DataGridView; + private DateTimePicker dateTimePicker1; + private Label labelName; + private Label label2; + private Label labelDate; + private TextBox textBox1; + private TextBox textBox2; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnName; + private DataGridViewTextBoxColumn ColumnPrice; + private DataGridViewTextBoxColumn ColumnCount; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs new file mode 100644 index 0000000..1cfe0fd --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs @@ -0,0 +1,122 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic.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 BlacksmithWorkshop +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public int? _id; + private Dictionary _manufactures; + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _manufactures = new(); + } + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var shop = _logic.ReadElement(new ShopSearchModel { Id = _id }); + if (shop != null) + { + NameTextBox.Text = shop.ShopName; + AddressTextBox.Text = shop.Address; + DateTimePicker.Text = shop.OpeningDate.ToString(); + _manufactures = shop.ShopManufactures; + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка товаров магазина"); + try + { + if (_manufactures != null) + { + foreach (var manufactures in _manufactures) + { + System.Windows.Forms.DataGridView.Rows.Add(new object[] { manufactures.Key, manufactures.Value.Item1.ManufactureName, + manufactures.Value.Item1.Price, manufactures.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void CancelButton_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(NameTextBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(AddressTextBox.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = NameTextBox.Text, + Address = AddressTextBox.Text, + DateOpen = DateTimePicker.Value.Date, + ShopComputers = _manufactures + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + 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); + } + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx new file mode 100644 index 0000000..b0c7c5d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + True + + + True + + + True + + + True + + \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..02ba054 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,152 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.BusinessLogics +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + public ShopLogic(ILogger logger, IShopStorage ShopStorage) + { + _logger = logger; + _shopStorage = ShopStorage; + } + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}. Id:{ Id}", model?.ShopName, model?.Id); + var list = model == null ? _shopStorage.GetFullList() : + _shopStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + public bool ReplenishManufactures(ShopSearchModel model, IManufactureModel manufacture, int count) + { + _logger.LogInformation("Try to replenish manufactures. ShopName:{ShopName}. Id:{Id}", model.ShopName, model.Id); + if (model == null) + throw new ArgumentNullException(nameof(model)); + ShopViewModel? curModel = ReadElement(model); + if (curModel == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (manufacture == null) + throw new ArgumentNullException(nameof(manufacture)); + if (count <= 0) + throw new ArgumentException("Количество должно быть положительным числом"); + if (curModel.ShopManufactures.TryGetValue(manufacture.Id, out var pair)) + { + curModel.ShopManufactures[manufacture.Id] = (pair.Item1, pair.Item2 + count); + } + else + { + curModel.ShopManufactures.Add(manufacture.Id, (manufacture, count)); + } + Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + OpeningDate = curModel.OpeningDate, + Address = curModel.Address, + ShopManufactures = curModel.ShopManufactures, + }); + _logger.LogInformation("Success. ManufactureName:{ManufactureName}. Id:{Id}. Replenish:{count}", + manufacture.ManufactureName, manufacture.Id, count); + return true; + } + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); + } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адреса магазина", nameof(model.Address)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", + model.ShopName, model.Address, model.OpeningDate, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..79ff366 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlacksmithWorkshopDataModels.Models; + +namespace BlacksmithWorkshopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Address { get; set; } = string.Empty; + public DateTime OpeningDate { get; set; } + public Dictionary ShopManufactures { get; set; } = new(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..5b6358d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool ReplenishManufactures(ShopSearchModel shop, IManufactureModel manufacture, int count); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..64a9f62 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? ShopName { get; set; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..c36afa4 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,21 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.StoragesContracts +{ + public interface IShopStorage + { + List GetFullList(); + List GetFilteredList(ShopSearchModel model); + ShopViewModel? GetElement(ShopSearchModel model); + ShopViewModel? Insert(ShopBindingModel model); + ShopViewModel? Update(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..1a17384 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,22 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название магазина")] + public string ShopName { get; set; } = string.Empty; + [DisplayName("Адрес магазина")] + public string Address { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime OpeningDate { get; set; } + public Dictionary ShopManufactures { get; set; } = new(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..1e742af --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IShopModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Address { get; } + DateTime OpeningDate { get; } + Dictionary ShopManufactures { get; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index 71c00d4..32ab5b6 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace BlacksmithWorkshopListImplement public List Components { get; set; } public List Orders { get; set; } public List Manufactures { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Manufactures = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..eb418a9 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,107 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach(var Shop in _source.Shops) + { + result.Add(Shop.GetViewModel); + } + return result; + } + public List GetFilteredList(ShopSearchModel model) + { + List result = new(); + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + foreach(var Shop in _source.Shops) + { + if (Shop.ShopName.Contains(model.ShopName)) + { + result.Add(Shop.GetViewModel); + } + } + return result; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + foreach(var Shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && + Shop.ShopName == model.ShopName) || + (model.Id.HasValue && Shop.Id == model.Id)) + { + return Shop.GetViewModel; + } + } + return null; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach(var Shop in _source.Shops) + { + if (Shop.Id >= model.Id) + { + model.Id = Shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var Shop in _source.Shops) + { + if (Shop.Id == model.Id) + { + Shop.Update(model); + return Shop.GetViewModel; + } + } + return null; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs new file mode 100644 index 0000000..4c761e4 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Shop.cs @@ -0,0 +1,54 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary ShopManufactures { get; private set; } = new(); + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + OpeningDate = model.OpeningDate, + ShopManufactures = model.ShopManufactures + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + OpeningDate = model.OpeningDate; + ShopManufactures = model.ShopManufactures; + } + public ShopViewModel GetViewModel => new ShopViewModel() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopManufactures = ShopManufactures + }; + } +}