MessageInterface inherited IID and MessageModels were modified.
This commit is contained in:
parent
5ed0dc584b
commit
8abe0bbfb2
98
DressAtelierBusinessLogic/BusinessLogic/BackupLogic.cs
Normal file
98
DressAtelierBusinessLogic/BusinessLogic/BackupLogic.cs
Normal file
@ -0,0 +1,98 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.BusinessLogicContracts;
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierDataModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
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 DressAtelierBusinessLogic.BusinessLogic
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (_backupInfo == null) { return; }
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Clear folder");
|
||||
// removing data from dir and old archive deletion
|
||||
var dirInfo = new DirectoryInfo(model.FolderName);
|
||||
if (dirInfo.Exists)
|
||||
{
|
||||
foreach (var file in dirInfo.GetFiles())
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
}
|
||||
_logger.LogDebug("Delete archive");
|
||||
string fileName = $"{model.FolderName}.zip";
|
||||
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
File.Delete(fileName);
|
||||
}
|
||||
// taking method for saving
|
||||
_logger.LogDebug("Get assembly");
|
||||
var typeIID = typeof(IID);
|
||||
var assembly = typeIID.Assembly;
|
||||
if (assembly == null)
|
||||
{
|
||||
throw new ArgumentNullException("Assembly wasn't 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)
|
||||
{
|
||||
var modelType = _backupInfo.GetTypeByModelInterface(type.Name);
|
||||
if (modelType == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Class-model wasn't found for {type.Name}");
|
||||
}
|
||||
_logger.LogDebug("Call SaveToFile method for {name} type", type.Name);
|
||||
// call method to perform
|
||||
method?.MakeGenericMethod(modelType).Invoke(this, new object[] { model.FolderName });
|
||||
}
|
||||
}
|
||||
_logger.LogDebug("Create zip and remove folder");
|
||||
// archive
|
||||
ZipFile.CreateFromDirectory(model.FolderName, fileName);
|
||||
// folder deletion
|
||||
dirInfo.Delete(true);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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(string.Format("{0}/{1}.json", folderName, typeof(T).Name), FileMode.OpenOrCreate);
|
||||
jsonFormatter.WriteObject(fs, records); }
|
||||
}
|
||||
}
|
@ -61,7 +61,7 @@ namespace DressAtelierBusinessLogic.MailEmployee
|
||||
list.Add(new MessageInfoBindingModel
|
||||
{
|
||||
DeliveryDate = message.Date.DateTime,
|
||||
ID = message.MessageId,
|
||||
MessageID = message.MessageId,
|
||||
SenderName = mail.Address,
|
||||
Subject = message.Subject,
|
||||
Body = message.TextBody != null ? message.TextBody : message.HtmlBody
|
||||
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierContracts.BindingModels
|
||||
{
|
||||
public class BackupSaveBindingModel
|
||||
{
|
||||
public string FolderName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace DressAtelierContracts.BindingModels
|
||||
{
|
||||
public class MessageInfoBindingModel : IMessageInfoModel
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public string MessageID { get; set; } = string.Empty;
|
||||
|
||||
public int? ClientID { get; set; }
|
||||
|
||||
@ -20,5 +20,7 @@ namespace DressAtelierContracts.BindingModels
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public int ID { get; set; }
|
||||
}
|
||||
}
|
||||
|
14
DressAtelierContracts/BusinessLogicContracts/IBackupLogic.cs
Normal file
14
DressAtelierContracts/BusinessLogicContracts/IBackupLogic.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierContracts.BusinessLogicContracts
|
||||
{
|
||||
public interface IBackupLogic
|
||||
{
|
||||
void CreateBackup(BackupSaveBindingModel model);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ namespace DressAtelierContracts.SearchModels
|
||||
{
|
||||
public class MessageInfoSearchModel
|
||||
{
|
||||
public string? ID { get; set; }
|
||||
public string? MessageID { get; set; }
|
||||
public int? ClientID { get; set; }
|
||||
|
||||
}
|
||||
|
14
DressAtelierContracts/StorageContracts/IBackupInfo.cs
Normal file
14
DressAtelierContracts/StorageContracts/IBackupInfo.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierContracts.StorageContracts
|
||||
{
|
||||
public interface IBackupInfo
|
||||
{
|
||||
List<T>? GetList<T>() where T : class, new();
|
||||
Type? GetTypeByModelInterface(string modelInterfaceName);
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ namespace DressAtelierContracts.ViewModels
|
||||
public class MessageInfoViewModel : IMessageInfoModel
|
||||
{
|
||||
[Column(visible: false)]
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public string MessageID { get; set; } = string.Empty;
|
||||
|
||||
[Column(visible: false)]
|
||||
public int? ClientID { get; set; }
|
||||
@ -28,5 +28,7 @@ namespace DressAtelierContracts.ViewModels
|
||||
|
||||
[Column(title: "Message content", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public int ID { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDataModels.Models
|
||||
{
|
||||
public interface IMessageInfoModel
|
||||
public interface IMessageInfoModel : IID
|
||||
{
|
||||
string ID { get; }
|
||||
string MessageID { get; }
|
||||
int? ClientID { get; }
|
||||
string SenderName { get; }
|
||||
DateTime DeliveryDate { get; }
|
||||
|
33
DressAtelierDatabaseImplement/Implements/BackupInfo.cs
Normal file
33
DressAtelierDatabaseImplement/Implements/BackupInfo.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierDatabaseImplementation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplement.Implements
|
||||
{
|
||||
public class BackupInfo : IBackupInfo
|
||||
{
|
||||
public List<T>? GetList<T>() where T : class, new()
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -17,12 +17,12 @@ namespace DressAtelierDatabaseImplement.Implements
|
||||
{
|
||||
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
||||
{
|
||||
if(string.IsNullOrEmpty(model.ID))
|
||||
if(string.IsNullOrEmpty(model.MessageID))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.ID.Equals(model.ID))?.GetViewModel;
|
||||
return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.MessageID.Equals(model.MessageID))?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
|
||||
@ -54,7 +54,7 @@ namespace DressAtelierDatabaseImplement.Implements
|
||||
return null;
|
||||
}
|
||||
|
||||
if (GetElement(new MessageInfoSearchModel { ID = model.ID }) != null)
|
||||
if (GetElement(new MessageInfoSearchModel { MessageID = model.MessageID }) != null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ namespace DressAtelierDatabaseImplement.Models
|
||||
public class MessageInfo : IMessageInfoModel
|
||||
{
|
||||
|
||||
public string ID { get; private set; } = string.Empty;
|
||||
public string MessageID { get; private set; } = string.Empty;
|
||||
|
||||
public int? ClientID { get; private set; }
|
||||
|
||||
@ -25,6 +25,8 @@ namespace DressAtelierDatabaseImplement.Models
|
||||
|
||||
public virtual Client? Client { get; private set; }
|
||||
|
||||
public int ID { get; set; }
|
||||
|
||||
public static MessageInfo? Create(MessageInfoBindingModel model)
|
||||
{
|
||||
if(model == null)
|
||||
@ -33,7 +35,7 @@ namespace DressAtelierDatabaseImplement.Models
|
||||
}
|
||||
return new MessageInfo
|
||||
{
|
||||
ID = model.ID,
|
||||
MessageID = model.MessageID,
|
||||
ClientID = model.ClientID,
|
||||
SenderName = model.SenderName,
|
||||
Body = model.Body,
|
||||
@ -44,12 +46,14 @@ namespace DressAtelierDatabaseImplement.Models
|
||||
|
||||
public MessageInfoViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
MessageID = MessageID,
|
||||
ClientID = ClientID,
|
||||
SenderName = SenderName,
|
||||
Body = Body,
|
||||
DeliveryDate = DeliveryDate,
|
||||
Subject = Subject,
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +23,11 @@ namespace DressAtelierFileImplement.Implements
|
||||
|
||||
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
||||
{
|
||||
if(string.IsNullOrEmpty(model.ID))
|
||||
if(string.IsNullOrEmpty(model.MessageID))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _source.Messages.FirstOrDefault(x => x.ID.Equals(model.ID))?.GetViewModel;
|
||||
return _source.Messages.FirstOrDefault(x => x.MessageID.Equals(model.MessageID))?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
|
||||
|
@ -12,7 +12,7 @@ namespace DressAtelierFileImplement.Models
|
||||
{
|
||||
public class MessageInfo : IMessageInfoModel
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public string MessageID { get; set; } = string.Empty;
|
||||
|
||||
public int? ClientID { get; set; }
|
||||
|
||||
@ -24,6 +24,8 @@ namespace DressAtelierFileImplement.Models
|
||||
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public int ID { get; set; }
|
||||
|
||||
public static MessageInfo? Create(MessageInfoBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
@ -32,7 +34,7 @@ namespace DressAtelierFileImplement.Models
|
||||
}
|
||||
return new MessageInfo()
|
||||
{
|
||||
ID = model.ID,
|
||||
MessageID = model.MessageID,
|
||||
ClientID = model.ClientID,
|
||||
SenderName = model.SenderName,
|
||||
DeliveryDate = model.DeliveryDate,
|
||||
@ -50,7 +52,7 @@ namespace DressAtelierFileImplement.Models
|
||||
}
|
||||
return new MessageInfo()
|
||||
{
|
||||
ID = element.Attribute("ID")!.Value,
|
||||
MessageID = element.Attribute("MessageID")!.Value,
|
||||
ClientID = Convert.ToInt32(element.Attribute("ClientID")!.Value),
|
||||
SenderName = element.Element("SenderName")!.Value,
|
||||
DeliveryDate = Convert.ToDateTime(element.Element("DeliveryDate")!.Value),
|
||||
@ -61,7 +63,7 @@ namespace DressAtelierFileImplement.Models
|
||||
|
||||
public MessageInfoViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
MessageID = MessageID,
|
||||
ClientID = ClientID,
|
||||
SenderName = SenderName,
|
||||
DeliveryDate = DeliveryDate,
|
||||
@ -69,12 +71,13 @@ namespace DressAtelierFileImplement.Models
|
||||
Body = Body
|
||||
};
|
||||
|
||||
public XElement GetXElement => new("MessageInfo", new XAttribute("ID", ID),
|
||||
public XElement GetXElement => new("MessageInfo", new XAttribute("MessageID", MessageID),
|
||||
new XElement("ClientID", ClientID.ToString()),
|
||||
new XElement("SenderName", SenderName),
|
||||
new XElement("DeliveryDate", DeliveryDate),
|
||||
new XElement("Subject", Subject),
|
||||
new XElement("Body", Body),
|
||||
new XElement("SenderName", SenderName));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -20,14 +20,14 @@ namespace DressAtelierListImplement.Implements
|
||||
|
||||
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
||||
{
|
||||
if(string.IsNullOrEmpty(model.ID))
|
||||
if(string.IsNullOrEmpty(model.MessageID))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach(var msg in _source.Messages)
|
||||
{
|
||||
if(msg.ID.Equals(model.ID))
|
||||
if(msg.MessageID.Equals(model.MessageID))
|
||||
{
|
||||
return msg.GetViewModel;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ namespace DressAtelierListImplement.Models
|
||||
{
|
||||
public class MessageInfo : IMessageInfoModel
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public string MessageID { get; set; } = string.Empty;
|
||||
|
||||
public int? ClientID { get; set; }
|
||||
|
||||
@ -23,6 +23,8 @@ namespace DressAtelierListImplement.Models
|
||||
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public int ID { get; set; }
|
||||
|
||||
public static MessageInfo? Create(MessageInfoBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
@ -31,7 +33,7 @@ namespace DressAtelierListImplement.Models
|
||||
}
|
||||
return new()
|
||||
{
|
||||
ID = model.ID,
|
||||
MessageID = model.MessageID,
|
||||
SenderName = model.SenderName,
|
||||
DeliveryDate = model.DeliveryDate,
|
||||
Subject = model.Subject,
|
||||
@ -41,7 +43,7 @@ namespace DressAtelierListImplement.Models
|
||||
|
||||
public MessageInfoViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
MessageID = MessageID,
|
||||
SenderName = SenderName,
|
||||
DeliveryDate = DeliveryDate,
|
||||
Subject = Subject,
|
||||
|
Loading…
x
Reference in New Issue
Block a user