2023-10-19 11:22:35 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using FluentValidation;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-10-19 11:22:35 +10:00
|
|
|
|
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;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using PARR.Constants;
|
2023-10-19 11:22:35 +10:00
|
|
|
|
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;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
private readonly ITemplateService templateService;
|
|
|
|
|
|
private readonly IClientService clientService;
|
|
|
|
|
|
private readonly ILogger<AgentHistoryController> logger;
|
2023-10-19 11:22:35 +10:00
|
|
|
|
|
|
|
|
|
|
public AgentHistoryController(
|
|
|
|
|
|
IValidator<AgentHistoryRequest> validator,
|
|
|
|
|
|
IAgentHistoryService agentHistoryService,
|
|
|
|
|
|
IUriService uriService,
|
2023-12-04 10:16:00 +10:00
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
ITemplateService templateService,
|
|
|
|
|
|
IClientService clientService,
|
|
|
|
|
|
ILogger<AgentHistoryController> logger
|
2023-10-19 11:22:35 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.validator = validator;
|
|
|
|
|
|
this.agentHistoryService = agentHistoryService;
|
|
|
|
|
|
this.uriService = uriService;
|
|
|
|
|
|
this.mapper = mapper;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
this.templateService = templateService;
|
|
|
|
|
|
this.clientService = clientService;
|
|
|
|
|
|
this.logger = logger;
|
2023-10-19 11:22:35 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Получить историю работы агента постранично
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
2023-10-19 11:22:35 +10:00
|
|
|
|
[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()
|
2023-12-27 08:50:45 +10:00
|
|
|
|
.Include(t => t.AgentHistoryLevel)
|
|
|
|
|
|
.Include(t => t.Template);
|
2023-10-19 11:22:35 +10:00
|
|
|
|
|
|
|
|
|
|
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>
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
2023-10-19 11:22:35 +10:00
|
|
|
|
[HttpGet(ApiRoutes.AgentHistory.Get)]
|
|
|
|
|
|
public async Task<IActionResult> GetById([FromRoute] Guid id)
|
|
|
|
|
|
{
|
2023-12-27 08:50:45 +10:00
|
|
|
|
var history = await agentHistoryService.Get()
|
|
|
|
|
|
.Include(t => t.AgentHistoryLevel)
|
|
|
|
|
|
.Include(t => t.Template)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == id);
|
2023-10-19 11:22:35 +10:00
|
|
|
|
|
|
|
|
|
|
if (history == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
var response = mapper.Map<AgentHistoryResponse>(history);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<AgentHistoryResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Добавить запись в историю работы агента
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.Agent.RoleOrAdmin)]
|
2023-10-19 11:22:35 +10:00
|
|
|
|
[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));
|
|
|
|
|
|
|
2023-12-04 10:16:00 +10:00
|
|
|
|
|
|
|
|
|
|
var clientIp = clientService.GetClientIp()?.ToString();
|
|
|
|
|
|
var template = await templateService.Get().Include(t => t.Host).FirstOrDefaultAsync(t => t.Id == request.TemplateId);
|
|
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { FieldName = nameof(request.TemplateId), Message = $"Не найден шаблон с Id: {request.TemplateId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
//проверять что этот этот шаблон привязан к этому серверу по ip
|
|
|
|
|
|
if (template.Host?.IP != clientIp)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning($"Клиент с ip: {clientIp} пытается записать историю агента для templateId: {request.TemplateId}, " +
|
|
|
|
|
|
$"но у шаблона ip: {template.Host?.IP}, доступ запрещен так как их ip не равны.");
|
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
}
|
2023-10-19 11:22:35 +10:00
|
|
|
|
|
|
|
|
|
|
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 = "Ошибка при добавлении записи в историю работы агента." } }));
|
|
|
|
|
|
|
2023-12-27 08:50:45 +10:00
|
|
|
|
var createdObject = await agentHistoryService.Get().Include(t => t.AgentHistoryLevel).Include(t => t.Template).FirstAsync(t => t.Id == agentJournal.Id);
|
2023-10-19 11:22:35 +10:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|