Files
parr_api/PARR.API/Controllers/V1/HostController.cs

59 lines
2.2 KiB
C#

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.BLL.Helpers;
using PARR.DAL.DomainModels;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
public class HostController : BaseApiController
{
private readonly IMapper mapper;
private readonly IHostService hostService;
public HostController(IMapper mapper, IHostService hostService)
{
this.mapper = mapper;
this.hostService = hostService;
}
/// <summary>
/// Получить список хостов постранично
/// </summary>
/// <param name="paginationQuery"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.Host.GetAll)]
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] HostQuery request)
{
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
IQueryable<DAL.Models.Host> query = hostService.Get()
.Include(s => s.EkStatus)
.Include(s => s.ResponseArea)
.Include(t => t.ApplicationsInHosts).ThenInclude(t => t.Application).ThenInclude(t => t!.ApplicationType)
.OrderBy(t => t.Ek);
if (!string.IsNullOrEmpty(request.Mask))
query = query.Where(t => EF.Functions.Like(t.Ek.ToLower(), SqlHelpers.RegexToLike(request.Mask)));
var hosts = await hostService.GetPage(query, paginationFilter).ToListAsync();
if (!hosts.Any())
return NoContent();
var hostResponse = mapper.Map<List<HostWithApplicationsResponse>>(hosts);
var paginationResponse = new PagedResponse<HostWithApplicationsResponse>(hostResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
}
}