using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors.Infrastructure; 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.Constants; using PARR.DAL.DomainModels; using PARR.DAL.Models; using PARR.DAL.Services.Interfaces; namespace PARR.API.Controllers.V1 { /// /// Управление приложениями /// [Authorize(Roles = ParrRoles.Administrator.Role)] public class ApplicationController : BaseApiController { private readonly ILogger logger; private readonly IMapper mapper; private readonly IApplicationService applicationService; public ApplicationController( ILogger logger, IMapper mapper, IApplicationService applicationService ) { this.logger = logger; this.mapper = mapper; this.applicationService = applicationService; } /// /// Список приложений постранично /// /// /// [HttpGet(ApiRoutes.Application.GetAll)] public async Task GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] ApplicationQuery filter) { var paginationFilter = mapper.Map(paginationQuery); IQueryable query = applicationService.Get() .Include(t => t.ApplicationType) .OrderBy(t => t.Name); if (!string.IsNullOrWhiteSpace(filter.Name)) query = query.Where(t => t.Name.ToLower().Contains(filter.Name.ToLower())); if (filter.Type.HasValue) query = query.Where(t => t.ApplicationType!.Name == filter.Type.Value.ToString()); var apps = await applicationService.GetPage(query, paginationFilter).ToListAsync(); if (!apps.Any()) return NoContent(); var appsResponse = mapper.Map>(apps); var paginationResponse = new PagedResponse(appsResponse, true).GetPaginatedProps(paginationFilter, query); return Ok(paginationResponse); } } }