using AutoMapper; using FluentValidation; using Microsoft.AspNetCore.Authorization; 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; using PARR.API.Controllers.V1.Base; using PARR.API.Extensions; using PARR.API.Services.Interfaces; using PARR.Constants; using PARR.DAL.DomainModels; using PARR.DAL.Models; using PARR.DAL.Services.Interfaces; namespace PARR.API.Controllers.V1 { [Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)] public class RobotHistoryController : BaseApiController { private readonly IValidator validator; private readonly IMapper mapper; private readonly IUriService uriService; private readonly IRobotHistoryService robotHistoryService; private readonly IRobotConfigurationService robotConfigurationService; public RobotHistoryController( IValidator validator, IMapper mapper, IUriService uriService, IRobotHistoryService robotHistoryService, IRobotConfigurationService robotConfigurationService ) { this.validator = validator; this.mapper = mapper; this.uriService = uriService; this.robotHistoryService = robotHistoryService; this.robotConfigurationService = robotConfigurationService; } /// /// Просмотр истории работы робота /// /// /// /// [HttpGet(ApiRoutes.RobotHistory.GetAll)] public async Task GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] RobotHistoryQuery request) { var paginationFilter = mapper.Map(paginationQuery); IQueryable query = robotHistoryService.Get() .Include(t => t.RobotConfiguration) .ThenInclude(t => t!.Template) .Include(t => t.RobotHistoryLevel) .Include(t => t.StatusTask) .OrderByDescending(t => t.DateCreated); if (request.TemplateId.HasValue) query = query.Where(t => t.RobotConfiguration!.TemplateId == request.TemplateId.Value); if (request.RobotCode.HasValue) query = query.Where(t => t.RobotConfiguration!.RobotCode == (int)request.RobotCode); if (request.HistoryLevel.HasValue) query = query.Where(t => t.HistoryLevel == (int)request.HistoryLevel); var history = await robotHistoryService.GetPage(query, paginationFilter).ToListAsync(); if (!history.Any()) return NoContent(); var response = mapper.Map>(history); var paginationResponse = new PagedResponse(response, true).GetPaginatedProps(paginationFilter, query); return Ok(paginationResponse); } /// /// Добавить историю работы робота /// /// /// [HttpPost(ApiRoutes.RobotHistory.Create)] public async Task 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 { 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 { new ErrorModel { Message = $"Ошибка при добавлении записи в историю" } })); var createdObj = await robotHistoryService.Get() .Include(t => t.RobotConfiguration) .ThenInclude(t => t!.Template) .Include(t => t.RobotHistoryLevel) .Include(t => t.StatusTask) .FirstOrDefaultAsync(t => t.Id == history.Id); var response = mapper.Map(createdObj); //todo: createdUri //var createdUri = uriService.GetAllUri(ApiRoutes.RobotTemplateHistory.GetAll) + $"?{nameof(RobotTemplateHistoryQuery.IdSeries)}={createdObj!.IdSeries}"; var createdUri = ""; return Created(createdUri, new Response(response, true)); } } }