use of com.kloia.sample.dto.StockNotEnoughException in project eventapis by kloiasoft.
the class ReserveStockEventHandler method execute.
@Override
@KafkaListener(topics = "ReserveStockEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(ReserveStockEvent dto) throws Exception {
Stock stock = stockQuery.queryEntity(dto.getStockId());
if (stock.getRemainingStock() >= dto.getNumberOfItemsSold()) {
StockReservedEvent stockReservedEvent = new StockReservedEvent();
BeanUtils.copyProperties(dto, stockReservedEvent);
stockReservedEvent.setOrderId(dto.getSender().getEntityId());
try {
return eventRepository.recordAndPublish(stock, stockReservedEvent, entityEvent -> new StockConcurrencyResolver(stockQuery, dto));
} catch (StockNotEnoughException e) {
return recordStockNotEnough(dto, stock);
} catch (Exception e) {
log.error(e.getMessage(), e);
return stock.getEventKey();
}
} else
return recordStockNotEnough(dto, stock);
}
Aggregations