From fe06793f3c9352e463754cf8adafd21a562dd765 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Mon, 24 Apr 2023 21:25:09 +0400 Subject: [PATCH 1/4] Back up and dm --- SushiBar/SushiBar/FormClients.cs | 9 +- SushiBar/SushiBar/FormMain.cs | 21 ++++- SushiBar/SushiBar/Program.cs | 1 + .../BusinessLogics/BackUpLogic.cs | 87 +++++++++++++++++++ .../Attributes/ColumnAttribute.cs | 23 +++++ .../Attributes/DataGridViewExtension.cs | 48 ++++++++++ .../Attributes/GridViewAutoSize.cs | 13 +++ .../BindingModels/BackUpSaveBindingModel.cs | 6 ++ .../BusinessLogicsContracts/IBackUpLogic.cs | 8 ++ .../SushiBarContracts/DI/DependencyManager.cs | 45 ++++++++++ .../DI/IDependencyContainer.cs | 11 +++ .../DI/IImplementationExtension.cs | 7 ++ .../DI/ServiceProviderLoader.cs | 47 ++++++++++ .../StoragesContracts/IBackUpInfo.cs | 7 ++ .../SushiBarContracts.csproj | 10 +++ .../Implements/BackUpInfo.cs | 26 ++++++ 16 files changed, 361 insertions(+), 8 deletions(-) create mode 100644 SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs create mode 100644 SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs create mode 100644 SushiBar/SushiBarContracts/Attributes/DataGridViewExtension.cs create mode 100644 SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs create mode 100644 SushiBar/SushiBarContracts/BindingModels/BackUpSaveBindingModel.cs create mode 100644 SushiBar/SushiBarContracts/BusinessLogicsContracts/IBackUpLogic.cs create mode 100644 SushiBar/SushiBarContracts/DI/DependencyManager.cs create mode 100644 SushiBar/SushiBarContracts/DI/IDependencyContainer.cs create mode 100644 SushiBar/SushiBarContracts/DI/IImplementationExtension.cs create mode 100644 SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs create mode 100644 SushiBar/SushiBarContracts/StoragesContracts/IBackUpInfo.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs diff --git a/SushiBar/SushiBar/FormClients.cs b/SushiBar/SushiBar/FormClients.cs index 795dd16..7b2985c 100644 --- a/SushiBar/SushiBar/FormClients.cs +++ b/SushiBar/SushiBar/FormClients.cs @@ -2,6 +2,7 @@ using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; using System.Windows.Forms; +using SushiBarContracts.Attributes; namespace SushiBar { @@ -21,13 +22,7 @@ namespace SushiBar { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFio"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Load clients"); } catch (Exception ex) diff --git a/SushiBar/SushiBar/FormMain.cs b/SushiBar/SushiBar/FormMain.cs index eadfcba..dfb6b0b 100644 --- a/SushiBar/SushiBar/FormMain.cs +++ b/SushiBar/SushiBar/FormMain.cs @@ -13,17 +13,20 @@ namespace SushiBar private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; private readonly IWorkProcess _workProcess; + private readonly IBackUpLogic _backUpLogic; public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, - IWorkProcess workProcess) + IWorkProcess workProcess, + IBackUpLogic backUpLogic) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportLogic; _workProcess = workProcess; + _backUpLogic = backUpLogic; } private void LoadData() @@ -249,5 +252,21 @@ namespace SushiBar form.ShowDialog(); } } + + private void CreateBackupStripMenuItem_Click(object sender, EventArgs e) + { + try + { + var fbd = new FolderBrowserDialog(); + if (fbd.ShowDialog() != DialogResult.OK) return; + _backUpLogic.CreateBackUp(new BackUpSaveBindingModel { FolderName = fbd.SelectedPath }); + MessageBox.Show("Backup is ready", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } } } diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 9324ad2..0c9549d 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -76,6 +76,7 @@ namespace SushiBar services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddSingleton(); diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..5dff3b3 --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,87 @@ +using System.IO.Compression; +using System.Reflection; +using System.Runtime.Serialization.Json; +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.StoragesContracts; +using SushiBarDataModels; + +namespace SushiBarBusinessLogic.BusinessLogics; + +public class BackUpLogic : IBackUpLogic +{ + private readonly ILogger _logger; + private readonly IBackUpInfo _backUpInfo; + + public BackUpLogic(ILogger logger, IBackUpInfo backUpInfo) + { + _logger = logger; + _backUpInfo = backUpInfo; + } + + public void CreateBackUp(BackUpSaveBindingModel model) + { + _logger.LogDebug("Clear folder"); + var dirInfo = new DirectoryInfo(model.FolderName); + + if (dirInfo.Exists) + { + foreach (var file in dirInfo.GetFiles()) + { + file.Delete(); + } + } + + _logger.LogDebug("Delete archive"); + var fileName = $"{model.FolderName}.zip"; + + if (File.Exists(fileName)) File.Delete(fileName); + + _logger.LogDebug("Get assembly"); + var typeIId = typeof(IId); + var assembly = typeIId.Assembly; + + if (assembly == null) + { + throw new ArgumentNullException("Not found", nameof(assembly)); + } + + var types = assembly.GetTypes(); + var method = GetType().GetMethod("SaveToFile", BindingFlags.NonPublic | BindingFlags.Instance); + _logger.LogDebug("Find {count} types", types.Length); + + foreach (var type in types) + { + if (!type.IsInterface || type.GetInterface(typeIId.Name) == null) continue; + var modelType = _backUpInfo.GetTypeByModelInterface(type.Name); + + if (modelType == null) + { + throw new InvalidOperationException($"Not fount model {type.Name}"); + } + + _logger.LogDebug("Call SaveToFile method for {name} type", type.Name); + method?.MakeGenericMethod(modelType).Invoke(this, new object[] { model.FolderName }); + } + + _logger.LogDebug("Create zip and remove folder"); + ZipFile.CreateFromDirectory(model.FolderName, fileName); + dirInfo.Delete(true); + } + + private void SaveToFile(string folderName) where T : class, new() + { + var records = _backUpInfo.GetList(); + + if (records == null) + { + _logger.LogWarning("{type} type get null list", typeof(T).Name); + return; + } + + var jsonFormatter = new DataContractJsonSerializer(typeof(List)); + using var fs = new FileStream($"{folderName}/{typeof(T).Name}.json", FileMode.OpenOrCreate); + jsonFormatter.WriteObject(fs, records); + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs b/SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..1ca5a50 --- /dev/null +++ b/SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,23 @@ +namespace SushiBarContracts.Attributes; + +[AttributeUsage(AttributeTargets.Property)] +public class ColumnAttribute : Attribute +{ + public ColumnAttribute(string title = "", + bool visible = true, + int width = 0, + GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, + bool isUseAutoSize = false) + { + Title = title; + Visible = visible; + Width = width; + GridViewAutoSize = gridViewAutoSize; + IsUseAutoSize = isUseAutoSize; + } + public string Title { get; private set; } + public bool Visible { get; private set; } + public int Width { get; private set; } + public GridViewAutoSize GridViewAutoSize { get; private set; } + public bool IsUseAutoSize { get; private set; } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/Attributes/DataGridViewExtension.cs b/SushiBar/SushiBarContracts/Attributes/DataGridViewExtension.cs new file mode 100644 index 0000000..5f14d80 --- /dev/null +++ b/SushiBar/SushiBarContracts/Attributes/DataGridViewExtension.cs @@ -0,0 +1,48 @@ +using System.Windows.Forms; + +namespace SushiBarContracts.Attributes; + +public static class DataGridViewExtension +{ + public static void FillAndConfigGrid(this DataGridView grid, List? data) + { + if (data == null) + { + return; + } + grid.DataSource = data; + var type = typeof(T); + var properties = type.GetProperties(); + foreach (DataGridViewColumn column in grid.Columns) + { + var property = properties.FirstOrDefault(x => x.Name == column.Name); + if (property == null) + { + throw new InvalidOperationException($"In type {type.Name} not found references with {column.Name}"); + } + var attribute = property.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault(); + switch (attribute) + { + case null: + throw new InvalidOperationException($"Not found attribute ColumnAttribute to property {property.Name}"); + case ColumnAttribute columnAttr: + { + column.HeaderText = columnAttr.Title; + column.Visible = columnAttr.Visible; + if (columnAttr.IsUseAutoSize) + { + column.AutoSizeMode = + (DataGridViewAutoSizeColumnMode)Enum.Parse(typeof(DataGridViewAutoSizeColumnMode), + columnAttr.GridViewAutoSize.ToString()); + } + else + { + column.Width = columnAttr.Width; + } + + break; + } + } + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs b/SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..79c4ad8 --- /dev/null +++ b/SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,13 @@ +namespace SushiBarContracts.Attributes; + +public enum GridViewAutoSize +{ + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/BindingModels/BackUpSaveBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/BackUpSaveBindingModel.cs new file mode 100644 index 0000000..b345f35 --- /dev/null +++ b/SushiBar/SushiBarContracts/BindingModels/BackUpSaveBindingModel.cs @@ -0,0 +1,6 @@ +namespace SushiBarContracts.BindingModels; + +public class BackUpSaveBindingModel +{ + public string FolderName { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/BusinessLogicsContracts/IBackUpLogic.cs b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..2ad9f74 --- /dev/null +++ b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -0,0 +1,8 @@ +using SushiBarContracts.BindingModels; + +namespace SushiBarContracts.BusinessLogicsContracts; + +public interface IBackUpLogic +{ + void CreateBackUp(BackUpSaveBindingModel model); +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/DependencyManager.cs b/SushiBar/SushiBarContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..9c21ae0 --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/DependencyManager.cs @@ -0,0 +1,45 @@ +using Microsoft.Extensions.Logging; + +namespace SushiBarContracts.DI; + +public class DependencyManager +{ + private readonly IDependencyContainer _dependencyManager; + private static DependencyManager? _manager; + private static readonly object _locjObject = new(); + + private DependencyManager() + { + _dependencyManager = new ServiceDependencyContainer(); + } + + public static DependencyManager Instance { get { + if (_manager != null) + return + _manager; + lock (_locjObject) { _manager = new DependencyManager(); } + return + _manager; } } + + public static void InitDependency() + { + var ext = ServiceProviderLoader.GetImplementationExtensions(); + if (ext == null) + { + throw new ArgumentNullException("Missing components to load module dependencies"); + } + ext.RegisterServices(); + } + + public void AddLogging(Action configure) => + _dependencyManager.AddLogging(configure); + + public void RegisterType(bool isSingle = false) where TU : class, T where T : class => + _dependencyManager.RegisterType(isSingle); + + public void RegisterType(bool isSingle = false) where T : class => + _dependencyManager.RegisterType(isSingle); + + public T Resolve() => + _dependencyManager.Resolve(); +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/IDependencyContainer.cs b/SushiBar/SushiBarContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..391b45c --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/IDependencyContainer.cs @@ -0,0 +1,11 @@ +using Microsoft.Extensions.Logging; + +namespace SushiBarContracts.DI; + +public interface IDependencyContainer +{ + void AddLogging(Action configure); + void RegisterType(bool isSingle) where TU : class, T where T : class; + void RegisterType(bool isSingle) where T : class; + T Resolve(); +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/IImplementationExtension.cs b/SushiBar/SushiBarContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..305d478 --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/IImplementationExtension.cs @@ -0,0 +1,7 @@ +namespace SushiBarContracts.DI; + +public interface IImplementationExtension +{ + public int Priority { get; } + public void RegisterServices(); +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs b/SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..505a0d9 --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,47 @@ +using System.Reflection; + +namespace SushiBarContracts.DI; + +public static partial class ServiceProviderLoader +{ + public static IImplementationExtension? GetImplementationExtensions() + { + IImplementationExtension? source = null; + var files = + Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", + SearchOption.AllDirectories); + foreach (var file in files.Distinct()) + { + var asm = Assembly.LoadFrom(file); + foreach (var t in asm.GetExportedTypes()) + { + if (!t.IsClass || !typeof(IImplementationExtension).IsAssignableFrom(t)) continue; + if (source == null) + { + source = (IImplementationExtension)Activator.CreateInstance(t)!; + } + else + { + var newSource = (IImplementationExtension)Activator.CreateInstance(t)!; + if (newSource.Priority > source.Priority) + { + source = newSource; + } + } + } + } + return source; + } + private static string TryGetImplementationExtensionsFolder() + { + var directory = new + DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory != null && directory + .GetDirectories("ImplementationExtensions", SearchOption.AllDirectories) + .All(x => x.Name != "ImplementationExtensions")) + { + directory = directory.Parent; + } + return $"{directory?.FullName}\\ImplementationExtensions"; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/StoragesContracts/IBackUpInfo.cs b/SushiBar/SushiBarContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..265e98d --- /dev/null +++ b/SushiBar/SushiBarContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,7 @@ +namespace SushiBarContracts.StoragesContracts; + +public interface IBackUpInfo +{ + List? GetList() where T : class, new(); + Type? GetTypeByModelInterface(string modelInterfaceName); +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/SushiBarContracts.csproj b/SushiBar/SushiBarContracts/SushiBarContracts.csproj index dcc6717..a09a3ba 100644 --- a/SushiBar/SushiBarContracts/SushiBarContracts.csproj +++ b/SushiBar/SushiBarContracts/SushiBarContracts.csproj @@ -10,4 +10,14 @@ + + + ..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.8\System.Windows.Forms.dll + + + + + + + diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs b/SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..400e531 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,26 @@ +using SushiBarContracts.StoragesContracts; + +namespace SushiBarDatabaseImplement.Implements; + +public class BackUpInfo : IBackUpInfo +{ + public List? GetList() where T: class, new() + { + using var context = new SushiBarDatabase(); + return context.Set().ToList(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + var assembly = typeof(BackUpInfo).Assembly; + var types = assembly.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && type.GetInterface(modelInterfaceName) != null) + { + return type; + } + } + return null; + } +} \ No newline at end of file -- 2.25.1 From 8fdc5bc1ccecc508ab77be31275aa820461e0336 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Thu, 4 May 2023 16:10:15 +0400 Subject: [PATCH 2/4] one step is required --- SushiBar/SushiBar/FormComponents.cs | 64 +++--- SushiBar/SushiBar/FormImplementers.cs | 26 +-- SushiBar/SushiBar/FormMain.cs | 188 +++++++----------- SushiBar/SushiBar/FormSushi.cs | 17 +- SushiBar/SushiBar/FormSushiMoreThenOne.cs | 5 +- SushiBar/SushiBar/Program.cs | 91 ++++----- .../BindingModels/MessageInfoBindingModel.cs | 1 + .../DI/ServiceDependencyContainer.cs | 56 ++++++ .../DI/UnityDependencyContainer.cs | 37 ++++ .../SushiBarContracts.csproj | 2 + .../ViewModels/ClientViewModel.cs | 9 +- .../ViewModels/ComponentViewModel.cs | 9 +- .../ViewModels/ImplementerViewModel.cs | 13 +- .../ViewModels/MessageInfoViewModel.cs | 12 +- .../ViewModels/OrderViewModel.cs | 23 ++- .../ViewModels/ReportOrdersViewModel.cs | 4 +- .../ViewModels/SushiViewModel.cs | 7 +- .../Models/Message.cs | 2 + .../SushiBarFileImplement/Models/Message.cs | 2 + .../Models/IMessageInfoModel.cs | 2 +- .../Implements/BackUpInfo.cs | 17 ++ .../Implements/ClientStorage.cs | 39 ++++ .../ListImplementationExtension.cs | 20 ++ .../SushibarListImplement/Models/Message.cs | 2 + .../SushibarListImplement.csproj | 4 + 25 files changed, 389 insertions(+), 263 deletions(-) create mode 100644 SushiBar/SushiBarContracts/DI/ServiceDependencyContainer.cs create mode 100644 SushiBar/SushiBarContracts/DI/UnityDependencyContainer.cs create mode 100644 SushiBar/SushibarListImplement/Implements/BackUpInfo.cs create mode 100644 SushiBar/SushibarListImplement/Implements/ClientStorage.cs create mode 100644 SushiBar/SushibarListImplement/ListImplementationExtension.cs diff --git a/SushiBar/SushiBar/FormComponents.cs b/SushiBar/SushiBar/FormComponents.cs index c8543a9..aca4041 100644 --- a/SushiBar/SushiBar/FormComponents.cs +++ b/SushiBar/SushiBar/FormComponents.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.DI; namespace SushiBar { @@ -18,57 +19,48 @@ namespace SushiBar private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent form) + var service = DependencyManager.Instance.Resolve(); + if (service is not { }) return; + if (service.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } private void ButtonEdit_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + var service = DependencyManager.Instance.Resolve(); + if (service is not { }) return; + service.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (service.ShowDialog() == DialogResult.OK) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent form) - { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } + LoadData(); } } private void ButtonRemove_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != + DialogResult.Yes) return; + var id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Delete component"); + try { - if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (!_logic.Delete(new ComponentBindingModel + { + Id = id + })) { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Delete component"); - try - { - if (!_logic.Delete(new ComponentBindingModel - { - Id = id - })) - { - throw new Exception("Error on deleting. Additional info below."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error delete component"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + throw new Exception("Error on deleting. Additional info below."); } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete component"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/SushiBar/SushiBar/FormImplementers.cs b/SushiBar/SushiBar/FormImplementers.cs index 8c3d391..f719b35 100644 --- a/SushiBar/SushiBar/FormImplementers.cs +++ b/SushiBar/SushiBar/FormImplementers.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.DI; namespace SushiBar { @@ -17,29 +18,22 @@ namespace SushiBar private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) + var service = DependencyManager.Instance.Resolve(); + if (service.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } private void ButtonEdit_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + var service = DependencyManager.Instance.Resolve(); + if (service == null) return; + service.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (service.ShowDialog() == DialogResult.OK) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) - { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } + LoadData(); } } diff --git a/SushiBar/SushiBar/FormMain.cs b/SushiBar/SushiBar/FormMain.cs index dfb6b0b..caa4a30 100644 --- a/SushiBar/SushiBar/FormMain.cs +++ b/SushiBar/SushiBar/FormMain.cs @@ -1,7 +1,9 @@ using Microsoft.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogics; +using SushiBarContracts.Attributes; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.DI; using SushiBarDataModels.Enums; namespace SushiBar @@ -32,33 +34,14 @@ namespace SushiBar private void LoadData() { _logger.LogInformation("Load components"); - try - { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["SushiId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - dataGridView.Columns["ClientFio"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Error on load orders"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); } private void ButtonCreateOrder_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); - if (service is FormCreateOrder form) - { - form.ShowDialog(); - LoadData(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); + LoadData(); } private void ButtonSubmit_Click(object sender, EventArgs e) @@ -96,67 +79,63 @@ namespace SushiBar private void ButtonReady_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + var id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Order №{id}. Change status on -Ready",id); + try { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Order №{id}. Change status on -Ready",id); - try + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { + Id = id, + SushiId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["SushiId"].Value), + SushiName = dataGridView.SelectedRows[0].Cells["SushiName"].Value.ToString() ?? "", + ClientFio = dataGridView.SelectedRows[0].Cells["ClientFio"].Value.ToString() ?? "", + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString() ?? "Unknown"), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString() ?? "0"), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString() ?? ""), + }); + if (!operationResult) { - var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { - Id = id, - SushiId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["SushiId"].Value), - SushiName = dataGridView.SelectedRows[0].Cells["SushiName"].Value.ToString() ?? "", - ClientFio = dataGridView.SelectedRows[0].Cells["ClientFio"].Value.ToString() ?? "", - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString() ?? "Unknown"), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString() ?? "0"), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString() ?? ""), - }); - if (!operationResult) - { - throw new Exception("Error on saving. Additional info below."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error on change status"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + throw new Exception("Error on saving. Additional info below."); } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error on change status"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonIssue_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + var id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Order №{id}. Change status on -Issued", id); + try { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Order №{id}. Change status on -Issued", id); - try + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { + Id = id, + SushiId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["SushiId"].Value), + SushiName = dataGridView.SelectedRows[0].Cells["SushiName"].Value.ToString(), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + ClientFio = dataGridView.SelectedRows[0].Cells["ClientFio"].Value.ToString(), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + }); + if (!operationResult) { - var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { - Id = id, - SushiId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["SushiId"].Value), - SushiName = dataGridView.SelectedRows[0].Cells["SushiName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - ClientFio = dataGridView.SelectedRows[0].Cells["ClientFio"].Value.ToString(), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), - }); - if (!operationResult) - { - throw new Exception("Error on saving. Additional info below."); - } - _logger.LogInformation("Order №{id} issued", id); - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error on change status"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + throw new Exception("Error on saving. Additional info below."); } + _logger.LogInformation("Order №{id} issued", id); + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error on change status"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -172,85 +151,61 @@ namespace SushiBar private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); - if (service is FormComponents form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void SushiToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiMoreThenOne)); - - if (service is FormSushiMoreThenOne form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void ListComponentsToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; - if (dialog.ShowDialog() == DialogResult.OK) + if (dialog.ShowDialog() != DialogResult.OK) return; + _reportLogic.SaveSushiToWordFile(new ReportBindingModel { - _reportLogic.SaveSushiToWordFile(new ReportBindingModel - { - FileName = dialog.FileName - }); - MessageBox.Show("Complete", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); - } + FileName = dialog.FileName + }); + MessageBox.Show("Complete", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void ComponentsOnSushiToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiOnComponents)); - if (service is FormSushiOnComponents form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void ListOrdersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders)); - if (service is FormReportOrders form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void ClientsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormClients)); - if (service is FormClients form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void StartWorkToolStripMenuItem_Click(object sender, EventArgs e) { - _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); + _workProcess.DoWork(DependencyManager.Instance.Resolve(), _orderLogic); MessageBox.Show("Process work is started", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void ImplementersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementers)); - if (service is FormImplementers form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void MailsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormMails)); - if (service is FormMails form) - { - form.ShowDialog(); - } + var service = DependencyManager.Instance.Resolve(); + service.ShowDialog(); } private void CreateBackupStripMenuItem_Click(object sender, EventArgs e) @@ -266,7 +221,6 @@ namespace SushiBar { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - } } } diff --git a/SushiBar/SushiBar/FormSushi.cs b/SushiBar/SushiBar/FormSushi.cs index 8c6f484..2c8df97 100644 --- a/SushiBar/SushiBar/FormSushi.cs +++ b/SushiBar/SushiBar/FormSushi.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.DI; using SushiBarContracts.SearchModels; using SushiBarDataModels.Models; @@ -27,15 +28,13 @@ namespace SushiBar _logger.LogInformation("Load components"); try { - if (_sushiComponents != null) + if (_sushiComponents == null) return; + dataGridView.Rows.Clear(); + foreach (var pc in _sushiComponents) { - dataGridView.Rows.Clear(); - foreach (var pc in _sushiComponents) - { - dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); - } - textBoxPrice.Text = CalcPrice().ToString(); + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); } + textBoxPrice.Text = CalcPrice().ToString(); } catch (Exception ex) { @@ -82,7 +81,7 @@ namespace SushiBar private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormSushiComponent form) { if (form.ShowDialog() == DialogResult.OK) @@ -109,7 +108,7 @@ namespace SushiBar { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormSushiComponent form) { int id = diff --git a/SushiBar/SushiBar/FormSushiMoreThenOne.cs b/SushiBar/SushiBar/FormSushiMoreThenOne.cs index c7404ec..9455401 100644 --- a/SushiBar/SushiBar/FormSushiMoreThenOne.cs +++ b/SushiBar/SushiBar/FormSushiMoreThenOne.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.DI; namespace SushiBar { @@ -38,7 +39,7 @@ namespace SushiBar private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); + var service = DependencyManager.Instance.Resolve(); if (service is FormSushi form) { if (form.ShowDialog() == DialogResult.OK) @@ -52,7 +53,7 @@ namespace SushiBar { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); + var service = DependencyManager.Instance.Resolve(); if (service is FormSushi form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 0c9549d..71e0c4c 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -1,4 +1,3 @@ -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogics; @@ -7,26 +6,20 @@ using SushiBarBusinessLogic.OfficePackage; using SushiBarBusinessLogic.OfficePackage.Implements; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; -using SushiBarContracts.StoragesContracts; -using SushiBarDatabaseImplement.Implements; +using SushiBarContracts.DI; namespace SushiBar { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; - [STAThread] static void Main() { ApplicationConfiguration.Initialize(); - var services = new ServiceCollection(); - ConfigureServices(services); - _serviceProvider = services.BuildServiceProvider(); + InitDependency(); try { - var mailSender = _serviceProvider.GetService(); + var mailSender = DependencyManager.Instance.Resolve(); mailSender?.MailConfig(new MailConfigBindingModel { MailLogin = @@ -49,56 +42,54 @@ namespace SushiBar } catch(Exception ex) { - var logger = _serviceProvider.GetService(); + var logger = DependencyManager.Instance.Resolve(); logger?.LogError(ex, "Error on working w/ email"); } - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(DependencyManager.Instance.Resolve()); } - private static void ConfigureServices(IServiceCollection services) + + private static void InitDependency() { - services.AddLogging(option => + DependencyManager.InitDependency(); + + DependencyManager.Instance.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - - services.AddTransient(); - services.AddSingleton(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + + DependencyManager.Instance.RegisterType(true); + + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); } - private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + private static void MailCheck(object obj) => + DependencyManager.Instance.Resolve()?.MailCheck(); } } \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/BindingModels/MessageInfoBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/MessageInfoBindingModel.cs index 8ceba17..bddb67d 100644 --- a/SushiBar/SushiBarContracts/BindingModels/MessageInfoBindingModel.cs +++ b/SushiBar/SushiBarContracts/BindingModels/MessageInfoBindingModel.cs @@ -10,4 +10,5 @@ public class MessageInfoBindingModel : IMessageInfoModel public DateTime DateDelivery { get; set; } public string Subject { get; set; } = string.Empty; public string Body { get; set; } = string.Empty; + public int Id { get; } } \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/ServiceDependencyContainer.cs b/SushiBar/SushiBarContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..adcd28f --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/ServiceDependencyContainer.cs @@ -0,0 +1,56 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace SushiBarContracts.DI; + +public class ServiceDependencyContainer : IDependencyContainer +{ + private ServiceProvider? _serviceProvider; + + private readonly ServiceCollection _serviceCollection; + + public ServiceDependencyContainer() + { + _serviceCollection = new ServiceCollection(); + } + + public void AddLogging(Action configure) + { + _serviceCollection.AddLogging(configure); + } + + public void RegisterType(bool isSingle) where U : class, T where T : class + { + if (isSingle) + { + _serviceCollection.AddSingleton(); + } + else + { + _serviceCollection.AddTransient(); + } + _serviceProvider = null; + } + + public void RegisterType(bool isSingle) where T : class + { + if (isSingle) + { + _serviceCollection.AddSingleton(); + } + else + { + _serviceCollection.AddTransient(); + } + _serviceProvider = null; + } + + public T Resolve() + { + if (_serviceProvider == null) + { + _serviceProvider = _serviceCollection.BuildServiceProvider(); + } + return _serviceProvider.GetService()!; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/DI/UnityDependencyContainer.cs b/SushiBar/SushiBarContracts/DI/UnityDependencyContainer.cs new file mode 100644 index 0000000..7ea0672 --- /dev/null +++ b/SushiBar/SushiBarContracts/DI/UnityDependencyContainer.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Logging; +using Unity; +using Unity.Microsoft.Logging; + +namespace SushiBarContracts.DI; + +public class UnityDependencyContainer : IDependencyContainer +{ + private readonly IUnityContainer _container; + + public UnityDependencyContainer() + { + _container = new UnityContainer(); + } + + public void AddLogging(Action configure) + { + var factory = LoggerFactory.Create(configure); + _container.AddExtension(new LoggingExtension(factory)); + } + + public void RegisterType(bool isSingle) where T : class + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + + } + + public T Resolve() + { + return _container.Resolve(); + } + + void IDependencyContainer.RegisterType(bool isSingle) + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/SushiBarContracts.csproj b/SushiBar/SushiBarContracts/SushiBarContracts.csproj index a09a3ba..eae6d6e 100644 --- a/SushiBar/SushiBarContracts/SushiBarContracts.csproj +++ b/SushiBar/SushiBarContracts/SushiBarContracts.csproj @@ -18,6 +18,8 @@ + + diff --git a/SushiBar/SushiBarContracts/ViewModels/ClientViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/ClientViewModel.cs index cb9e6ea..5c5cc69 100644 --- a/SushiBar/SushiBarContracts/ViewModels/ClientViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/ClientViewModel.cs @@ -1,15 +1,16 @@ -using System.ComponentModel; +using SushiBarContracts.Attributes; using SushiBarDataModels.Models; namespace SushiBarContracts.ViewModels; public class ClientViewModel : IClientModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("FIO client")] + [Column("FIO client", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ClientFio { get; set; } = string.Empty; - [DisplayName("Email")] + [Column("Email", width: 150)] public string Email { get; set; } = string.Empty; - [DisplayName("Password")] + [Column("Password", width : 150)] public string Password { get; set; } = string.Empty; } \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/ViewModels/ComponentViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/ComponentViewModel.cs index 4651811..4b97377 100644 --- a/SushiBar/SushiBarContracts/ViewModels/ComponentViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/ComponentViewModel.cs @@ -1,16 +1,15 @@ using SushiBarDataModels.Models; -using System.ComponentModel; +using SushiBarContracts.Attributes; namespace SushiBarContracts.ViewModels { public class ComponentViewModel : IComponentModel { + [Column(visible: false)] public int Id { get; set; } - - [DisplayName("Name of Component")] + [Column("Name of Component", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ComponentName { get; set; } = string.Empty; - - [DisplayName("Cost")] + [Column("Cost", width: 80)] public double Cost { get; set; } } diff --git a/SushiBar/SushiBarContracts/ViewModels/ImplementerViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/ImplementerViewModel.cs index 20c5a96..670b159 100644 --- a/SushiBar/SushiBarContracts/ViewModels/ImplementerViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/ImplementerViewModel.cs @@ -1,20 +1,19 @@ using System.ComponentModel; +using SushiBarContracts.Attributes; using SushiBarDataModels.Models; namespace SushiBarContracts.ViewModels; public class ImplementerViewModel : IImplementerModel { + [Column(visible : false)] public int Id { get; init; } - - [DisplayName("Implementer FIO")] + [Column("Implementer FIO", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ImplementerFio { get; set; } = string.Empty; - + [Column("Password", width: 150)] public string Password { get; set; } = string.Empty; - - [DisplayName("Work Experience")] + [Column("Work Experience", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int WorkExperience { get; set; } - - [DisplayName("Qualification")] + [Column("Qualification", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Qualification { get; set; } } \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/ViewModels/MessageInfoViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/MessageInfoViewModel.cs index 5ec60fb..3f0a765 100644 --- a/SushiBar/SushiBarContracts/ViewModels/MessageInfoViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/MessageInfoViewModel.cs @@ -1,16 +1,22 @@ -using System.ComponentModel; +using SushiBarContracts.Attributes; using SushiBarDataModels.Models; namespace SushiBarContracts.ViewModels; public class MessageInfoViewModel : IMessageInfoModel { + [Column(visible: false)] public string MessageId { get; set; } + [Column(visible: false)] public int? ClientId { get; set; } - [DisplayName("Sender name")] + [Column("Sender name", gridViewAutoSize: GridViewAutoSize.DisplayedCells, isUseAutoSize: true)] public string SenderName { get; set; } - [DisplayName("Date delivery")] + [Column("Date delivery", width: 100)] public DateTime DateDelivery { get; set; } + [Column("Subject", width:150)] public string Subject { get; set; } + [Column("Body", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Body { get; set; } + [Column(visible:false)] + public int Id { get; } } \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs index 5a0d8e7..5e0dab3 100644 --- a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs @@ -1,40 +1,43 @@ using SushiBarDataModels.Enums; using SushiBarDataModels.Models; using System.ComponentModel; +using SushiBarContracts.Attributes; namespace SushiBarContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Number")] + [Column("Number", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Id { get; init; } - + [Column(visible:false)] public int SushiId { get; init; } + [Column(visible:false)] public int ClientId { get; init; } + [Column(visible:false)] public int? ImplementerId { get; set; } - [DisplayName("Client FIO")] + [Column("Client FIO", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ClientFio { get; init; } = string.Empty; - [DisplayName("Implementer FIO")] + [Column("Implementer FIO", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ImplementerFio { get; set; } = string.Empty; - [DisplayName("Name of Product")] + [Column("Name of Product", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public string SushiName { get; init; } = string.Empty; - [DisplayName("Count")] + [Column("Count", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Count { get; set; } - [DisplayName("Sum")] + [Column("Sum", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public double Sum { get; set; } - [DisplayName("Status")] + [Column("Status", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public OrderStatus Status { get; set; } = OrderStatus.Unknown; - [DisplayName("Date Create")] + [Column("Date Create", width:100)] public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Date Implement")] + [Column("Date Implement", width:100)] public DateTime? DateImplement { get; set; } } } diff --git a/SushiBar/SushiBarContracts/ViewModels/ReportOrdersViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/ReportOrdersViewModel.cs index f205c08..f075e8b 100644 --- a/SushiBar/SushiBarContracts/ViewModels/ReportOrdersViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/ReportOrdersViewModel.cs @@ -1,4 +1,6 @@ -namespace SushiBarContracts.ViewModels +using SushiBarContracts.Attributes; + +namespace SushiBarContracts.ViewModels { public class ReportOrdersViewModel { diff --git a/SushiBar/SushiBarContracts/ViewModels/SushiViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/SushiViewModel.cs index 1567b04..49cd319 100644 --- a/SushiBar/SushiBarContracts/ViewModels/SushiViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/SushiViewModel.cs @@ -1,17 +1,20 @@ using SushiBarDataModels.Models; using System.ComponentModel; +using SushiBarContracts.Attributes; namespace SushiBarContracts.ViewModels { public class SushiViewModel : ISushiModel { + [Column(visible:false)] public int Id { get; set; } - [DisplayName("Name of Product")] + [Column("Name of Product", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string SushiName { get; set; } = string.Empty; - [DisplayName("Cost")] + [Column("Cost", width:100)] public double Price { get; set; } + [Column(visible:false)] public Dictionary SushiComponents { get; set; } = new(); } } diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Message.cs b/SushiBar/SushiBarDatabaseImplement/Models/Message.cs index 8f70c2e..08d7dcd 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Message.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Message.cs @@ -47,4 +47,6 @@ public class Message : IMessageInfoModel SenderName = SenderName, DateDelivery = DateDelivery, }; + + public int Id { get; } } \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Models/Message.cs b/SushiBar/SushiBarFileImplement/Models/Message.cs index 3f2314f..18e297d 100644 --- a/SushiBar/SushiBarFileImplement/Models/Message.cs +++ b/SushiBar/SushiBarFileImplement/Models/Message.cs @@ -66,4 +66,6 @@ public class Message : IMessageInfoModel new XAttribute("SenderName", SenderName), new XAttribute("DateDelivery", DateDelivery) ); + + public int Id { get; } } \ No newline at end of file diff --git a/SushiBar/SushiBarModels/Models/IMessageInfoModel.cs b/SushiBar/SushiBarModels/Models/IMessageInfoModel.cs index 360e36d..0eada2c 100644 --- a/SushiBar/SushiBarModels/Models/IMessageInfoModel.cs +++ b/SushiBar/SushiBarModels/Models/IMessageInfoModel.cs @@ -1,6 +1,6 @@ namespace SushiBarDataModels.Models; -public interface IMessageInfoModel +public interface IMessageInfoModel : IId { string MessageId { get; } int? ClientId { get; } diff --git a/SushiBar/SushibarListImplement/Implements/BackUpInfo.cs b/SushiBar/SushibarListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..cf7347a --- /dev/null +++ b/SushiBar/SushibarListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,17 @@ +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.StoragesContracts; + +namespace SushibarListImplement.Implements; + +public class BackUpInfo : IBackUpInfo +{ + public List? GetList() where T: class, new() + { + throw new NotImplementedException(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/SushiBar/SushibarListImplement/Implements/ClientStorage.cs b/SushiBar/SushibarListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..eda3729 --- /dev/null +++ b/SushiBar/SushibarListImplement/Implements/ClientStorage.cs @@ -0,0 +1,39 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; + +namespace SushibarListImplement.Implements; + +public class ClientStorage : IClientStorage +{ + public List GetFullList() + { + throw new NotImplementedException(); + } + + public List GetFilteredList(ClientSearchModel? model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? Update(ClientBindingModel model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/SushiBar/SushibarListImplement/ListImplementationExtension.cs b/SushiBar/SushibarListImplement/ListImplementationExtension.cs new file mode 100644 index 0000000..be8c196 --- /dev/null +++ b/SushiBar/SushibarListImplement/ListImplementationExtension.cs @@ -0,0 +1,20 @@ +using SushiBarContracts.DI; +using SushiBarContracts.StoragesContracts; +using SushibarListImplement.Implements; + +namespace SushibarListImplement; + +public class ListImplementationExtension : IImplementationExtension +{ + public int Priority => 0; + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } +} \ No newline at end of file diff --git a/SushiBar/SushibarListImplement/Models/Message.cs b/SushiBar/SushibarListImplement/Models/Message.cs index 2f2ce2d..8897ed0 100644 --- a/SushiBar/SushibarListImplement/Models/Message.cs +++ b/SushiBar/SushibarListImplement/Models/Message.cs @@ -39,4 +39,6 @@ public class Message : IMessageInfoModel ClientId = ClientId, MessageId = MessageId }; + + public int Id { get; } } \ No newline at end of file diff --git a/SushiBar/SushibarListImplement/SushibarListImplement.csproj b/SushiBar/SushibarListImplement/SushibarListImplement.csproj index c8c675f..1c72149 100644 --- a/SushiBar/SushibarListImplement/SushibarListImplement.csproj +++ b/SushiBar/SushibarListImplement/SushibarListImplement.csproj @@ -11,4 +11,8 @@ + + + + -- 2.25.1 From 324abc8c749edaad6491081d067de5cd8d8c46db Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Thu, 4 May 2023 16:29:50 +0400 Subject: [PATCH 3/4] edit Forms --- SushiBar/SushiBar/FormComponents.cs | 9 +-- SushiBar/SushiBar/FormImplementers.cs | 10 +-- SushiBar/SushiBar/FormMails.cs | 10 +-- SushiBar/SushiBar/FormMain.Designer.cs | 8 +++ SushiBar/SushiBar/FormSushi.cs | 2 +- SushiBar/SushiBar/FormSushiComponent.cs | 26 +++----- SushiBar/SushiBar/FormSushiMoreThenOne.cs | 63 +++++++------------ SushiBar/SushiBar/FormSushiOnComponents.cs | 39 +++++------- .../BusinessLogics/OrderLogic.cs | 5 -- .../DatabaseImplementationExtension.cs | 21 +++++++ .../SushiBarDatabaseImplement.csproj | 4 ++ 11 files changed, 89 insertions(+), 108 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/DatabaseImplementationExtension.cs diff --git a/SushiBar/SushiBar/FormComponents.cs b/SushiBar/SushiBar/FormComponents.cs index aca4041..a726c08 100644 --- a/SushiBar/SushiBar/FormComponents.cs +++ b/SushiBar/SushiBar/FormComponents.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using SushiBarContracts.Attributes; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.DI; @@ -78,13 +79,7 @@ namespace SushiBar { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Load components"); } catch (Exception ex) diff --git a/SushiBar/SushiBar/FormImplementers.cs b/SushiBar/SushiBar/FormImplementers.cs index f719b35..38fc41a 100644 --- a/SushiBar/SushiBar/FormImplementers.cs +++ b/SushiBar/SushiBar/FormImplementers.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using SushiBarContracts.Attributes; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.DI; @@ -29,7 +30,6 @@ namespace SushiBar { if (dataGridView.SelectedRows.Count != 1) return; var service = DependencyManager.Instance.Resolve(); - if (service == null) return; service.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (service.ShowDialog() == DialogResult.OK) { @@ -82,13 +82,7 @@ namespace SushiBar { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ImplementerFio"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Load implementers"); } catch (Exception ex) diff --git a/SushiBar/SushiBar/FormMails.cs b/SushiBar/SushiBar/FormMails.cs index 28ec5f7..0fde14e 100644 --- a/SushiBar/SushiBar/FormMails.cs +++ b/SushiBar/SushiBar/FormMails.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using SushiBarContracts.Attributes; using SushiBarContracts.BusinessLogicsContracts; namespace SushiBar @@ -20,14 +21,7 @@ namespace SushiBar { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Load mails"); } catch (Exception ex) diff --git a/SushiBar/SushiBar/FormMain.Designer.cs b/SushiBar/SushiBar/FormMain.Designer.cs index 2b138f9..adda0e8 100644 --- a/SushiBar/SushiBar/FormMain.Designer.cs +++ b/SushiBar/SushiBar/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.componentsOnSushiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.listOrdersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.startWorkToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.createBackUpMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonSubmit = new System.Windows.Forms.Button(); this.buttonReady = new System.Windows.Forms.Button(); @@ -65,6 +66,7 @@ this.directoryToolStripMenuItem, this.reportsToolStripMenuItem, this.startWorkToolStripMenuItem, + this.createBackUpMenuItem, this.mailsToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; @@ -148,6 +150,11 @@ this.startWorkToolStripMenuItem.Size = new System.Drawing.Size(72, 20); this.startWorkToolStripMenuItem.Text = "Start work"; this.startWorkToolStripMenuItem.Click += new System.EventHandler(this.StartWorkToolStripMenuItem_Click); + + this.createBackUpMenuItem.Name = "createBackUpMenuItem"; + this.createBackUpMenuItem.Size = new System.Drawing.Size(72, 20); + this.createBackUpMenuItem.Text = "Create backup"; + this.createBackUpMenuItem.Click += new System.EventHandler(this.CreateBackupStripMenuItem_Click); // // buttonCreateOrder // @@ -248,6 +255,7 @@ private ToolStripMenuItem listOrdersToolStripMenuItem; private ToolStripMenuItem clientsToolStripMenuItem; private ToolStripMenuItem startWorkToolStripMenuItem; + private ToolStripMenuItem createBackUpMenuItem; private ToolStripMenuItem implementersToolStripMenuItem; private ToolStripMenuItem mailsToolStripMenuItem; } diff --git a/SushiBar/SushiBar/FormSushi.cs b/SushiBar/SushiBar/FormSushi.cs index 2c8df97..90f214e 100644 --- a/SushiBar/SushiBar/FormSushi.cs +++ b/SushiBar/SushiBar/FormSushi.cs @@ -183,7 +183,7 @@ namespace SushiBar SushiComponents = _sushiComponents }; var operationResult = _id.HasValue ? _logic.Update(model) : - _logic.Create(model); + _logic.Create(model); if (!operationResult) { throw new Exception("Error on saving. Additional info below."); diff --git a/SushiBar/SushiBar/FormSushiComponent.cs b/SushiBar/SushiBar/FormSushiComponent.cs index be86298..aad4b25 100644 --- a/SushiBar/SushiBar/FormSushiComponent.cs +++ b/SushiBar/SushiBar/FormSushiComponent.cs @@ -10,14 +10,8 @@ namespace SushiBar private readonly List? _list; public int Id { - get - { - return Convert.ToInt32(comboBoxComponent.SelectedValue); - } - set - { - comboBoxComponent.SelectedValue = value; - } + get => Convert.ToInt32(comboBoxComponent.SelectedValue); + set => comboBoxComponent.SelectedValue = value; } public IComponentModel? ComponentModel { @@ -39,21 +33,19 @@ namespace SushiBar } public int Count { - get { return Convert.ToInt32(textBoxCount.Text); } - set { textBoxCount.Text = value.ToString(); } + get => Convert.ToInt32(textBoxCount.Text); + set => textBoxCount.Text = value.ToString(); } public FormSushiComponent(IComponentLogic logic) { InitializeComponent(); _list = logic.ReadList(null); - if (_list != null) - { - comboBoxComponent.DisplayMember = "ComponentName"; - comboBoxComponent.ValueMember = "Id"; - comboBoxComponent.DataSource = _list; - comboBoxComponent.SelectedItem = null; - } + if (_list == null) return; + comboBoxComponent.DisplayMember = "ComponentName"; + comboBoxComponent.ValueMember = "Id"; + comboBoxComponent.DataSource = _list; + comboBoxComponent.SelectedItem = null; } private void ButtonSave_Click(object sender, EventArgs e) diff --git a/SushiBar/SushiBar/FormSushiMoreThenOne.cs b/SushiBar/SushiBar/FormSushiMoreThenOne.cs index 9455401..4122b42 100644 --- a/SushiBar/SushiBar/FormSushiMoreThenOne.cs +++ b/SushiBar/SushiBar/FormSushiMoreThenOne.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using SushiBarContracts.Attributes; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.DI; @@ -20,14 +21,7 @@ namespace SushiBar { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["SushiComponents"].Visible = false; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Load sushi"); } catch (Exception ex) @@ -40,53 +34,42 @@ namespace SushiBar private void ButtonAdd_Click(object sender, EventArgs e) { var service = DependencyManager.Instance.Resolve(); - if (service is FormSushi form) + if (service.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } private void ButtonEdit_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + var service = DependencyManager.Instance.Resolve(); + service.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (service.ShowDialog() == DialogResult.OK) { - var service = DependencyManager.Instance.Resolve(); - if (service is FormSushi form) - { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } + LoadData(); } } private void ButtonRemove_Click(object sender, EventArgs e) { - if (dataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count != 1) return; + if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != + DialogResult.Yes) return; + var id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Delete sushi"); + try { - if (MessageBox.Show("Delete record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (!_logic.Delete(new SushiBindingModel { Id = id })) { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Delete sushi"); - try - { - if (!_logic.Delete(new SushiBindingModel { Id = id })) - { - throw new Exception("Error on deleting. Additional info below."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error delete sushi"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + throw new Exception("Error on deleting. Additional info below."); } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete sushi"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/SushiBar/SushiBar/FormSushiOnComponents.cs b/SushiBar/SushiBar/FormSushiOnComponents.cs index c00e8b3..d5b66f4 100644 --- a/SushiBar/SushiBar/FormSushiOnComponents.cs +++ b/SushiBar/SushiBar/FormSushiOnComponents.cs @@ -22,25 +22,23 @@ namespace SushiBar { Filter = "xlsx|*.xlsx" }; - if (dialog.ShowDialog() == DialogResult.OK) + if (dialog.ShowDialog() != DialogResult.OK) return; + try { - try - { - _logic.SaveProductComponentToExcelFile(new + _logic.SaveProductComponentToExcelFile(new ReportBindingModel { FileName = dialog.FileName }); - _logger.LogInformation("Saving list sushi on components"); + _logger.LogInformation("Saving list sushi on components"); - MessageBox.Show("Success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка сохранения списка изделий по компонентам"); + MessageBox.Show("Success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения списка изделий по компонентам"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -50,19 +48,16 @@ namespace SushiBar try { var dict = _logic.GetSushi(); - if (dict != null) + dataGridView.Rows.Clear(); + foreach (var elem in dict) { - dataGridView.Rows.Clear(); - foreach (var elem in dict) + dataGridView.Rows.Add(elem.SushiName, "", ""); + foreach (var listElem in elem.Components) { - dataGridView.Rows.Add(new object[] { elem.SushiName, "", "" }); - foreach (var listElem in elem.Components) - { - dataGridView.Rows.Add(new object[] { "", listElem.Item1, listElem.Item2 }); - } - dataGridView.Rows.Add(new object[] { "Count", "", elem.TotalCount }); - dataGridView.Rows.Add(Array.Empty()); + dataGridView.Rows.Add("", listElem.Item1, listElem.Item2); } + dataGridView.Rows.Add("Count", "", elem.TotalCount); + dataGridView.Rows.Add(Array.Empty()); } _logger.LogInformation("Load list sushi on components"); } diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs index c6ea26c..bb9f4a2 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs @@ -82,11 +82,6 @@ namespace SushiBarBusinessLogic.BusinessLogics { _logger.LogInformation("ReadList .Id:{Id}", model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } diff --git a/SushiBar/SushiBarDatabaseImplement/DatabaseImplementationExtension.cs b/SushiBar/SushiBarDatabaseImplement/DatabaseImplementationExtension.cs new file mode 100644 index 0000000..bab0cda --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/DatabaseImplementationExtension.cs @@ -0,0 +1,21 @@ +using SushiBarContracts.DI; +using SushiBarContracts.StoragesContracts; +using SushiBarDatabaseImplement.Implements; + +namespace SushiBarDatabaseImplement; + +public class DatabaseImplementationExtension : IImplementationExtension +{ + public int Priority => 2; + + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj index af97e9f..a226e1c 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -19,4 +19,8 @@ + + + + -- 2.25.1 From 68cb1e64d1e09d31a6d6ad8e66737b7a41a74821 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Thu, 4 May 2023 16:39:59 +0400 Subject: [PATCH 4/4] Completed lab 8 --- .../Models/Client.cs | 6 + .../Models/Component.cs | 92 ++++----- .../Models/Implementer.cs | 11 +- .../Models/Message.cs | 5 + .../SushiBarDatabaseImplement/Models/Order.cs | 187 +++++++++--------- .../SushiBarDatabaseImplement/Models/Sushi.cs | 164 +++++++-------- .../Models/SushiComponents.cs | 27 ++- 7 files changed, 261 insertions(+), 231 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Client.cs b/SushiBar/SushiBarDatabaseImplement/Models/Client.cs index 76ecb27..d4ea043 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Client.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Client.cs @@ -1,18 +1,24 @@ using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; using SushiBarContracts.BindingModels; using SushiBarContracts.ViewModels; using SushiBarDataModels.Models; namespace SushiBarDatabaseImplement.Models; +[DataContract] public class Client : IClientModel { + [DataMember] public int Id { get; private init; } [Required] + [DataMember] public string ClientFio { get; private set; } = string.Empty; [Required] + [DataMember] public string Email { get; private set; } = string.Empty; [Required] + [DataMember] public string Password { get; private set; } = string.Empty; public static Client? Create(ClientBindingModel? model) diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Component.cs b/SushiBar/SushiBarDatabaseImplement/Models/Component.cs index 881d6db..c8ae120 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Component.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Component.cs @@ -3,55 +3,59 @@ using SushiBarContracts.ViewModels; using SushiBarDataModels.Models; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; -namespace SushiBarDatabaseImplement.Models +namespace SushiBarDatabaseImplement.Models; + +[DataContract] +public class Component : IComponentModel { - public class Component : IComponentModel + [DataMember] + public int Id { get; private set; } + [Required] + [DataMember] + public string ComponentName { get; private set; } = string.Empty; + [Required] + [DataMember] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List SushiComponent { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) { - public int Id { get; private set; } - [Required] - public string ComponentName { get; private set; } = string.Empty; - [Required] - public double Cost { get; set; } - [ForeignKey("ComponentId")] - public virtual List SushiComponent { get; set; } = new(); - public static Component? Create(ComponentBindingModel model) + if (model == null) { - if (model == null) - { - return null; - } - return new Component() - { - Id = model.Id, - ComponentName = model.ComponentName, - Cost = model.Cost - }; + return null; } - public static Component Create(ComponentViewModel model) + return new Component() { - return new Component - { - Id = model.Id, - ComponentName = model.ComponentName, - Cost = model.Cost - }; - } - 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 + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost }; - } -} + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + 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 + }; + +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Implementer.cs b/SushiBar/SushiBarDatabaseImplement/Models/Implementer.cs index 72fa96e..e771742 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Implementer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Implementer.cs @@ -1,17 +1,20 @@ using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; using SushiBarContracts.BindingModels; using SushiBarContracts.ViewModels; using SushiBarDataModels.Models; namespace SushiBarDatabaseImplement.Models; +[DataContract] public class Implementer : IImplementerModel { + [DataMember] public int Id { get; private init; } - [Required] public string ImplementerFio { get; private set; } = string.Empty; - [Required] public string Password { get; private set; } = string.Empty; - public int WorkExperience { get; private set; } - public int Qualification { get; private set; } + [Required] [DataMember] public string ImplementerFio { get; private set; } = string.Empty; + [Required] [DataMember] public string Password { get; private set; } = string.Empty; + [DataMember] public int WorkExperience { get; private set; } + [DataMember] public int Qualification { get; private set; } public static Implementer? Create(ImplementerBindingModel? model) { diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Message.cs b/SushiBar/SushiBarDatabaseImplement/Models/Message.cs index 08d7dcd..7ac98dc 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Message.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Message.cs @@ -1,18 +1,23 @@ using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; using SushiBarContracts.BindingModels; using SushiBarContracts.ViewModels; using SushiBarDataModels.Models; namespace SushiBarDatabaseImplement.Models; +[DataContract] public class Message : IMessageInfoModel { [Key] + [DataMember] public string MessageId { get; private set; } = string.Empty; public int? ClientId { get; private set; } [Required] + [DataMember] public string SenderName { get; private set; } = string.Empty; [Required] + [DataMember] public DateTime DateDelivery { get; private set; } = DateTime.Now; [Required] public string Subject { get; private set; } = string.Empty; diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index ed432af..e664b15 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -3,100 +3,109 @@ using SushiBarContracts.ViewModels; using SushiBarDataModels.Enums; using SushiBarDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; -namespace SushiBarDatabaseImplement.Models +namespace SushiBarDatabaseImplement.Models; + +[DataContract] +public class Order : IOrderModel { - public class Order : IOrderModel + [DataMember] + public int Id { get; private set; } + + [Required] + [DataMember] + public int SushiId { get; private set; } + + [Required] + [DataMember] + public int ClientId { get; private set; } + [DataMember] + public int? ImplementerId { get; private set; } + [DataMember] + public string SushiName { get; set; } = string.Empty; + + [Required] + [DataMember] + public int Count { get; private set; } + + [Required] + [DataMember] + public double Sum { get; private set; } + + [Required] + [DataMember] + public OrderStatus Status { get; private set; } = OrderStatus.Unknown; + + [Required] + [DataMember] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public virtual Sushi Sushi { get; set; } + + public virtual Client Client { get; set; } + + public virtual Implementer? Implementer { get; private set; } + + public static Order? Create(OrderBindingModel? model) { - public int Id { get; private set; } - - [Required] - public int SushiId { get; private set; } - - [Required] - public int ClientId { get; private set; } - public int? ImplementerId { get; private set; } = null; - - public string SushiName { get; set; } = string.Empty; - - [Required] - public int Count { get; private set; } - - [Required] - public double Sum { get; private set; } - - [Required] - public OrderStatus Status { get; private set; } = OrderStatus.Unknown; - - [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; - - public DateTime? DateImplement { get; private set; } - - public virtual Sushi Sushi { get; set; } - - public virtual Client Client { get; set; } - - public virtual Implementer? Implementer { get; private set; } - - public static Order? Create(OrderBindingModel? model) + if (model == null) { - if (model == null) - { - return null; - } - - return new Order - { - Id = model.Id, - SushiId = model.SushiId, - SushiName = model.SushiName, - ClientId = model.ClientId, - ImplementerId = model.ImplementerId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; + return null; } - public void Update(OrderBindingModel? model) + return new Order { - if (model == null) - { - return; - } - - SushiId = model.SushiId; - SushiName = model.SushiName; - ClientId = model.ClientId; - ImplementerId = model.ImplementerId; - Count = model.Count; - Sum = model.Sum; - Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; - } - - public OrderViewModel GetViewModel { get - { - var context = new SushiBarDatabase(); - return new OrderViewModel - { - Id = Id, - ClientId = ClientId, - SushiId = SushiId, - Count = Count, - DateCreate = DateCreate, - DateImplement = DateImplement, - ImplementerId = ImplementerId, - Sum = Sum, - Status = Status, - ClientFio = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFio ?? string.Empty, - SushiName = context.Sushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty, - ImplementerFio = context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFio ?? string.Empty, - }; - } } + Id = model.Id, + SushiId = model.SushiId, + SushiName = model.SushiName, + ClientId = model.ClientId, + ImplementerId = model.ImplementerId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; } -} + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + SushiId = model.SushiId; + SushiName = model.SushiName; + ClientId = model.ClientId; + ImplementerId = model.ImplementerId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel { get + { + var context = new SushiBarDatabase(); + return new OrderViewModel + { + Id = Id, + ClientId = ClientId, + SushiId = SushiId, + Count = Count, + DateCreate = DateCreate, + DateImplement = DateImplement, + ImplementerId = ImplementerId, + Sum = Sum, + Status = Status, + ClientFio = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFio ?? string.Empty, + SushiName = context.Sushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty, + ImplementerFio = context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFio ?? string.Empty, + }; + } } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs index d97988c..d34c1dc 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -1,88 +1,92 @@ using SushiBarDataModels.Models; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; using SushiBarContracts.BindingModels; using SushiBarContracts.ViewModels; -namespace SushiBarDatabaseImplement.Models -{ - public class Sushi : ISushiModel - { - public int Id { get; set; } - [Required] - public string SushiName { get; set; } = string.Empty; - [Required] - public double Price { get; set; } - private Dictionary? _sushiComponents = null; - [NotMapped] - public Dictionary SushiComponents - { - get - { - _sushiComponents ??= Components - .ToDictionary(recPC => recPC.ComponentId, recPC => - (recPC.Component as IComponentModel, recPC.Count)); - return _sushiComponents; - } - } - [ForeignKey("SushiId")] - public virtual List Components { get; set; } = new(); - [ForeignKey("SushiId")] - public virtual List Orders { get; set; } = new(); - public static Sushi Create(SushiBarDatabase context, SushiBindingModel model) - { - return new Sushi() - { - Id = model.Id, - SushiName = model.SushiName, - Price = model.Price, - Components = model.SushiComponents.Select(x => new SushiComponent - { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList() - }; - } - public void Update(SushiBindingModel model) - { - SushiName = model.SushiName; - Price = model.Price; - } - public SushiViewModel GetViewModel => new() - { - Id = Id, - SushiName = SushiName, - Price = Price, - SushiComponents = SushiComponents - }; - public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model) - { - var sushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList(); - if (sushiComponents != null && sushiComponents.Count > 0) - { - context.SushiComponents.RemoveRange(sushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId))); - context.SaveChanges(); - - foreach (var updateComponent in sushiComponents) - { - updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2; - model.SushiComponents.Remove(updateComponent.ComponentId); - } - context.SaveChanges(); - } - var sushi = context.Sushi.First(x => x.Id == Id); - foreach (var pc in model.SushiComponents) - { - context.SushiComponents.Add(new SushiComponent - { - Sushi = sushi, - Component = context.Components.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 - }); - context.SaveChanges(); - } - _sushiComponents = null; - } +namespace SushiBarDatabaseImplement.Models; +[DataContract] +public class Sushi : ISushiModel +{ + [DataMember] + public int Id { get; set; } + [Required] + [DataMember] + public string SushiName { get; set; } = string.Empty; + [Required] + [DataMember] + public double Price { get; set; } + private Dictionary? _sushiComponents = null; + [NotMapped] + public Dictionary SushiComponents + { + get + { + _sushiComponents ??= Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + return _sushiComponents; + } } -} + [ForeignKey("SushiId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("SushiId")] + public virtual List Orders { get; set; } = new(); + public static Sushi Create(SushiBarDatabase context, SushiBindingModel model) + { + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Components = model.SushiComponents.Select(x => new SushiComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SushiBindingModel model) + { + SushiName = model.SushiName; + Price = model.Price; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model) + { + var sushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList(); + if (sushiComponents != null && sushiComponents.Count > 0) + { + context.SushiComponents.RemoveRange(sushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + + foreach (var updateComponent in sushiComponents) + { + updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2; + model.SushiComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var sushi = context.Sushi.First(x => x.Id == Id); + foreach (var pc in model.SushiComponents) + { + context.SushiComponents.Add(new SushiComponent + { + Sushi = sushi, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _sushiComponents = null; + } + +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs index dca7318..7f52112 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs @@ -1,17 +1,16 @@ using System.ComponentModel.DataAnnotations; -namespace SushiBarDatabaseImplement.Models +namespace SushiBarDatabaseImplement.Models; + +public class SushiComponent { - public class SushiComponent - { - public int Id { get; set; } - [Required] - public int SushiId { get; set; } - [Required] - public int ComponentId { get; set; } - [Required] - public int Count { get; set; } - public virtual Component Component { get; set; } = new(); - public virtual Sushi Sushi { get; set; } = new(); - } -} + public int Id { get; set; } + [Required] + public int SushiId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Sushi Sushi { get; set; } = new(); +} \ No newline at end of file -- 2.25.1