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

@@ -56,6 +56,7 @@
{
public const string GetAll = Base + "/templates/";
public const string Get = Base + "/templates/" + getParam;
public const string ChangeState = Base + "/templates/" + getParam + "/change-state";
//public const string SetOkStatus = Base + "/templates/" + getParam + "/status/ok";
//public const string GetByStatusCode = Base + "/templates/status/{statusCode}";

View File

@@ -0,0 +1,15 @@
namespace PARR.API.Contracts.V1.Requests
{
public class TemplateChangeStateRequest
{
/// <summary>
/// Активировать шаблон
/// </summary>
public bool IsActiveTemplate { get; set; }
/// <summary>
/// Активировать расписание
/// </summary>
public bool IsActiveSchedule { get; set; }
}
}

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>

View File

@@ -115,6 +115,7 @@ namespace PARR.API.MappingProfiles
.ForMember(d => d.RepeatRange, o => o.MapFrom<RobotTaskScheduleRepeatRangeResolver>())
.ForMember(d => d.GenerationTime, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.ScheduleGenerationTime))
.ForMember(d => d.NextStart, o => o.MapFrom(s => EsppScheduleHelpers.GetFirstStart(s.Template!.ApplicationsInWork!.ScheduleStartDate, s.Template!.ApplicationsInWork!.ScheduleGenerationTime)))
//.ForMember(d => d.NextStart, o => o.MapFrom(s => EsppScheduleHelpers.GetNextScheduleStart(s.Template!.NextScheduleStart)))
.ForMember(d => d.ScheduleType, o => o.MapFrom(s =>
s.Template!.ApplicationsInWork!.EsppSchValues!.First()!.EsppSchTypeConfig!.EsppSchTypeSchedule!.Description))
.ForMember(d => d.ScheduleTypeCode, o => o.MapFrom(s =>