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;
}
///
/// Получить список хостов постранично
///
///
///
[HttpGet(ApiRoutes.Host.GetAll)]
public async Task GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] HostQuery request)
{
var paginationFilter = mapper.Map(paginationQuery);
IQueryable query = hostService.Get()
.Include(s => s.EkStatus)
.Include(s => s.ResponseArea)
.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>(hosts);
var paginationResponse = new PagedResponse(hostResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
}
}