using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
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.DAL.DomainModels;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using static PARR.API.Contracts.V1.ApiRoutes;
namespace PARR.API.Controllers.V1
{
public class JobController : BaseApiController
{
private readonly IMapper mapper;
private readonly IJobService jobService;
//private readonly ISchedulerService schedulerService;
private readonly IClientService clientService;
private readonly IHostService hostService;
public JobController(
IMapper mapper,
IJobService jobService,
IClientService clientService,
IHostService hostService
)
{
this.mapper = mapper;
this.jobService = jobService;
//this.schedulerService = schedulerService;
this.clientService = clientService;
this.hostService = hostService;
}
///
/// Список всех jobов постранично в соотвествии с фильтрами
///
///
///
[HttpGet(ApiRoutes.Jobs.GetAll)]
public async Task GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] JobGetAllQuery filter)
{
var paginationFilter = mapper.Map(paginationQuery);
IQueryable query = jobService.Get().Include(t => t.JobMode).OrderBy(s => s.StartAt);
if (filter.IsEnabled.HasValue)
query = query.Where(t => t.IsEnabled == filter.IsEnabled);
if (filter.StartDatePlanned.HasValue)
query = query.Where(t => t.StartAt.DateTime.Date <= filter.StartDatePlanned.Value.DateTime.Date); ;
var jobs = await jobService.GetPage(query, paginationFilter).ToListAsync();
if (!jobs.Any())
return NoContent();
var jobResponse = mapper.Map>(jobs);
var paginationResponse = new PagedResponse(jobResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
[HttpGet(ApiRoutes.Jobs.GetByIp)]
public async Task GetByClientIP([FromQuery] JobGetByIpQuery requestQuery)
{
var ip = requestQuery.Ip ?? clientService.GetClientIp()?.ToString();
if (string.IsNullOrEmpty(ip))
return BadRequest(new Response(false, new List { new ErrorModel { Message = "Client IP address is null." } }));
var date = requestQuery.Date ?? DateTimeOffset.Now;
// С одним IP может быть несколько информационных систем, соответственного а таблице храниться несколько записей хостов с одинаковым IP
var hosts = await hostService.Get().Where(h => h.IP == ip).ToListAsync();
var hostJobsDict = new Dictionary>();
foreach (var host in hosts)
{
var jobsHost = await jobService.Get()
.Include(a => a.Application).ThenInclude(ah => ah.ApplicationsInHosts)/*.ThenInclude(h => h.Host)*/
.Include(jm=>jm.JobMode)
.AsSplitQuery()
.Where(j => j.Application != null && j.Application.ApplicationsInHosts.Any(ah => ah.HostId == host.Id))
.ToListAsync();
if (jobsHost.Any())
hostJobsDict.Add(host, jobsHost);
}
//hostJobsDict.Values.Distinct();
var jobsJoined = new List();
foreach(var job in hostJobsDict.Values)
{
job.ForEach(item =>
{
jobsJoined.Add(item);
});
}
jobsJoined.Distinct();
if (!jobsJoined.Any())
return NoContent();
var jobResponse = mapper.Map>(jobsJoined);
return Ok(new Response>(jobResponse, true, new List(), $"{ip},{date}"));
}
}
}