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