Добавил бэкапы
This commit is contained in:
parent
bf57e05c9a
commit
b9c7b83110
@ -0,0 +1,90 @@
|
||||
using RestaurantContracts.BindingModels;
|
||||
using RestaurantContracts.BusinessLogicsContracts;
|
||||
using RestaurantContracts.StoragesContracts;
|
||||
using RestaurantDataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RestaurantBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class BackUpLogic : IBackUpLogic
|
||||
{
|
||||
private readonly IBackUpInfo _backUpInfo;
|
||||
|
||||
public BackUpLogic(IBackUpInfo backUpInfo)
|
||||
{
|
||||
_backUpInfo = backUpInfo;
|
||||
}
|
||||
|
||||
public void CreateBackUp(BackUpBindingModel model)
|
||||
{
|
||||
if (_backUpInfo == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(model.FolderName);
|
||||
if (dirInfo.Exists)
|
||||
{
|
||||
foreach (var file in dirInfo.GetFiles())
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
}
|
||||
string fileName = $"{model.FolderName}.zip";
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
File.Delete(fileName);
|
||||
}
|
||||
var typeIId = typeof(IId);
|
||||
var assembly = typeIId.Assembly;
|
||||
if (assembly == null)
|
||||
{
|
||||
throw new ArgumentNullException("Сборка не найдена", nameof(assembly));
|
||||
}
|
||||
var types = assembly.GetTypes();
|
||||
var method = GetType().GetMethod("SaveToFile", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
foreach (var type in types)
|
||||
{
|
||||
if (type.IsInterface && type.GetInterface(typeIId.Name) != null)
|
||||
{
|
||||
var modelType = _backUpInfo.GetTypeByModelInterface(type.Name);
|
||||
if (modelType == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Не найден класс-модель для {type.Name}");
|
||||
}
|
||||
// вызываем метод на выполнение
|
||||
method?.MakeGenericMethod(modelType).Invoke(this, new object[] { model.FolderName });
|
||||
}
|
||||
}
|
||||
// архивируем
|
||||
ZipFile.CreateFromDirectory(model.FolderName, fileName);
|
||||
// удаляем папку
|
||||
dirInfo.Delete(true);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveToFile<T>(string folderName) where T : class, new()
|
||||
{
|
||||
var records = _backUpInfo.GetList<T>();
|
||||
if (records == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var jsonFormatter = new DataContractJsonSerializer(typeof(List<T>));
|
||||
using var fs = new FileStream(string.Format("{0}/{1}.json", folderName, typeof(T).Name), FileMode.OpenOrCreate);
|
||||
jsonFormatter.WriteObject(fs, records);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RestaurantContracts.BindingModels
|
||||
{
|
||||
public class BackUpBindingModel
|
||||
{
|
||||
public string FolderName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using RestaurantContracts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RestaurantContracts.BusinessLogicsContracts
|
||||
{
|
||||
public interface IBackUpLogic
|
||||
{
|
||||
void CreateBackUp(BackUpBindingModel model);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RestaurantContracts.StoragesContracts
|
||||
{
|
||||
public interface IBackUpInfo
|
||||
{
|
||||
List<T>? GetList<T>() where T : class, new();
|
||||
Type? GetTypeByModelInterface(string modelInterfaceName);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using RestaurantContracts.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RestaurantDatabaseImplement.Implements
|
||||
{
|
||||
public class BackUpInfo : IBackUpInfo
|
||||
{
|
||||
public List<T>? GetList<T>() where T : class, new()
|
||||
{
|
||||
using var context = new RestaurantDatabase();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
12
Restaurant/RestaurantView/FormMain.Designer.cs
generated
12
Restaurant/RestaurantView/FormMain.Designer.cs
generated
@ -35,6 +35,7 @@
|
||||
this.ProvidersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.ComponentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.ProductsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.backUpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.buttonUpd = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
|
||||
this.menuStrip1.SuspendLayout();
|
||||
@ -65,7 +66,8 @@
|
||||
this.ClientsToolStripMenuItem,
|
||||
this.ProvidersToolStripMenuItem,
|
||||
this.ComponentsToolStripMenuItem,
|
||||
this.ProductsToolStripMenuItem});
|
||||
this.ProductsToolStripMenuItem,
|
||||
this.backUpToolStripMenuItem});
|
||||
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
|
||||
this.menuStrip1.Name = "menuStrip1";
|
||||
this.menuStrip1.Size = new System.Drawing.Size(1163, 24);
|
||||
@ -100,6 +102,13 @@
|
||||
this.ProductsToolStripMenuItem.Text = "Продукты";
|
||||
this.ProductsToolStripMenuItem.Click += new System.EventHandler(this.ProductsToolStripMenuItem_Click);
|
||||
//
|
||||
// backUpToolStripMenuItem
|
||||
//
|
||||
this.backUpToolStripMenuItem.Name = "backUpToolStripMenuItem";
|
||||
this.backUpToolStripMenuItem.Size = new System.Drawing.Size(51, 20);
|
||||
this.backUpToolStripMenuItem.Text = "Бэкап";
|
||||
this.backUpToolStripMenuItem.Click += new System.EventHandler(this.backUpToolStripMenuItem_Click);
|
||||
//
|
||||
// buttonUpd
|
||||
//
|
||||
this.buttonUpd.Location = new System.Drawing.Point(785, 12);
|
||||
@ -141,5 +150,6 @@
|
||||
private ToolStripMenuItem ComponentsToolStripMenuItem;
|
||||
private ToolStripMenuItem ProductsToolStripMenuItem;
|
||||
private Button buttonUpd;
|
||||
private ToolStripMenuItem backUpToolStripMenuItem;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using RestaurantContracts.BusinessLogicsContracts;
|
||||
using RestaurantContracts.BindingModels;
|
||||
using RestaurantContracts.BusinessLogicsContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@ -15,10 +16,13 @@ namespace RestaurantView
|
||||
{
|
||||
private readonly IOrderLogic _orderLogic;
|
||||
|
||||
public FormMain(IOrderLogic orderLogic)
|
||||
private readonly IBackUpLogic _backUpLogic;
|
||||
|
||||
public FormMain(IOrderLogic orderLogic, IBackUpLogic backUpLogic)
|
||||
{
|
||||
InitializeComponent();
|
||||
_orderLogic = orderLogic;
|
||||
_backUpLogic = backUpLogic;
|
||||
}
|
||||
|
||||
private void LoadData()
|
||||
@ -94,5 +98,30 @@ namespace RestaurantView
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
|
||||
private void backUpToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_backUpLogic != null)
|
||||
{
|
||||
var fbd = new FolderBrowserDialog();
|
||||
if (fbd.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
_backUpLogic.CreateBackUp(new BackUpBindingModel
|
||||
{
|
||||
FolderName = fbd.SelectedPath
|
||||
});
|
||||
MessageBox.Show("Áåêàï ñîçäàí", "Ñîîáùåíèå",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,12 +33,14 @@ namespace RestaurantView
|
||||
services.AddTransient<IProductStorage, ProductStorage>();
|
||||
services.AddTransient<IOrderStorage, OrderStorage>();
|
||||
services.AddTransient<IProviderStorage, ProviderStorage>();
|
||||
services.AddTransient<IBackUpInfo, BackUpInfo>();
|
||||
|
||||
services.AddTransient<IClientLogic, ClientLogic>();
|
||||
services.AddTransient<IComponentLogic, ComponentLogic>();
|
||||
services.AddTransient<IProductLogic, ProductLogic>();
|
||||
services.AddTransient<IOrderLogic, OrderLogic>();
|
||||
services.AddTransient<IProviderLogic, ProviderLogic>();
|
||||
services.AddTransient<IBackUpLogic, BackUpLogic>();
|
||||
|
||||
services.AddTransient<FormMain>();
|
||||
services.AddTransient<FormClients>();
|
||||
|
Loading…
Reference in New Issue
Block a user