2023-10-11 15:28:37 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using FluentValidation;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-10-11 15:28:37 +10:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
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.API.Services.Interfaces;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using PARR.Constants;
|
2023-10-11 15:28:37 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Контроллер по управлению ScheduleEsppId у шаблона
|
|
|
|
|
|
/// </summary>
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
|
2023-10-11 15:28:37 +10:00
|
|
|
|
public class TemplateScheduleEspp : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ITemplateService templateService;
|
|
|
|
|
|
private readonly IMapper mapper;
|
|
|
|
|
|
private readonly IValidator<TemplateScheduleEsppIdRequest> validator;
|
|
|
|
|
|
private readonly IUriService uriService;
|
|
|
|
|
|
|
|
|
|
|
|
public TemplateScheduleEspp(
|
|
|
|
|
|
ITemplateService templateService,
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IValidator<TemplateScheduleEsppIdRequest> validator,
|
|
|
|
|
|
IUriService uriService
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.templateService = templateService;
|
|
|
|
|
|
this.mapper = mapper;
|
|
|
|
|
|
this.validator = validator;
|
|
|
|
|
|
this.uriService = uriService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Получить ИД расписания ЕСПП по ИД шаблона
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.TemplateScheduleEspp.Get)]
|
|
|
|
|
|
public async Task<IActionResult> Get([FromRoute] Guid templateId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var template = await templateService.GetAsync(templateId);
|
|
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<TemplateScheduleEsppIdResponse>(mapper.Map<TemplateScheduleEsppIdResponse>(template), true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Задать ИД расписания ЕСПП шаблону с ИД
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost(ApiRoutes.TemplateScheduleEspp.Create)]
|
|
|
|
|
|
public async Task<IActionResult> SetEsppId([FromRoute] Guid templateId, [FromBody] TemplateScheduleEsppIdRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resultValidate = await validator.ValidateAsync(request);
|
|
|
|
|
|
if (!resultValidate.IsValid)
|
|
|
|
|
|
return BadRequest(new Response(resultValidate.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
var template = await templateService.GetAsync(templateId);
|
|
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
template.ScheduleEsppId = request.ScheduleEsppId;
|
|
|
|
|
|
|
|
|
|
|
|
if (!await templateService.CommitAsync())
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при сохранении шаблона." } }));
|
|
|
|
|
|
|
|
|
|
|
|
var createdUri = uriService.GetUri(ApiRoutes.TemplateScheduleEspp.Get, ApiRoutes.TemplateScheduleEspp.templateId, templateId);
|
|
|
|
|
|
|
|
|
|
|
|
return Created(createdUri, new Response<TemplateScheduleEsppIdResponse>(mapper.Map<TemplateScheduleEsppIdResponse>(template), true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|