dde4c68b2f
Build and Deploy to Kubernetes / build-and-deploy (push) Failing after 2m33s
- Add GetLowStockItemsResponseDto and LowStockItemDto for low stock item queries. - Create CreateStockMovementCommand and its handler for managing stock movements. - Implement CreateStockMovementCommandValidator for validating stock movement commands. - Add GetStockMovementsQuery and its handler to retrieve stock movement records. - Create GetStockMovementsResponseDto and StockMovementListDto for stock movement responses. - Implement GetStockMovementsByInventoryItemQuery and its handler for fetching movements by inventory item. - Add CreateWarehouseCommand and its handler for creating new warehouses. - Implement CreateWarehouseCommandValidator for warehouse creation validation. - Add DeleteWarehouseCommand and its handler for removing warehouses. - Implement SetDefaultWarehouseCommand and its handler for setting a default warehouse. - Create UpdateWarehouseCommand and its handler for updating warehouse details. - Implement GetAllWarehousesQuery and its handler to retrieve all warehouses. - Add GetWarehouseQuery and its handler for fetching a specific warehouse by ID. - Implement SearchWarehousesQuery and its handler for searching warehouses based on criteria.
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using CMSMicroservice.Application.Common.Exceptions;
|
|
|
|
namespace CMSMicroservice.Application.WarehouseCQ.Commands.UpdateWarehouse;
|
|
|
|
public class UpdateWarehouseCommandHandler : IRequestHandler<UpdateWarehouseCommand, UpdateWarehouseResponseDto>
|
|
{
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
public UpdateWarehouseCommandHandler(IApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<UpdateWarehouseResponseDto> Handle(UpdateWarehouseCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var warehouse = await _context.Warehouses
|
|
.FirstOrDefaultAsync(w => w.Id == request.Id, cancellationToken);
|
|
|
|
if (warehouse == null)
|
|
{
|
|
throw new NotFoundException(nameof(Domain.Entities.Warehouse), request.Id);
|
|
}
|
|
|
|
// بررسی تکراری نبودن کد جدید
|
|
if (!string.IsNullOrEmpty(request.Code) && request.Code != warehouse.Code)
|
|
{
|
|
var codeExists = await _context.Warehouses
|
|
.AnyAsync(w => w.Code == request.Code && w.Id != request.Id, cancellationToken);
|
|
|
|
if (codeExists)
|
|
{
|
|
throw new InvalidOperationException($"Warehouse with code '{request.Code}' already exists");
|
|
}
|
|
warehouse.Code = request.Code;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.Name))
|
|
{
|
|
warehouse.Name = request.Name;
|
|
}
|
|
|
|
if (request.Address != null)
|
|
{
|
|
warehouse.Address = request.Address;
|
|
}
|
|
|
|
if (request.IsActive.HasValue)
|
|
{
|
|
warehouse.IsActive = request.IsActive.Value;
|
|
}
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdateWarehouseResponseDto { Success = true };
|
|
}
|
|
}
|