feat(api): HostController вывод даных из таблицы Unit

This commit is contained in:
Mikhail Kuznetsov
2025-06-06 16:39:41 +10:00
parent b82a1de8bf
commit 59b592fdd9
5 changed files with 112 additions and 22 deletions

View File

@@ -3,7 +3,7 @@
public class HostQuery
{
/// <summary>
/// Поиск по маске ЭК. Например: "*-ДВС"
/// Поиск по маске ЭК. Например: "ВРТ-*-ДВС"
/// </summary>
public string? Mask { get; set; }
}

View File

@@ -0,0 +1,17 @@
namespace PARR.API.Contracts.V1.Responses
{
public class AttributeResponse
{
public Guid Id { get; set; }
/// <summary>
/// Имя из поля DisplayName, если пустое - берём AihitName
/// </summary>
public required string Name { get; set; }
/// <summary>
/// Соответствующее полю значение
/// </summary>
public required string Value { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
namespace PARR.API.Contracts.V1.Responses
{
public class UnitResponse
{
public Guid Id { get; set; }
public required string Name { get; set; }
}
/// <summary>
/// Response с всеми аттребутами и их значениями
/// </summary>
public class UnitWithAttributesResponse : UnitResponse
{
public List<AttributeResponse>? Attributes { get; set; }
}
}

View File

@@ -11,7 +11,9 @@ using PARR.API.Extensions;
using PARR.BLL.Helpers;
using PARR.Constants;
using PARR.DAL.DomainModels;
using PARR.DAL.Models.Unit;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Unit;
namespace PARR.API.Controllers.V1
{
@@ -20,11 +22,17 @@ namespace PARR.API.Controllers.V1
{
private readonly IMapper mapper;
private readonly IHostService hostService;
private readonly IUnitService unitService;
public HostController(IMapper mapper, IHostService hostService)
public HostController(
IMapper mapper,
IHostService hostService,
IUnitService unitService
)
{
this.mapper = mapper;
this.hostService = hostService;
this.unitService = unitService;
}
@@ -38,23 +46,42 @@ namespace PARR.API.Controllers.V1
{
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
IQueryable<DAL.Models.Host> query = hostService.Get()
.Include(t=>t.WorkGroup).ThenInclude(t=>t!.ResponseArea)
.Include(s => s.EkStatus)
.Include(s => s.ResponseArea)
.Include(t => t.ApplicationsInHosts).ThenInclude(t => t.Application).ThenInclude(t => t!.ApplicationType)
.OrderBy(t => t.Ek);
//IQueryable<DAL.Models.Host> query = hostService.Get()
// .Include(t=>t.WorkGroup).ThenInclude(t=>t!.ResponseArea)
// .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);
IQueryable<Unit> query = unitService.Get()
.AsNoTracking()
.Include(t => t.UnitValues)
.ThenInclude(uv => uv.Value)
.Include(tf => tf.UnitValues)
.ThenInclude(uv => uv.Field)
.OrderBy(t => t.Name);
if (!string.IsNullOrEmpty(request.Mask))
query = query.Where(t => EF.Functions.Like(t.Ek.ToLower(), SqlHelpers.RegexToLike(request.Mask)));
query = query.Where(t => EF.Functions.Like(t.Name.ToLower(), SqlHelpers.RegexToLike(request.Mask)));
var hosts = await hostService.GetPage(query, paginationFilter).ToListAsync();
var units = await unitService.GetPage(query, paginationFilter).ToListAsync();
if (!hosts.Any())
if (!units.Any())
return NoContent();
var hostResponse = mapper.Map<List<HostWithApplicationsResponse>>(hosts);
var paginationResponse = new PagedResponse<HostWithApplicationsResponse>(hostResponse, true).GetPaginatedProps(paginationFilter, query);
var unitResponse = mapper.Map<List<UnitWithAttributesResponse>>(units);
var paginationResponse = new PagedResponse<UnitWithAttributesResponse>(unitResponse, true).GetPaginatedProps(paginationFilter, query);
return Ok(paginationResponse);
}
@@ -68,19 +95,31 @@ namespace PARR.API.Controllers.V1
[HttpGet(ApiRoutes.Host.Get)]
public async Task<IActionResult> GetById([FromRoute] Guid id)
{
var host = await hostService.Get()
.Include(t => t.WorkGroup).ThenInclude(t => t!.ResponseArea)
.Include(s => s.EkStatus)
.Include(s => s.ResponseArea)
.Include(t => t.ApplicationsInHosts).ThenInclude(t => t.Application).ThenInclude(t => t!.ApplicationType)
.FirstOrDefaultAsync(t => t.Id == id);
//var host = await hostService.Get()
// .Include(t => t.WorkGroup).ThenInclude(t => t!.ResponseArea)
// .Include(s => s.EkStatus)
// .Include(s => s.ResponseArea)
// .Include(t => t.ApplicationsInHosts).ThenInclude(t => t.Application).ThenInclude(t => t!.ApplicationType)
// .FirstOrDefaultAsync(t => t.Id == id);
if (host == null)
//if (host == null)
// return NotFound();
//var response = mapper.Map<HostWithApplicationsResponse>(host);
var unit = await unitService.Get()
.AsNoTracking()
.Include(t => t.UnitValues)
.ThenInclude(uv => uv.Value)
.Include(tf => tf.UnitValues)
.ThenInclude(uv => uv.Field)
.FirstOrDefaultAsync(t => t.Id == id);
if (unit == null)
return NotFound();
var response = mapper.Map<HostWithApplicationsResponse>(host);
var response = mapper.Map<UnitWithAttributesResponse>(unit);
return Ok(new Response<HostWithApplicationsResponse>(response, true));
return Ok(new Response<UnitWithAttributesResponse>(response, true));
}

View File

@@ -7,6 +7,7 @@ using PARR.Constants.Shortcodes;
using PARR.DAL.DomainModels;
using PARR.DAL.Extensions;
using PARR.DAL.Models;
using PARR.DAL.Models.Unit;
namespace PARR.API.MappingProfiles
{
@@ -103,6 +104,21 @@ namespace PARR.API.MappingProfiles
#endregion
#region Unit
CreateMap<UnitInValue, AttributeResponse>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.Value!.Id))
.ForMember(d => d.Name, o => o.MapFrom(s => (string.IsNullOrEmpty(s.Field!.DisplayName)) ? s.Field!.AihitName : s.Field!.DisplayName))
.ForMember(d => d.Value, o => o.MapFrom(s => s.Value!.Value));
CreateMap<Unit, UnitResponse>()
.Include<Unit, UnitWithAttributesResponse>()
.ForMember(d => d.Name, o => o.MapFrom(s => s.Name));
CreateMap<Unit, UnitWithAttributesResponse>()
.ForMember(d => d.Attributes, o => o.MapFrom(s => s.UnitValues!.OrderBy(s => (string.IsNullOrEmpty(s.Field!.DisplayName)) ? s.Field!.AihitName : s.Field!.DisplayName)));
#endregion
#region WorkGroup
CreateMap<WorkGroup, WorkGroupResponse>();