Back up and dm
This commit is contained in:
parent
2c4cb0dbb2
commit
fe06793f3c
@ -2,6 +2,7 @@
|
|||||||
using SushiBarContracts.BindingModels;
|
using SushiBarContracts.BindingModels;
|
||||||
using SushiBarContracts.BusinessLogicsContracts;
|
using SushiBarContracts.BusinessLogicsContracts;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using SushiBarContracts.Attributes;
|
||||||
|
|
||||||
namespace SushiBar
|
namespace SushiBar
|
||||||
{
|
{
|
||||||
@ -21,13 +22,7 @@ namespace SushiBar
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var list = _logic.ReadList(null);
|
dataGridView.FillAndConfigGrid(_logic.ReadList(null));
|
||||||
if (list != null)
|
|
||||||
{
|
|
||||||
dataGridView.DataSource = list;
|
|
||||||
dataGridView.Columns["Id"].Visible = false;
|
|
||||||
dataGridView.Columns["ClientFio"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
|
||||||
}
|
|
||||||
_logger.LogInformation("Load clients");
|
_logger.LogInformation("Load clients");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -13,17 +13,20 @@ namespace SushiBar
|
|||||||
private readonly IOrderLogic _orderLogic;
|
private readonly IOrderLogic _orderLogic;
|
||||||
private readonly IReportLogic _reportLogic;
|
private readonly IReportLogic _reportLogic;
|
||||||
private readonly IWorkProcess _workProcess;
|
private readonly IWorkProcess _workProcess;
|
||||||
|
private readonly IBackUpLogic _backUpLogic;
|
||||||
|
|
||||||
public FormMain(ILogger<FormMain> logger,
|
public FormMain(ILogger<FormMain> logger,
|
||||||
IOrderLogic orderLogic,
|
IOrderLogic orderLogic,
|
||||||
IReportLogic reportLogic,
|
IReportLogic reportLogic,
|
||||||
IWorkProcess workProcess)
|
IWorkProcess workProcess,
|
||||||
|
IBackUpLogic backUpLogic)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_orderLogic = orderLogic;
|
_orderLogic = orderLogic;
|
||||||
_reportLogic = reportLogic;
|
_reportLogic = reportLogic;
|
||||||
_workProcess = workProcess;
|
_workProcess = workProcess;
|
||||||
|
_backUpLogic = backUpLogic;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadData()
|
private void LoadData()
|
||||||
@ -249,5 +252,21 @@ namespace SushiBar
|
|||||||
form.ShowDialog();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,7 @@ namespace SushiBar
|
|||||||
services.AddTransient<IClientLogic, ClientLogic>();
|
services.AddTransient<IClientLogic, ClientLogic>();
|
||||||
services.AddTransient<IImplementerLogic, ImplementerLogic>();
|
services.AddTransient<IImplementerLogic, ImplementerLogic>();
|
||||||
services.AddTransient<IMessageInfoLogic, MessageInfoLogic>();
|
services.AddTransient<IMessageInfoLogic, MessageInfoLogic>();
|
||||||
|
services.AddTransient<IBackUpLogic, BackUpLogic>();
|
||||||
|
|
||||||
services.AddTransient<IWorkProcess, WorkModeling>();
|
services.AddTransient<IWorkProcess, WorkModeling>();
|
||||||
services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
||||||
|
87
SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs
Normal file
87
SushiBar/SushiBarBusinessLogic/BusinessLogics/BackUpLogic.cs
Normal file
@ -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<BackUpLogic> 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<T>(string folderName) where T : class, new()
|
||||||
|
{
|
||||||
|
var records = _backUpInfo.GetList<T>();
|
||||||
|
|
||||||
|
if (records == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("{type} type get null list", typeof(T).Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonFormatter = new DataContractJsonSerializer(typeof(List<T>));
|
||||||
|
using var fs = new FileStream($"{folderName}/{typeof(T).Name}.json", FileMode.OpenOrCreate);
|
||||||
|
jsonFormatter.WriteObject(fs, records);
|
||||||
|
}
|
||||||
|
}
|
23
SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs
Normal file
23
SushiBar/SushiBarContracts/Attributes/ColumnAttribute.cs
Normal file
@ -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; }
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace SushiBarContracts.Attributes;
|
||||||
|
|
||||||
|
public static class DataGridViewExtension
|
||||||
|
{
|
||||||
|
public static void FillAndConfigGrid<T>(this DataGridView grid, List<T>? 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs
Normal file
13
SushiBar/SushiBarContracts/Attributes/GridViewAutoSize.cs
Normal file
@ -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
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace SushiBarContracts.BindingModels;
|
||||||
|
|
||||||
|
public class BackUpSaveBindingModel
|
||||||
|
{
|
||||||
|
public string FolderName { get; set; } = string.Empty;
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
using SushiBarContracts.BindingModels;
|
||||||
|
|
||||||
|
namespace SushiBarContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IBackUpLogic
|
||||||
|
{
|
||||||
|
void CreateBackUp(BackUpSaveBindingModel model);
|
||||||
|
}
|
45
SushiBar/SushiBarContracts/DI/DependencyManager.cs
Normal file
45
SushiBar/SushiBarContracts/DI/DependencyManager.cs
Normal file
@ -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<ILoggingBuilder> configure) =>
|
||||||
|
_dependencyManager.AddLogging(configure);
|
||||||
|
|
||||||
|
public void RegisterType<T, TU>(bool isSingle = false) where TU : class, T where T : class =>
|
||||||
|
_dependencyManager.RegisterType<T, TU>(isSingle);
|
||||||
|
|
||||||
|
public void RegisterType<T>(bool isSingle = false) where T : class =>
|
||||||
|
_dependencyManager.RegisterType<T>(isSingle);
|
||||||
|
|
||||||
|
public T Resolve<T>() =>
|
||||||
|
_dependencyManager.Resolve<T>();
|
||||||
|
}
|
11
SushiBar/SushiBarContracts/DI/IDependencyContainer.cs
Normal file
11
SushiBar/SushiBarContracts/DI/IDependencyContainer.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace SushiBarContracts.DI;
|
||||||
|
|
||||||
|
public interface IDependencyContainer
|
||||||
|
{
|
||||||
|
void AddLogging(Action<ILoggingBuilder> configure);
|
||||||
|
void RegisterType<T, TU>(bool isSingle) where TU : class, T where T : class;
|
||||||
|
void RegisterType<T>(bool isSingle) where T : class;
|
||||||
|
T Resolve<T>();
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace SushiBarContracts.DI;
|
||||||
|
|
||||||
|
public interface IImplementationExtension
|
||||||
|
{
|
||||||
|
public int Priority { get; }
|
||||||
|
public void RegisterServices();
|
||||||
|
}
|
47
SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs
Normal file
47
SushiBar/SushiBarContracts/DI/ServiceProviderLoader.cs
Normal file
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace SushiBarContracts.StoragesContracts;
|
||||||
|
|
||||||
|
public interface IBackUpInfo
|
||||||
|
{
|
||||||
|
List<T>? GetList<T>() where T : class, new();
|
||||||
|
Type? GetTypeByModelInterface(string modelInterfaceName);
|
||||||
|
}
|
@ -10,4 +10,14 @@
|
|||||||
<ProjectReference Include="..\SushiBarModels\SushiBarDataModels.csproj" />
|
<ProjectReference Include="..\SushiBarModels\SushiBarDataModels.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System.Windows.Forms">
|
||||||
|
<HintPath>..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.8\System.Windows.Forms.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
26
SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs
Normal file
26
SushiBar/SushiBarDatabaseImplement/Implements/BackUpInfo.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
|
||||||
|
namespace SushiBarDatabaseImplement.Implements;
|
||||||
|
|
||||||
|
public class BackUpInfo : IBackUpInfo
|
||||||
|
{
|
||||||
|
public List<T>? GetList<T>() where T: class, new()
|
||||||
|
{
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
return context.Set<T>().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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user