GetStorageLocationsQueryHandler.cs
5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using AutoMapper;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.DTOs;
using Rcs.Application.MessageBus.Commands.StorageLocations;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands.StorageLocations
{
/// <summary>
/// Get storage location list query handler.
/// </summary>
public class GetStorageLocationsQueryHandler : IConsumer<GetStorageLocationsQuery>
{
private readonly ILogger<GetStorageLocationsQueryHandler> _logger;
private readonly IStorageLocationRepository _repository;
private readonly IMapper _mapper;
public GetStorageLocationsQueryHandler(
ILogger<GetStorageLocationsQueryHandler> logger,
IStorageLocationRepository repository,
IMapper mapper)
{
_logger = logger;
_repository = repository;
_mapper = mapper;
}
public async Task Consume(ConsumeContext<GetStorageLocationsQuery> context)
{
var query = context.Message;
try
{
IQueryable<Rcs.Domain.Entities.StorageLocation> queryable = _repository.GetQueryable()
.Include(x => x.StorageArea)
.Include(x => x.LocationType)
.Include(x => x.MapNode);
if (!string.IsNullOrWhiteSpace(query.FilterModel))
{
queryable = FilterHelper.ApplyFilters(queryable, query.FilterModel);
}
if (!string.IsNullOrWhiteSpace(query.LocationCode))
{
queryable = queryable.Where(x => x.LocationCode.Contains(query.LocationCode));
}
if (!string.IsNullOrWhiteSpace(query.LocationName))
{
queryable = queryable.Where(x => x.LocationName != null && x.LocationName.Contains(query.LocationName));
}
if (!string.IsNullOrWhiteSpace(query.AreaCode))
{
queryable = queryable.Where(x => x.StorageArea != null && x.StorageArea.AreaCode.Contains(query.AreaCode));
}
if (!string.IsNullOrWhiteSpace(query.AreaName))
{
queryable = queryable.Where(x => x.StorageArea != null && x.StorageArea.AreaName != null && x.StorageArea.AreaName.Contains(query.AreaName));
}
if (!string.IsNullOrWhiteSpace(query.LocationTypeCode))
{
queryable = queryable.Where(x => x.LocationType != null && x.LocationType.TypeCode.Contains(query.LocationTypeCode));
}
if (!string.IsNullOrWhiteSpace(query.LocationTypeName))
{
queryable = queryable.Where(x => x.LocationType != null && x.LocationType.TypeName.Contains(query.LocationTypeName));
}
if (!string.IsNullOrWhiteSpace(query.MapNodeCode))
{
queryable = queryable.Where(x => x.MapNode != null && x.MapNode.NodeCode.Contains(query.MapNodeCode));
}
if (query.Orientation.HasValue)
{
queryable = queryable.Where(x => x.Orientation.HasValue && x.Orientation.Value == query.Orientation.Value);
}
if (!string.IsNullOrWhiteSpace(query.AreaId) && Guid.TryParse(query.AreaId, out var areaId))
{
queryable = queryable.Where(x => x.AreaId == areaId);
}
if (!string.IsNullOrWhiteSpace(query.LocationTypeId) && Guid.TryParse(query.LocationTypeId, out var locationTypeId))
{
queryable = queryable.Where(x => x.LocationTypeId == locationTypeId);
}
if (query.Status.HasValue && Enum.IsDefined(typeof(StorageLocationStatus), query.Status.Value))
{
var status = (StorageLocationStatus)query.Status.Value;
queryable = queryable.Where(x => x.Status == status);
}
if (query.IsActive.HasValue)
{
queryable = queryable.Where(x => x.IsActive == query.IsActive.Value);
}
var totalCount = await queryable.CountAsync(context.CancellationToken);
var items = await queryable
.OrderByDescending(x => x.CreatedAt)
.Skip((query.PageNumber - 1) * query.PageSize)
.Take(query.PageSize)
.ToListAsync(context.CancellationToken);
var dtos = _mapper.Map<List<StorageLocationListItemDto>>(items);
await context.RespondAsync(
PagedResponse<StorageLocationListItemDto>.Successful(dtos, query.PageNumber, query.PageSize, totalCount));
}
catch (Exception ex)
{
_logger.LogError(ex, "Get storage location list failed");
await context.RespondAsync(PagedResponse<StorageLocationListItemDto>.Failed(ex.Message));
}
}
}
}