feat(api): Изменение состояния у шаблона/расписания (акивировано/деактивировано)

This commit is contained in:
Mikhail Trubnikov
2023-10-13 14:21:49 +10:00
parent b177c0cc27
commit f69be40ad3
10 changed files with 148 additions and 40 deletions

View File

@@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests;
using PARR.API.Contracts.V1.Requests.Queries;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
@@ -20,16 +21,19 @@ namespace PARR.API.Controllers.V1
private readonly IMapper mapper;
private readonly ITemplateService templateService;
private readonly SettingsFromDb settingsFromDb;
private readonly IRobotConfigurationService robotConfigurationService;
public TemplateController(
IMapper mapper,
ITemplateService templateService,
SettingsFromDb settingsFromDb
SettingsFromDb settingsFromDb,
IRobotConfigurationService robotConfigurationService
)
{
this.mapper = mapper;
this.templateService = templateService;
this.settingsFromDb = settingsFromDb;
this.robotConfigurationService = robotConfigurationService;
}
@@ -44,7 +48,7 @@ namespace PARR.API.Controllers.V1
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
IQueryable<Template> query = templateService.GetWithIncludes()
.Include(t => t.RobotConfigurations).ThenInclude(t=>t.Robot)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
.OrderBy(t => t.Name)
@@ -88,6 +92,56 @@ namespace PARR.API.Controllers.V1
return Ok(new Response<TemplateResponse>(response, true));
}
/// <summary>
/// Изменить статус у шаблона по ИД (активировать/деактивировать)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPut(ApiRoutes.Template.ChangeState)]
public async Task<IActionResult> ChangeState([FromRoute] Guid id, [FromBody] TemplateChangeStateRequest request)
{
var template = await templateService.GetWithIncludes()
.Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
.AsSplitQuery()
.FirstOrDefaultAsync(t => t.Id == id);
if (template == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблона с id: {id}" } }));
if (template.IsActiveTemplate != request.IsActiveTemplate)
{
template.IsActiveTemplate = request.IsActiveTemplate;
//необходимо обновить шаблон
var config = robotConfigurationService.GetFromTemplateByRobotCode(RobotsEnum.TemplateOrder, ref template);
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, ref config);
}
if (template.IsActiveSchedule != request.IsActiveSchedule)
{
template.IsActiveSchedule = request.IsActiveSchedule;
//необходимо обновить расписание
var config = robotConfigurationService.GetFromTemplateByRobotCode(RobotsEnum.ScheduleOrder, ref template);
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, ref config);
}
if (!await templateService.CommitAsync())
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при изменении шаблона." } }));
var templateToResponse = await templateService.GetWithIncludes()
.Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
.AsSplitQuery()
.FirstOrDefaultAsync(t => t.Id == id);
var response = mapper.Map<TemplateResponse>(templateToResponse);
return Ok(new Response<TemplateResponse>(response, true));
}
/// <summary>
/// Получить один шаблон по заданному статусу
/// </summary>