2023-10-04 12:00:43 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
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.DAL.Contracts;
|
2023-10-04 13:41:33 +10:00
|
|
|
|
using PARR.DAL.Models;
|
2023-10-04 12:00:43 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TemplateRobotStatusController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper mapper;
|
|
|
|
|
|
private readonly ITemplateService templateService;
|
|
|
|
|
|
private readonly IValidator<TemplateRobotStatusRequest> validator;
|
|
|
|
|
|
|
|
|
|
|
|
public TemplateRobotStatusController(
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
ITemplateService templateService,
|
|
|
|
|
|
IValidator<TemplateRobotStatusRequest> validator
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.mapper = mapper;
|
|
|
|
|
|
this.templateService = templateService;
|
|
|
|
|
|
this.validator = validator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Получить статус робота по Id шаблона
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.TemplateRobotStatus.Get)]
|
|
|
|
|
|
public async Task<IActionResult> Get([FromRoute] Guid templateId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var template = await templateService.Get()
|
|
|
|
|
|
.Include(t => t.RobotStatus)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == templateId);
|
|
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
var response = mapper.Map<TemplateRobotStatusResponse>(template);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<TemplateRobotStatusResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Обновить статус робота по Id шаблона
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPut(ApiRoutes.TemplateRobotStatus.Update)]
|
|
|
|
|
|
public async Task<IActionResult> UpdateStatus([FromRoute] Guid templateId, [FromBody] TemplateRobotStatusRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resultValidate = await validator.ValidateAsync(request);
|
|
|
|
|
|
if (!resultValidate.IsValid)
|
|
|
|
|
|
return BadRequest(new Response(resultValidate.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
var template = await templateService.Get()
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == templateId);
|
|
|
|
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));
|
|
|
|
|
|
|
2023-10-04 13:41:33 +10:00
|
|
|
|
var enumStatus = (RobotStatusEnum)request.Code;
|
|
|
|
|
|
|
|
|
|
|
|
// изменение статуса
|
|
|
|
|
|
templateService.ChangeRobotStatus(enumStatus, ref template);
|
|
|
|
|
|
|
2023-10-04 12:00:43 +10:00
|
|
|
|
if (!await templateService.CommitAsync())
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при изменении статуса шаблону {templateId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
var templateToResponse = await templateService.Get()
|
|
|
|
|
|
.Include(t => t.RobotStatus)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == templateId);
|
|
|
|
|
|
|
|
|
|
|
|
var response = mapper.Map<TemplateRobotStatusResponse>(templateToResponse);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<TemplateRobotStatusResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|