diff --git a/PARR.API/Controllers/V1/TemplateController.cs b/PARR.API/Controllers/V1/TemplateController.cs index 0c3cf8fb..960f242a 100644 --- a/PARR.API/Controllers/V1/TemplateController.cs +++ b/PARR.API/Controllers/V1/TemplateController.cs @@ -9,6 +9,7 @@ using PARR.API.Contracts.V1.Responses; using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Controllers.V1.Base; using PARR.API.Extensions; +using PARR.API.Services.Interfaces; using PARR.BLL.Helpers; using PARR.Constants; using PARR.DAL.Contracts; @@ -28,18 +29,21 @@ namespace PARR.API.Controllers.V1 private readonly ITemplateService templateService; private readonly SettingsFromDb settingsFromDb; private readonly IRobotConfigurationService robotConfigurationService; + private readonly IClientService clientService; public TemplateController( IMapper mapper, ITemplateService templateService, SettingsFromDb settingsFromDb, - IRobotConfigurationService robotConfigurationService + IRobotConfigurationService robotConfigurationService, + IClientService clientService ) { this.mapper = mapper; this.templateService = templateService; this.settingsFromDb = settingsFromDb; this.robotConfigurationService = robotConfigurationService; + this.clientService = clientService; } @@ -91,7 +95,7 @@ namespace PARR.API.Controllers.V1 .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot) .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus) .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus) - .Include(t=>t.Orders) + .Include(t => t.Orders) .AsSplitQuery() .FirstOrDefaultAsync(t => t.Id == id); @@ -139,7 +143,7 @@ namespace PARR.API.Controllers.V1 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 { new ErrorModel { Message = "Ошибка при изменении шаблона." } })); var templateToResponse = await templateService.GetWithIncludes() diff --git a/PARR.DAL/Services/Abstracts/BaseService.cs b/PARR.DAL/Services/Abstracts/BaseService.cs index 193eff96..59e82796 100644 --- a/PARR.DAL/Services/Abstracts/BaseService.cs +++ b/PARR.DAL/Services/Abstracts/BaseService.cs @@ -40,12 +40,6 @@ namespace PARR.DAL.Services.Abstracts public async Task CommitAsync(HistoryInitiator? initiator = null) { - //todo: remove initiator - //проверить что инициатор сохраняется в родительской таблице - // что с родительской переписывается в историю - // входной initiator не переписывает историю - initiator = new HistoryInitiator { InitiatorComment = "Comment", InitiatorIp = "10.10.10.10", InitiatorParrComponentId = Constants.ParrComponentsEnum.Api }; - #region Изменения var modifiedEntrities = EntitiContext.ChangeTracker.Entries() .Where(t => t.State == EntityState.Modified/* || t.State == EntityState.Deleted*/); @@ -60,8 +54,7 @@ namespace PARR.DAL.Services.Abstracts } #endregion - if (initiator != null) - SetInitiator(initiator); + SetInitiator(initiator); try { @@ -80,7 +73,7 @@ namespace PARR.DAL.Services.Abstracts /// Установить инициатора /// /// - private void SetInitiator(IHistoryInitiator initiator) + private void SetInitiator(IHistoryInitiator? initiator) { // Задваем иницатора только для новых и измененных записей var entrities = EntitiContext.ChangeTracker.Entries() @@ -92,9 +85,9 @@ namespace PARR.DAL.Services.Abstracts // для всех объектов которые наследуются от IHistoryInitiator и не являются наследниками IHistoryTable if (obj.Entity is IHistoryInitiator && obj.Entity is IHistoryTable == false) { - (obj.Entity as IHistoryInitiator)!.InitiatorIp = initiator.InitiatorIp; - (obj.Entity as IHistoryInitiator)!.InitiatorParrComponentId = initiator.InitiatorParrComponentId; - (obj.Entity as IHistoryInitiator)!.InitiatorComment = initiator.InitiatorComment; + (obj.Entity as IHistoryInitiator)!.InitiatorIp = initiator?.InitiatorIp ?? null; + (obj.Entity as IHistoryInitiator)!.InitiatorParrComponentId = initiator?.InitiatorParrComponentId ?? null; + (obj.Entity as IHistoryInitiator)!.InitiatorComment = initiator?.InitiatorComment ?? null; } } } @@ -198,7 +191,7 @@ namespace PARR.DAL.Services.Abstracts /// /// /// - private void FillHistoryProp(ref object instanceObj, string propName, T value) + private void FillHistoryProp(ref object instanceObj, string propName, TValue value) { var histProp = instanceObj.GetType().GetProperty(propName); 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; }