Files
parr_api/PARR.API/Controllers/V1/RobotTaskRobotStatusController.cs

87 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
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.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
public class RobotTaskRobotStatusController : BaseApiController
{
private readonly IRobotConfigurationService robotConfigurationService;
private readonly IRobotHistoryService robotHistoryService;
private readonly IMapper mapper;
public RobotTaskRobotStatusController(
IRobotConfigurationService robotConfigurationService,
IRobotHistoryService robotHistoryService,
IMapper mapper
)
{
this.robotConfigurationService = robotConfigurationService;
this.robotHistoryService = robotHistoryService;
this.mapper = mapper;
}
/// <summary>
/// Изменить статус выполнения задания роботом по ИД задания
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
[HttpPut(ApiRoutes.RobotTaskRobotStatus.ChangeRobotStatus)]
public async Task<IActionResult> ChangeStatus([FromRoute] Guid taskId, [FromBody] RobotTaskChangeRobotStatusRequest request)
{
var config = await robotConfigurationService.Get()
.FirstOrDefaultAsync(t => t.Id == taskId);
if (config == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найдено задание с id: {taskId}" } }));
//изменение статуса робота
robotConfigurationService.ChangeRobotStatus(request.RobotStatusCode, ref config);
//если успех, изменяем статус задания на успех
if (request.RobotStatusCode == RobotStatusEnum.Complete)
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Ok, ref config);
if (!await robotConfigurationService.CommitAsync())
return BadRequest("Ошибка при изменении статуса работы робота.");
//записываем в лог робота
if (request.RobotStatusCode == RobotStatusEnum.InProgress || request.RobotStatusCode == RobotStatusEnum.Complete)
{
var historyLevel = request.RobotStatusCode == RobotStatusEnum.InProgress ? RobotHistoryLevelEnum.Start : RobotHistoryLevelEnum.Complete;
var history = new RobotHistory
{
Id = Guid.NewGuid(),
HistoryLevel = (int)historyLevel,
TaskStatusCode = config.TaskStatusCode,
RobotConfigurationId = config.Id
};
await robotHistoryService.CreateAsync(history);
await robotHistoryService.CommitAsync();
}
var configToResonse = await robotConfigurationService.Get()
.Include(t => t.Robot)
.Include(t => t.TaskStatus)
.Include(t => t.RobotStatus)
.FirstOrDefaultAsync(t => t.Id == taskId);
var response = mapper.Map<RobotConfigurationResponse>(configToResonse);
return Ok(new Response<RobotConfigurationResponse>(response, true));
}
}
}