GetStorageLocationsQueryHandler.cs 5.21 KB
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));
            }
        }
    }
}