feat(api): Обновил логику получения шаблона по статусу, согласоно статусам отработки робота. TemplateRobotStatusController - управление статусами робота в шаблоне
This commit is contained in:
109
PARR.API/Controllers/V1/TemplateRobotStatusController.cs
Normal file
109
PARR.API/Controllers/V1/TemplateRobotStatusController.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
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;
|
||||
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}" } }));
|
||||
|
||||
switch (request.Code)
|
||||
{
|
||||
case (int)RobotStatusEnum.InProgress:
|
||||
template.RobotAttemptsNumber++;
|
||||
template.RobotStatusCode = request.Code;
|
||||
template.RobotLastStatusUpdated = DateTimeOffset.UtcNow;
|
||||
break;
|
||||
case (int)RobotStatusEnum.Error:
|
||||
template.RobotStatusCode = request.Code;
|
||||
break;
|
||||
case (int)RobotStatusEnum.Complete:
|
||||
template.RobotStatusCode = request.Code;
|
||||
template.RobotLastStatusUpdated = DateTimeOffset.UtcNow;
|
||||
break;
|
||||
case (int)RobotStatusEnum.Wait:
|
||||
template.RobotStatusCode = request.Code;
|
||||
template.RobotLastStatusUpdated = null;
|
||||
template.RobotAttemptsNumber = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user