feat(api): Новый контроллер JobUnitPreview, для получения предпросмотра затрагиваемых ЭК работой
This commit is contained in:
76
PARR.API/Controllers/V1/JobUnitPreviewController.cs
Normal file
76
PARR.API/Controllers/V1/JobUnitPreviewController.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
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.Constants;
|
||||
using PARR.DAL.DomainServices.Interfaces;
|
||||
using PARR.DAL.Models.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
|
||||
namespace PARR.API.Controllers.V1
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение предварительной информации о работах
|
||||
/// </summary>
|
||||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||||
public class JobUnitPreviewController : BaseApiController
|
||||
{
|
||||
private readonly ILogger<JobUnitPreviewController> logger;
|
||||
private readonly IUnitFilterService unitFilterService;
|
||||
private readonly IValidator<JobRequest> jobValidator;
|
||||
private readonly IUnitService unitService;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public JobUnitPreviewController(
|
||||
ILogger<JobUnitPreviewController> logger,
|
||||
IUnitFilterService unitFilterService,
|
||||
IValidator<JobRequest> jobValidator,
|
||||
IUnitService unitService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.unitFilterService = unitFilterService;
|
||||
this.jobValidator = jobValidator;
|
||||
this.unitService = unitService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить список ЭК по предполагаемой работе перед сохранением
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost(ApiRoutes.JobUnitPreview.Preview)]
|
||||
public async Task<IActionResult> 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<Job>(request);
|
||||
|
||||
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();
|
||||
|
||||
var unitResponse = mapper.Map<List<UnitBaseResponse>>(units);
|
||||
|
||||
return Ok(new Response<List<UnitBaseResponse>>(unitResponse, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user