feat(api): AgentTask, AgentHistory
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace PARR.API.Contracts.V1
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace PARR.API.Contracts.V1
|
||||
{
|
||||
// https://tproger.ru/translations/luchshie-praktiki-razrabotki-rest-api-20-sovetov/
|
||||
|
||||
@@ -112,6 +114,15 @@
|
||||
public const string taskId = "{taskId}";
|
||||
}
|
||||
|
||||
public static class AgentHistory
|
||||
{
|
||||
public const string GetAll = Base + "/agent-histories/";
|
||||
public const string Get = Base + "/agent-histories/" + getParam;
|
||||
public const string Create = Base + "/agent-histories/";
|
||||
|
||||
public const string getParam = "{id}";
|
||||
}
|
||||
|
||||
|
||||
//public static class Layer
|
||||
//{
|
||||
|
||||
22
PARR.API/Contracts/V1/Requests/AgentHistoryRequest.cs
Normal file
22
PARR.API/Contracts/V1/Requests/AgentHistoryRequest.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using PARR.DAL.Contracts;
|
||||
|
||||
namespace PARR.API.Contracts.V1.Requests
|
||||
{
|
||||
public class AgentHistoryRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// ИД шаблона
|
||||
/// </summary>
|
||||
public Guid TemplateId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Сообщение
|
||||
/// </summary>
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Уровень истории (1 - Start, 5 - End)
|
||||
/// </summary>
|
||||
public AgentHistoryLevelEnum Level { get; set; }
|
||||
}
|
||||
}
|
||||
10
PARR.API/Contracts/V1/Requests/Queries/AgentHistoryQuery.cs
Normal file
10
PARR.API/Contracts/V1/Requests/Queries/AgentHistoryQuery.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PARR.API.Contracts.V1.Requests.Queries
|
||||
{
|
||||
public class AgentHistoryQuery
|
||||
{
|
||||
/// <summary>
|
||||
/// Ид шаблона
|
||||
/// </summary>
|
||||
public Guid? TemplateId { get; set; }
|
||||
}
|
||||
}
|
||||
11
PARR.API/Contracts/V1/Responses/AgentHistoryLevelResponse.cs
Normal file
11
PARR.API/Contracts/V1/Responses/AgentHistoryLevelResponse.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PARR.API.Contracts.V1.Responses
|
||||
{
|
||||
public class AgentHistoryLevelResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public required string Name { get; set; }
|
||||
|
||||
public required string Description { get; set; }
|
||||
}
|
||||
}
|
||||
15
PARR.API/Contracts/V1/Responses/AgentHistoryResponse.cs
Normal file
15
PARR.API/Contracts/V1/Responses/AgentHistoryResponse.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace PARR.API.Contracts.V1.Responses
|
||||
{
|
||||
public class AgentHistoryResponse
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
public string? Message { get; set; }
|
||||
|
||||
public Guid TemplateId { get; set; }
|
||||
|
||||
public AgentHistoryLevelResponse? Level { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -16,5 +16,7 @@
|
||||
public required string Script { get; set; }
|
||||
|
||||
public required string Name { get; set; }
|
||||
|
||||
public Guid TemplateId { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
121
PARR.API/Controllers/V1/AgentHistoryController.cs
Normal file
121
PARR.API/Controllers/V1/AgentHistoryController.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using AutoMapper;
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.API.Contracts.V1;
|
||||
using PARR.API.Contracts.V1.Requests;
|
||||
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.API.Services.Interfaces;
|
||||
using PARR.DAL.DomainModels;
|
||||
using PARR.DAL.Models;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Controllers.V1
|
||||
{
|
||||
/// <summary>
|
||||
/// История работы агента
|
||||
/// </summary>
|
||||
public class AgentHistoryController : BaseApiController
|
||||
{
|
||||
private readonly IValidator<AgentHistoryRequest> validator;
|
||||
private readonly IAgentHistoryService agentHistoryService;
|
||||
private readonly IUriService uriService;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public AgentHistoryController(
|
||||
IValidator<AgentHistoryRequest> validator,
|
||||
IAgentHistoryService agentHistoryService,
|
||||
IUriService uriService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.validator = validator;
|
||||
this.agentHistoryService = agentHistoryService;
|
||||
this.uriService = uriService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить историю работы агента постранично
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.AgentHistory.GetAll)]
|
||||
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] AgentHistoryQuery request)
|
||||
{
|
||||
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
||||
|
||||
IQueryable<AgentHistory> query = agentHistoryService.Get()
|
||||
.Include(t => t.AgentHistoryLevel);
|
||||
|
||||
if (request.TemplateId.HasValue)
|
||||
query = query.Where(t => t.TemplateId == request.TemplateId);
|
||||
|
||||
var history = await agentHistoryService.GetPage(query.OrderByDescending(t => t.DateCreated), paginationFilter).ToListAsync();
|
||||
|
||||
if (!history.Any())
|
||||
return NoContent();
|
||||
|
||||
var response = mapper.Map<List<AgentHistoryResponse>>(history);
|
||||
var paginationResponse = new PagedResponse<AgentHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
|
||||
|
||||
return Ok(paginationResponse);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить запись истории работы агента по id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.AgentHistory.Get)]
|
||||
public async Task<IActionResult> GetById([FromRoute] Guid id)
|
||||
{
|
||||
var history = await agentHistoryService.Get().Include(t => t.AgentHistoryLevel).FirstAsync(t => t.Id == id);
|
||||
|
||||
if (history == null)
|
||||
return NotFound();
|
||||
|
||||
var response = mapper.Map<AgentHistoryResponse>(history);
|
||||
|
||||
return Ok(new Response<AgentHistoryResponse>(response, true));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Добавить запись в историю работы агента
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost(ApiRoutes.AgentHistory.Create)]
|
||||
public async Task<IActionResult> Create([FromBody] AgentHistoryRequest request)
|
||||
{
|
||||
var resultValidate = await validator.ValidateAsync(request);
|
||||
if (!resultValidate.IsValid)
|
||||
return BadRequest(new Response(resultValidate.Errors));
|
||||
|
||||
//todo: проверять что этот этот шаблон привязан к этому серверу по ip???
|
||||
|
||||
var agentJournal = new AgentHistory
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Message = request.Message,
|
||||
HistoryLevelId = (int)request.Level,
|
||||
TemplateId = request.TemplateId
|
||||
};
|
||||
|
||||
if (!await agentHistoryService.CreateAsync(agentJournal) || !await agentHistoryService.CommitAsync())
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при добавлении записи в историю работы агента." } }));
|
||||
|
||||
var createdObject = await agentHistoryService.Get().Include(t => t.AgentHistoryLevel).FirstAsync(t => t.Id == agentJournal.Id);
|
||||
var response = mapper.Map<AgentHistoryResponse>(createdObject);
|
||||
|
||||
var createdUri = uriService.GetUri(ApiRoutes.AgentHistory.Get, ApiRoutes.AgentHistory.getParam, agentJournal.Id);
|
||||
|
||||
return Created(createdUri, new Response<AgentHistoryResponse>(response, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,13 @@ namespace PARR.API.Controllers.V1
|
||||
continue;
|
||||
}
|
||||
|
||||
templateSchedule.ForEach(item => response.Scheduled.Add(new AgentTaskMinScheduleResponse { Name = template.ApplicationsInWork!.AgentName, Script = template.ApplicationsInWork!.AgentScript, StartAt = item }));
|
||||
templateSchedule.ForEach(item => response.Scheduled.Add(new AgentTaskMinScheduleResponse
|
||||
{
|
||||
Name = template.ApplicationsInWork!.AgentName,
|
||||
Script = template.ApplicationsInWork!.AgentScript,
|
||||
StartAt = item,
|
||||
TemplateId = template.Id
|
||||
}));
|
||||
}
|
||||
|
||||
if (!response.Scheduled.Any())
|
||||
|
||||
@@ -142,6 +142,12 @@ namespace PARR.API.MappingProfiles
|
||||
.ForMember(d => d.Type, o => o.MapFrom(s => s.Type));
|
||||
// === EsppSch ===
|
||||
|
||||
CreateMap<AgentHistoryLevel, AgentHistoryLevelResponse>();
|
||||
|
||||
CreateMap<AgentHistory, AgentHistoryResponse>()
|
||||
.ForMember(t => t.Date, o => o.MapFrom(s => s.DateCreated))
|
||||
.ForMember(t => t.Level, o => o.MapFrom(s => s.AgentHistoryLevel));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
PARR.API/Validators/AgentHistoryRequestValidator.cs
Normal file
18
PARR.API/Validators/AgentHistoryRequestValidator.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using FluentValidation;
|
||||
using PARR.API.Contracts.V1.Requests;
|
||||
using PARR.DAL.Contracts;
|
||||
|
||||
namespace PARR.API.Validators
|
||||
{
|
||||
public class AgentHistoryRequestValidator : AbstractValidator<AgentHistoryRequest>
|
||||
{
|
||||
public AgentHistoryRequestValidator()
|
||||
{
|
||||
RuleFor(t => t.TemplateId).NotEmpty().NotNull();
|
||||
|
||||
RuleFor(t => t.Level)
|
||||
.Must(t => t == AgentHistoryLevelEnum.Start || t == AgentHistoryLevelEnum.End)
|
||||
.WithMessage($"Допустимые значения: {(int)AgentHistoryLevelEnum.Start}, {(int)AgentHistoryLevelEnum.End}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user