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.DAL.Models;
using PARR.DAL.Services.Interfaces;
using PARR.Domain.Common.Pagination;
using PARR.Domain.Common.Roles;
namespace PARR.API.Controllers.V1
{
///
/// История изменения шаблонов
///
[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;
}
///
/// Получить историю изменения шаблонов постранично
///
///
[HttpGet(ApiRoutes.TemplateHistory.GetAll)]
public async Task GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] TemplateHistoryQuery requestQuery)
{
var paginationFilter = mapper.Map(paginationQuery);
IQueryable 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>(history);
var paginationResponse = new PagedResponse(historyResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
}
}