feat(api): TemplateHistoryController

This commit is contained in:
Mikhail Trubnikov
2024-10-08 13:53:57 +10:00
parent 4ba5ad3a14
commit 7155156f66
12 changed files with 254 additions and 0 deletions

View File

@@ -73,6 +73,11 @@
public const string getParam = "{id}";
}
public static class TemplateHistory
{
public const string GetAll = Base + "/template-histories/";
}
public static class TemplateScheduleEspp
{
public const string Get = Base + "/templates/" + templateId + "/schedule-esppid";

View File

@@ -0,0 +1,22 @@
using PARR.Constants;
namespace PARR.API.Contracts.V1.Requests.Queries
{
public class TemplateHistoryQuery
{
/// <summary>
/// ИД шаблона
/// </summary>
public Guid? TemplateId { get; set; }
/// <summary>
/// IP инициатора
/// </summary>
public string? InitiatorIp { get; set; }
/// <summary>
/// ИД компонента ПАРР
/// </summary>
public ParrComponentsEnum? ParrComponentId { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
namespace PARR.API.Contracts.V1.Responses
{
public class HistoryInitiatorResponse
{
public string? Ip { get; set; }
public string? Comment { get; set; }
public int? ParrComponentId { get; set; }
public ParrComponentResponse? ParrComponent { get; set; }
public UserBaseResponse? User { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace PARR.API.Contracts.V1.Responses
{
public class ParrComponentResponse
{
public int Id { get; set; }
public required string Name { get; set; }
public required string Description { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
namespace PARR.API.Contracts.V1.Responses
{
public class TemplateHistoryResponse
{
public Guid Id { get; set; }
public Guid TemplateId { get; set; }
public DateTimeOffset DateAddedToHistory { get; set; }
public DateTimeOffset? DateModified { get; set; }
public required string Name { get; set; }
public bool IsActiveTemplate { get; set; }
public bool IsActiveSchedule { get; set; }
public DateTimeOffset? LastRun { get; set; }
public DateTimeOffset NextRun { get; set; }
public HistoryInitiatorResponse? Initiator { get; set; }
}
}

View File

@@ -0,0 +1,70 @@
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.Queries;
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.Constants;
using PARR.DAL.DomainModels;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
/// <summary>
/// История изменения шаблонов
/// </summary>
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class TemplateHistoryController : BaseApiController
{
private readonly IMapper mapper;
private readonly ITemplateHistoryService templateHistoryService;
public TemplateHistoryController(
IMapper mapper,
ITemplateHistoryService templateHistoryService
)
{
this.mapper = mapper;
this.templateHistoryService = templateHistoryService;
}
/// <summary>
/// Получить историю изменения шаблонов постранично
/// </summary>
/// <returns></returns>
[HttpGet(ApiRoutes.TemplateHistory.GetAll)]
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] TemplateHistoryQuery requestQuery)
{
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
IQueryable<TemplateHistory> query = templateHistoryService.Get()
.OrderByDescending(t => t.DateAddedToHistory);
if (requestQuery.TemplateId.HasValue)
query = query.Where(t => t.ParentId == requestQuery.TemplateId.Value);
if (!string.IsNullOrEmpty(requestQuery.InitiatorIp))
query = query.Where(t => t.InitiatorIp == requestQuery.InitiatorIp);
if (requestQuery.ParrComponentId != null)
query = query.Where(t => t.InitiatorParrComponentId == requestQuery.ParrComponentId);
var history = await templateHistoryService.GetPage(query, paginationFilter).ToListAsync();
if (!history.Any())
return NoContent();
var historyResponse = mapper.Map<List<TemplateHistoryResponse>>(history);
var paginationResponse = new PagedResponse<TemplateHistoryResponse>(historyResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
}
}

View File

@@ -2,6 +2,7 @@
using PARR.API.Authentication.Models;
using PARR.API.Contracts.V1.Responses;
using PARR.API.MappingProfiles.Resolvers;
using PARR.Common.Domain;
using PARR.Constants.Shortcode;
using PARR.DAL.DomainModels;
using PARR.DAL.Extensions;
@@ -300,6 +301,29 @@ namespace PARR.API.MappingProfiles
CreateMap<WeekendDay, WeekendResponse>();
#region TemplateHistory
CreateMap<TemplateHistory, TemplateHistoryResponse>()
.ForMember(d => d.TemplateId, o => o.MapFrom(s => s.ParentId))
.ForMember(d => d.Initiator, o => o.MapFrom(s => s));
#endregion
#region Initiator
CreateMap<IHistoryInitiator, HistoryInitiatorResponse>()
.ForMember(d => d.Ip, o => o.MapFrom(s => s.InitiatorIp))
.ForMember(d => d.Comment, o => o.MapFrom(s => s.InitiatorComment))
.ForMember(d => d.ParrComponentId, o => o.MapFrom(s => s.InitiatorParrComponentId))
.ForMember(d => d.ParrComponent, o => o.MapFrom<HistoryInitiatorParrComponentResolver>())
.ForMember(d => d.User, o => o.MapFrom<HistoryInitiatorUserResolver>());
#endregion
}
}
}

View File

@@ -0,0 +1,28 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1.Responses;
using PARR.Common.Domain;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.MappingProfiles.Resolvers
{
public class HistoryInitiatorParrComponentResolver : IValueResolver<IHistoryInitiator, HistoryInitiatorResponse, ParrComponentResponse?>
{
private readonly IParrComponentService parrComponentService;
public HistoryInitiatorParrComponentResolver(IParrComponentService parrComponentService)
{
this.parrComponentService = parrComponentService;
}
public ParrComponentResponse? Resolve(IHistoryInitiator source, HistoryInitiatorResponse destination, ParrComponentResponse? destMember, ResolutionContext context)
{
var component = parrComponentService.Get().FirstOrDefaultAsync(t => t.Id == source.InitiatorParrComponentId).GetAwaiter().GetResult();
if (component == null)
return null;
return new ParrComponentResponse { Description = component.Description, Name = component.Name, Id = (int)component.Id };
}
}
}

View File

@@ -0,0 +1,28 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1.Responses;
using PARR.Common.Domain;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.MappingProfiles.Resolvers
{
public class HistoryInitiatorUserResolver : IValueResolver<IHistoryInitiator, HistoryInitiatorResponse, UserBaseResponse?>
{
private readonly IUserService userService;
public HistoryInitiatorUserResolver(IUserService userService)
{
this.userService = userService;
}
public UserBaseResponse? Resolve(IHistoryInitiator source, HistoryInitiatorResponse destination, UserBaseResponse? destMember, ResolutionContext context)
{
var user = userService.Get().FirstOrDefaultAsync(t => t.Ip == source.InitiatorIp).GetAwaiter().GetResult();
if (user == null)
return null;
return new UserBaseResponse { Ip = user.Ip, Description = user.Description ?? string.Empty, Name = user.Name };
}
}
}

View File

@@ -87,6 +87,7 @@ namespace PARR.DAL
services.AddTransient<IJobAutoControlService, JobAutoControlService>();
services.AddTransient<IJobEkMaskService, JobEkMaskService>();
services.AddTransient<ITemplateHistoryService, TemplateHistoryService>();
services.AddTransient<IParrComponentService, ParrComponentService>();
// TransformServices

View File

@@ -0,0 +1,21 @@
using PARR.DAL.Context;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.DAL.Services.Implementations
{
internal class ParrComponentService : IParrComponentService
{
private readonly DataContext dataContext;
public ParrComponentService(DataContext dataContext)
{
this.dataContext = dataContext;
}
public IQueryable<ParrComponent> Get()
{
return dataContext.ParrComponents;
}
}
}

View File

@@ -0,0 +1,9 @@
using PARR.DAL.Models;
namespace PARR.DAL.Services.Interfaces
{
public interface IParrComponentService
{
IQueryable<ParrComponent> Get();
}
}