diff --git a/PersonnelDepartmentView/PersonnelDepartmentBusinessLogic/BusinessLogics/BackUpLogic.cs b/PersonnelDepartmentView/PersonnelDepartmentBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..344ac2d --- /dev/null +++ b/PersonnelDepartmentView/PersonnelDepartmentBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,90 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.BusinessLogicContracts; +using PersonnelDepartmentContracts.StoragesContracts; +using PersonnelDepartmentDataModels; +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 PersonnelDepartmentBusinessLogic.BusinessLogics +{ + public class BackUpLogic : IBackUpLogic + { + private readonly IBackUpInfo _backUpInfo; + + public BackUpLogic(IBackUpInfo backUpInfo) + { + _backUpInfo = backUpInfo; + } + + public void CreateBackUp(BackUpBinidngModel 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/PersonnelDepartmentView/PersonnelDepartmentContracts/BindingModels/BackUpBinidngModel.cs b/PersonnelDepartmentView/PersonnelDepartmentContracts/BindingModels/BackUpBinidngModel.cs new file mode 100644 index 0000000..d5394af --- /dev/null +++ b/PersonnelDepartmentView/PersonnelDepartmentContracts/BindingModels/BackUpBinidngModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentContracts.BindingModels +{ + public class BackUpBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} diff --git a/PersonnelDepartmentView/PersonnelDepartmentContracts/BusinessLogicContracts/IBackUpLogic.cs b/PersonnelDepartmentView/PersonnelDepartmentContracts/BusinessLogicContracts/IBackUpLogic.cs new file mode 100644 index 0000000..26e3301 --- /dev/null +++ b/PersonnelDepartmentView/PersonnelDepartmentContracts/BusinessLogicContracts/IBackUpLogic.cs @@ -0,0 +1,14 @@ +using PersonnelDepartmentContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentContracts.BusinessLogicContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpBinidngModel model); + } +} diff --git a/PersonnelDepartmentView/PersonnelDepartmentContracts/StoragesContracts/IBackUpInfo.cs b/PersonnelDepartmentView/PersonnelDepartmentContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..a6d271f --- /dev/null +++ b/PersonnelDepartmentView/PersonnelDepartmentContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} diff --git a/PersonnelDepartmentView/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs b/PersonnelDepartmentView/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..abc04ab --- /dev/null +++ b/PersonnelDepartmentView/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,32 @@ +using PersonnelDepartmentContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new PersonnelDepartmentDatabase(); + 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/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.Designer.cs b/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.Designer.cs index d26c542..d49c3a1 100644 --- a/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.Designer.cs +++ b/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.Designer.cs @@ -38,6 +38,7 @@ buttonPosition = new Button(); buttonType = new Button(); buttonGenerate = new Button(); + buttonBackUp = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // @@ -142,8 +143,8 @@ // // buttonGenerate // - buttonGenerate.Anchor = AnchorStyles.Bottom; - buttonGenerate.Location = new Point(391, 415); + buttonGenerate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonGenerate.Location = new Point(12, 415); buttonGenerate.Name = "buttonGenerate"; buttonGenerate.Size = new Size(163, 23); buttonGenerate.TabIndex = 9; @@ -151,12 +152,24 @@ buttonGenerate.UseVisualStyleBackColor = true; buttonGenerate.Click += ButtonGenerate_Click; // + // buttonBackUp + // + buttonBackUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonBackUp.Location = new Point(783, 415); + buttonBackUp.Name = "buttonBackUp"; + buttonBackUp.Size = new Size(117, 23); + buttonBackUp.TabIndex = 10; + buttonBackUp.Text = "Создать BackUp"; + buttonBackUp.UseVisualStyleBackColor = true; + buttonBackUp.Click += CreateBackup_Click; + // // FormDeals // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; BackColor = SystemColors.Control; ClientSize = new Size(914, 446); + Controls.Add(buttonBackUp); Controls.Add(buttonGenerate); Controls.Add(buttonType); Controls.Add(buttonPosition); @@ -187,5 +200,6 @@ private Button buttonPosition; private Button buttonType; private Button buttonGenerate; + private Button buttonBackUp; } } \ No newline at end of file diff --git a/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.cs b/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.cs index c736fd2..b4aa107 100644 --- a/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.cs +++ b/PersonnelDepartmentView/PersonnelDepartmentView/FormDeals.cs @@ -6,11 +6,13 @@ namespace PersonnelDepartmentView public partial class FormDeals : Form { private readonly IDealLogic _dealLogic; + private readonly IBackUpLogic _backUpLogic; - public FormDeals(IDealLogic dealLogic) + public FormDeals(IDealLogic dealLogic, IBackUpLogic backUpLogic) { InitializeComponent(); _dealLogic = dealLogic; + _backUpLogic = backUpLogic; } private void LoadData() @@ -148,5 +150,31 @@ namespace PersonnelDepartmentView form.ShowDialog(); } } + + private void CreateBackup_Click(object sender, EventArgs e) + { + try + { + if (_backUpLogic != null) + { + var fbd = new FolderBrowserDialog(); + if (fbd.ShowDialog() == DialogResult.OK) + { + _backUpLogic.CreateBackUp(new BackUpBinidngModel + { + FolderName = fbd.SelectedPath + }); + MessageBox.Show(" ", "", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + } } } \ No newline at end of file diff --git a/PersonnelDepartmentView/PersonnelDepartmentView/Program.cs b/PersonnelDepartmentView/PersonnelDepartmentView/Program.cs index deab47a..71db762 100644 --- a/PersonnelDepartmentView/PersonnelDepartmentView/Program.cs +++ b/PersonnelDepartmentView/PersonnelDepartmentView/Program.cs @@ -34,12 +34,14 @@ namespace PersonnelDepartmentView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();