diff --git a/Restaurant/RestaurantBusinessLogic/BusinessLogics/BackUpLogic.cs b/Restaurant/RestaurantBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..66ae7a1 --- /dev/null +++ b/Restaurant/RestaurantBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -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(string folderName) where T : class, new() + { + var records = _backUpInfo.GetList(); + if (records == null) + { + return; + } + var jsonFormatter = new DataContractJsonSerializer(typeof(List)); + using var fs = new FileStream(string.Format("{0}/{1}.json", folderName, typeof(T).Name), FileMode.OpenOrCreate); + jsonFormatter.WriteObject(fs, records); + } + } +} diff --git a/Restaurant/RestaurantContracts/BindingModels/BackUpBindingModel.cs b/Restaurant/RestaurantContracts/BindingModels/BackUpBindingModel.cs new file mode 100644 index 0000000..18444b9 --- /dev/null +++ b/Restaurant/RestaurantContracts/BindingModels/BackUpBindingModel.cs @@ -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; + } +} diff --git a/Restaurant/RestaurantContracts/BusinessLogicsContracts/IBackUpLogic.cs b/Restaurant/RestaurantContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..795326c --- /dev/null +++ b/Restaurant/RestaurantContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -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); + } +} diff --git a/Restaurant/RestaurantContracts/StoragesContracts/IBackUpInfo.cs b/Restaurant/RestaurantContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..b3ada84 --- /dev/null +++ b/Restaurant/RestaurantContracts/StoragesContracts/IBackUpInfo.cs @@ -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? GetList() where T : class, new(); + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/BackUpInfo.cs b/Restaurant/RestaurantDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..63135cf --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/BackUpInfo.cs @@ -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? GetList() where T : class, new() + { + using var context = new RestaurantDatabase(); + 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; + } + } +} diff --git a/Restaurant/RestaurantView/FormMain.Designer.cs b/Restaurant/RestaurantView/FormMain.Designer.cs index c69eff5..482b6f7 100644 --- a/Restaurant/RestaurantView/FormMain.Designer.cs +++ b/Restaurant/RestaurantView/FormMain.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormMain.cs b/Restaurant/RestaurantView/FormMain.cs index 7dc42cb..dda1cd9 100644 --- a/Restaurant/RestaurantView/FormMain.cs +++ b/Restaurant/RestaurantView/FormMain.cs @@ -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); + } + } } } diff --git a/Restaurant/RestaurantView/Program.cs b/Restaurant/RestaurantView/Program.cs index eda9b82..fa4a4e9 100644 --- a/Restaurant/RestaurantView/Program.cs +++ b/Restaurant/RestaurantView/Program.cs @@ -33,12 +33,14 @@ namespace RestaurantView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();