feat(api): RobotHistoryController - сохранение истории работы робота

This commit is contained in:
Mikhail Trubnikov
2023-10-06 13:58:10 +10:00
parent d334e46780
commit 9060db1fe4
14 changed files with 2228 additions and 187 deletions

View File

@@ -0,0 +1,126 @@
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.API.Services.Interfaces;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
public class RobotHistoryController : BaseApiController
{
private readonly IValidator<RobotHistoryRequest> validator;
private readonly ITemplateService templateService;
private readonly IRobotTemplateHistoryService historyService;
private readonly IMapper mapper;
private readonly IUriService uriService;
private readonly IRobotHistoryService robotHistoryService;
private readonly IRobotConfigurationService robotConfigurationService;
public RobotHistoryController(
IValidator<RobotHistoryRequest> validator,
ITemplateService templateService,
IRobotTemplateHistoryService historyService,
IMapper mapper,
IUriService uriService,
IRobotHistoryService robotHistoryService,
IRobotConfigurationService robotConfigurationService
)
{
this.validator = validator;
this.templateService = templateService;
this.historyService = historyService;
this.mapper = mapper;
this.uriService = uriService;
this.robotHistoryService = robotHistoryService;
this.robotConfigurationService = robotConfigurationService;
}
///// <summary>
///// Получить историю робота шаблонов
///// </summary>
///// <param name="paginationQuery"></param>
///// <param name="filter"></param>
///// <returns></returns>
//[HttpGet(ApiRoutes.RobotTemplateHistory.GetAll)]
//public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] RobotTemplateHistoryQuery filter)
//{
// var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
// IQueryable<RobotTemplateHistory> query = historyService.Get()
// .Include(t => t.Template)
// .Include(t => t.RobotHistoryLevel)
// .OrderByDescending(t => t.DateCreated).ThenBy(t => t.IdSeries);
// if (filter.IdSeries != null)
// query = query.Where(t => t.IdSeries == filter.IdSeries);
// if (filter.TemplateId != null)
// query = query.Where(t => t.TemplateId == filter.TemplateId);
// var history = await historyService.GetPage(query, paginationFilter).ToListAsync();
// if (!history.Any())
// return NoContent();
// var response = mapper.Map<List<RobotTemplateHistoryResponse>>(history);
// var paginationResponse = new PagedResponse<RobotTemplateHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
// return Ok(paginationResponse);
//}
/// <summary>
/// Добавить историю работы робота
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost(ApiRoutes.RobotHistory.Create)]
public async Task<IActionResult> Create([FromBody] RobotHistoryRequest request)
{
var resultValidate = await validator.ValidateAsync(request);
if (!resultValidate.IsValid)
return BadRequest(new Response(resultValidate.Errors));
var config = await robotConfigurationService.GetAsync(request.TaskId);
if (config == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найдено задание с id: {request.TaskId}" } }));
var history = new RobotHistory
{
Id = Guid.NewGuid(),
HistoryLevel = (int)request.HistoryLevel,
RobotMessage = request.RobotMessage,
EsppMessage = request.EsppMessage,
TaskStatusCode = config.TaskStatusCode,
RobotConfigurationId = request.TaskId
};
if (!await robotHistoryService.CreateAsync(history) || !await robotHistoryService.CommitAsync())
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при добавлении записи в историю" } }));
var createdObj = await robotHistoryService.Get()
.Include(t => t.RobotConfiguration)
.Include(t => t.RobotHistoryLevel)
.FirstOrDefaultAsync(t => t.Id == history.Id);
var response = mapper.Map<RobotHistoryResponse>(createdObj);
//todo: createdUri
//var createdUri = uriService.GetAllUri(ApiRoutes.RobotTemplateHistory.GetAll) + $"?{nameof(RobotTemplateHistoryQuery.IdSeries)}={createdObj!.IdSeries}";
var createdUri = "";
return Created(createdUri, new Response<RobotHistoryResponse>(response, true));
}
}
}