Дополнил

This commit is contained in:
maxnes3 2023-05-06 13:01:49 +04:00
parent 7716a6c367
commit 3bd366daee
8 changed files with 210 additions and 3 deletions

View File

@ -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<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);
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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<T>? GetList<T>() where T : class, new();
Type? GetTypeByModelInterface(string modelInterfaceName);
}
}

View File

@ -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<T>? GetList<T>() where T : class, new()
{
using var context = new PersonnelDepartmentDatabase();
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;
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}
}

View File

@ -34,12 +34,14 @@ namespace PersonnelDepartmentView
services.AddTransient<IEmployeeLogic, EmployeeLogic>();
services.AddTransient<IPositionLogic, PositionLogic>();
services.AddTransient<ITypeLogic, TypeLogic>();
services.AddTransient<IBackUpLogic, BackUpLogic>();
services.AddTransient<IDealStorage, DealStorage>();
services.AddTransient<IDepartmentStorage, DepartmentStorage>();
services.AddTransient<IEmployeeStorage, EmployeeStorage>();
services.AddTransient<ITypeStorage, TypeStorage>();
services.AddTransient<IPositionStorage, PositionStorage>();
services.AddTransient<IBackUpInfo, BackUpInfo>();
services.AddTransient<FormDeal>();
services.AddTransient<FormDeals>();