using AutoMapper;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.Core.Repositories.Interfaces.JobGroupRepositories;
using PARR.Core.Repositories.Interfaces.Unit;
using PARR.Core.Services.UnitFilterService;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.JobEntities;
namespace PARR.API.Controllers.V1
{
///
/// Получение предварительной информации о работах
///
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class JobUnitPreviewController : BaseApiController
{
private readonly ILogger logger;
private readonly IUnitFilterService unitFilterService;
//private readonly IValidator jobValidator;
private readonly IUnitRepository unitService;
private readonly IJobGroupRepository jobGroupService;
private readonly IMapper mapper;
public JobUnitPreviewController(
ILogger logger,
IUnitFilterService unitFilterService,
//IValidator jobValidator,
IUnitRepository unitService,
IJobGroupRepository jobGroupService,
IMapper mapper
)
{
this.logger = logger;
this.unitFilterService = unitFilterService;
//this.jobValidator = jobValidator;
this.unitService = unitService;
this.jobGroupService = jobGroupService;
this.mapper = mapper;
}
///
/// Получить список ЭК по предполагаемой работе перед сохранением
///
///
///
[HttpPost(ApiRoutes.JobUnitPreview.Preview)]
public async Task Preview([FromBody] JobRequest request)
{
//#region Валидация
//var jobValidateResult = await jobValidator.ValidateAsync(request);//Валидация параметров самого задания
//if (!jobValidateResult.IsValid)
// return BadRequest(new Response(jobValidateResult.Errors));
//#endregion
var job = mapper.Map(request);
var jobGroup = await jobGroupService.Get().AsNoTracking()
.Include(t => t.GroupType)
.FirstAsync(t => t.Id == job.GroupId);
job.Group = jobGroup;
var unitIds = await unitFilterService.GetUnitsIdByJobFilterAsync(job, 100);
if (unitIds == null || !unitIds.Any())
return NoContent();
var query = unitService.Get().Where(t => unitIds.Any(x => x == t.Id)).OrderBy(o => o.Name);
var units = await query.ToListAsync();
if (!units.Any())
return NoContent();
var unitResponse = mapper.Map>(units);
return Ok(new Response>(unitResponse, true));
}
}
}