Mico.Demo/MicoService.Demo2/Consumers/CheckInventoryConsumer.cs

36 lines
1.1 KiB
C#

using MassTransit;
using MassTransit.Message;
namespace MicoService.Demo2.Consumers
{
public class CheckInventoryConsumer : IConsumer<CheckInventoryRequest>
{
//private readonly IInventoryService _inventoryService;
public CheckInventoryConsumer(/*IInventoryService inventoryService*/)
{
//_inventoryService = inventoryService;
}
public async Task Consume(ConsumeContext<CheckInventoryRequest> context)
{
// 此处查询数据库,获取库存信息
//var inventory = await _inventoryService.GetByProductIdAsync(context.Message.ProductId);
// 模拟库存数据
var inventory = new
{
ProductId = context.Message.ProductId,
Quantity = 100 // 假设库存数量为100
};
var response = new CheckInventoryResponse(
inventory != null && inventory.Quantity >= context.Message.Quantity,
inventory?.Quantity ?? 0
);
await context.RespondAsync(response);
}
}
}