diff --git a/PARR.DAL/Services/Abstracts/BaseService.cs b/PARR.DAL/Services/Abstracts/BaseService.cs index 6806de8b..e01c344c 100644 --- a/PARR.DAL/Services/Abstracts/BaseService.cs +++ b/PARR.DAL/Services/Abstracts/BaseService.cs @@ -3,9 +3,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.Extensions.Logging; using PARR.DAL.Context; using PARR.DAL.DomainModels; -using PARR.DAL.Models; using PARR.DAL.Models.Base; -using PARR.DAL.Models.Base.History; using PARR.DAL.Models.Base.History.Base; using PARR.DAL.Services.Interfaces.Base; using System.Reflection; @@ -107,13 +105,20 @@ namespace PARR.DAL.Services.Abstracts // Заполняем поля истории, полями которые есть в исходной таблице FillHistoryProps(obj, ref historyInstance, historyProps); - // Заполняем поля интерфейса IHistoryTable + FillHistoryProp(ref historyInstance, nameof(IHistoryTable.DateAddedToHistory), DateTimeOffset.UtcNow); // Заполняем Id + FillHistoryProp(ref historyInstance, nameof(IBase.Id), Guid.NewGuid()); - - // EntitiContext.Add(historyInstance); + try + { + EntitiContext.Add(historyInstance); + } + catch (Exception ex) + { + logger.LogError(ex, $"Ошибка при добавлении объекта в историю {historyType.Name}"); + } } @@ -150,11 +155,34 @@ namespace PARR.DAL.Services.Abstracts } + /// + /// Заполнить поле в истории + /// + /// + /// + /// + /// + private void FillHistoryProp(ref object historyInstance, string propName, T value) + { + var histProp = historyInstance.GetType().GetProperty(propName); + if (histProp == null) + { + logger.LogError($"При добавлении истории, не найдено свойство ${propName}"); + return; + } + + // сравним типы + if (histProp.PropertyType != typeof(T)) + { + logger.LogError($"При добавлении истории, не совпадают типы у свойства {propName}, {histProp.PropertyType.Name}!={typeof(T).Name}"); + return; + } + + histProp.SetValue(historyInstance, value); + } - - - + #region test, comments //private void SaveHistory(EntityEntry obj) //{ // // пока просто попробуем для шаблонов @@ -207,8 +235,7 @@ namespace PARR.DAL.Services.Abstracts //} - - + #endregion public virtual async Task CreateAsync(T obj)