feat(dal): BaseService - доделал ведение истории

This commit is contained in:
Mikhail Trubnikov
2024-10-08 09:40:08 +10:00
parent a0e71c9948
commit 54fed9b597
2 changed files with 15 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions; using PARR.API.Extensions;
using PARR.API.Services.Interfaces;
using PARR.BLL.Helpers; using PARR.BLL.Helpers;
using PARR.Constants; using PARR.Constants;
using PARR.DAL.Contracts; using PARR.DAL.Contracts;
@@ -28,18 +29,21 @@ namespace PARR.API.Controllers.V1
private readonly ITemplateService templateService; private readonly ITemplateService templateService;
private readonly SettingsFromDb settingsFromDb; private readonly SettingsFromDb settingsFromDb;
private readonly IRobotConfigurationService robotConfigurationService; private readonly IRobotConfigurationService robotConfigurationService;
private readonly IClientService clientService;
public TemplateController( public TemplateController(
IMapper mapper, IMapper mapper,
ITemplateService templateService, ITemplateService templateService,
SettingsFromDb settingsFromDb, SettingsFromDb settingsFromDb,
IRobotConfigurationService robotConfigurationService IRobotConfigurationService robotConfigurationService,
IClientService clientService
) )
{ {
this.mapper = mapper; this.mapper = mapper;
this.templateService = templateService; this.templateService = templateService;
this.settingsFromDb = settingsFromDb; this.settingsFromDb = settingsFromDb;
this.robotConfigurationService = robotConfigurationService; this.robotConfigurationService = robotConfigurationService;
this.clientService = clientService;
} }
@@ -139,7 +143,7 @@ namespace PARR.API.Controllers.V1
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, ref config); robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, ref config);
} }
if (!await templateService.CommitAsync()) if (!await templateService.CommitAsync(new DAL.Models.Base.History.Base.HistoryInitiator { InitiatorComment = "Изменён статус шаблона/расписания", InitiatorIp = clientService.GetClientIp()?.ToString(), InitiatorParrComponentId = ParrComponentsEnum.Api }))
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при изменении шаблона." } })); return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при изменении шаблона." } }));
var templateToResponse = await templateService.GetWithIncludes() var templateToResponse = await templateService.GetWithIncludes()

View File

@@ -40,12 +40,6 @@ namespace PARR.DAL.Services.Abstracts
public async Task<bool> CommitAsync(HistoryInitiator? initiator = null) public async Task<bool> CommitAsync(HistoryInitiator? initiator = null)
{ {
//todo: remove initiator
//проверить что инициатор сохраняется в родительской таблице
// что с родительской переписывается в историю
// входной initiator не переписывает историю
initiator = new HistoryInitiator { InitiatorComment = "Comment", InitiatorIp = "10.10.10.10", InitiatorParrComponentId = Constants.ParrComponentsEnum.Api };
#region Изменения #region Изменения
var modifiedEntrities = EntitiContext.ChangeTracker.Entries() var modifiedEntrities = EntitiContext.ChangeTracker.Entries()
.Where(t => t.State == EntityState.Modified/* || t.State == EntityState.Deleted*/); .Where(t => t.State == EntityState.Modified/* || t.State == EntityState.Deleted*/);
@@ -60,7 +54,6 @@ namespace PARR.DAL.Services.Abstracts
} }
#endregion #endregion
if (initiator != null)
SetInitiator(initiator); SetInitiator(initiator);
try try
@@ -80,7 +73,7 @@ namespace PARR.DAL.Services.Abstracts
/// Установить инициатора /// Установить инициатора
/// </summary> /// </summary>
/// <param name="initiator"></param> /// <param name="initiator"></param>
private void SetInitiator(IHistoryInitiator initiator) private void SetInitiator(IHistoryInitiator? initiator)
{ {
// Задваем иницатора только для новых и измененных записей // Задваем иницатора только для новых и измененных записей
var entrities = EntitiContext.ChangeTracker.Entries() var entrities = EntitiContext.ChangeTracker.Entries()
@@ -92,9 +85,9 @@ namespace PARR.DAL.Services.Abstracts
// для всех объектов которые наследуются от IHistoryInitiator и не являются наследниками IHistoryTable // для всех объектов которые наследуются от IHistoryInitiator и не являются наследниками IHistoryTable
if (obj.Entity is IHistoryInitiator && obj.Entity is IHistoryTable == false) if (obj.Entity is IHistoryInitiator && obj.Entity is IHistoryTable == false)
{ {
(obj.Entity as IHistoryInitiator)!.InitiatorIp = initiator.InitiatorIp; (obj.Entity as IHistoryInitiator)!.InitiatorIp = initiator?.InitiatorIp ?? null;
(obj.Entity as IHistoryInitiator)!.InitiatorParrComponentId = initiator.InitiatorParrComponentId; (obj.Entity as IHistoryInitiator)!.InitiatorParrComponentId = initiator?.InitiatorParrComponentId ?? null;
(obj.Entity as IHistoryInitiator)!.InitiatorComment = initiator.InitiatorComment; (obj.Entity as IHistoryInitiator)!.InitiatorComment = initiator?.InitiatorComment ?? null;
} }
} }
} }
@@ -198,7 +191,7 @@ namespace PARR.DAL.Services.Abstracts
/// <param name="instanceObj"></param> /// <param name="instanceObj"></param>
/// <param name="propName"></param> /// <param name="propName"></param>
/// <param name="value"></param> /// <param name="value"></param>
private void FillHistoryProp<T>(ref object instanceObj, string propName, T value) private void FillHistoryProp<TValue>(ref object instanceObj, string propName, TValue value)
{ {
var histProp = instanceObj.GetType().GetProperty(propName); var histProp = instanceObj.GetType().GetProperty(propName);
if (histProp == null) if (histProp == null)
@@ -208,9 +201,9 @@ namespace PARR.DAL.Services.Abstracts
} }
// сравним типы // сравним типы
if (histProp.PropertyType != typeof(T)) if (histProp.PropertyType != typeof(TValue))
{ {
logger.LogError($"При изменении объекта для БД, не совпадают типы у свойства {propName}, {histProp.PropertyType.Name}!={typeof(T).Name}"); logger.LogError($"При изменении объекта для БД, не совпадают типы у свойства {propName}, {histProp.PropertyType.Name}!={typeof(TValue).Name}");
return; return;
} }