Search in sources :

Example 1 with Stock

use of com.kloia.sample.model.Stock in project eventapis by kloiasoft.

the class AddStockCommandHandler method execute.

@RequestMapping(value = "/stock/{stockId}/add", method = RequestMethod.POST)
@Command()
public EventKey execute(String stockId, @RequestBody AddStockCommandDto dto) throws Exception {
    dto.setStockId(stockId);
    Stock stock = stockQuery.queryEntity(dto.getStockId());
    if (dto.getStockToAdd() > 1000000)
        throw new IllegalArgumentException("Invalid Stock to Add");
    EventKey eventKey = eventRepository.recordAndPublish(stock.getEventKey(), new StockAddedEvent(dto.getStockToAdd()));
    return eventKey;
}
Also used : StockAddedEvent(com.kloia.sample.dto.event.StockAddedEvent) EventKey(com.kloia.eventapis.common.EventKey) Stock(com.kloia.sample.model.Stock) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Stock

use of com.kloia.sample.model.Stock 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);
}
Also used : StockNotEnoughException(com.kloia.sample.dto.StockNotEnoughException) StockReservedEvent(com.kloia.sample.dto.event.StockReservedEvent) Stock(com.kloia.sample.model.Stock) StockNotEnoughException(com.kloia.sample.dto.StockNotEnoughException) EventStoreException(com.kloia.eventapis.exception.EventStoreException) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 3 with Stock

use of com.kloia.sample.model.Stock in project eventapis by kloiasoft.

the class StockRestController method getStock.

@RequestMapping(value = "/{stockId}", method = RequestMethod.GET)
public ResponseEntity<?> getStock(@PathVariable("stockId") String stockId) throws IOException, EventStoreException {
    Stock one = stockRepository.findOne(stockId);
    Stock responseDto = new Stock();
    BeanUtils.copyProperties(one, responseDto);
    return new ResponseEntity<Object>(responseDto, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Stock(com.kloia.sample.model.Stock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Stock

use of com.kloia.sample.model.Stock in project eventapis by kloiasoft.

the class WaitingStockReleaseEventHandler method execute.

@Override
@KafkaListener(topics = "WaitingStockReleaseEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(WaitingStockReleaseEvent dto) throws Exception {
    Stock stock = stockQuery.queryEntity(dto.getStockId());
    StockReservedEvent stockReservedEvent = stockQuery.queryEventData(dto.getStockId(), dto.getReservedStockVersion());
    return eventRepository.recordAndPublish(stock, new StockReleasedEvent(dto.getSender().getEntityId(), stockReservedEvent.getNumberOfItemsSold()), entityEvent -> new StockConcurrencyResolver());
}
Also used : StockReleasedEvent(com.kloia.sample.dto.event.StockReleasedEvent) StockReservedEvent(com.kloia.sample.dto.event.StockReservedEvent) Stock(com.kloia.sample.model.Stock) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Aggregations

Stock (com.kloia.sample.model.Stock)4 StockReservedEvent (com.kloia.sample.dto.event.StockReservedEvent)2 KafkaListener (org.springframework.kafka.annotation.KafkaListener)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Command (com.kloia.eventapis.api.Command)1 EventKey (com.kloia.eventapis.common.EventKey)1 EventStoreException (com.kloia.eventapis.exception.EventStoreException)1 StockNotEnoughException (com.kloia.sample.dto.StockNotEnoughException)1 StockAddedEvent (com.kloia.sample.dto.event.StockAddedEvent)1 StockReleasedEvent (com.kloia.sample.dto.event.StockReleasedEvent)1 ResponseEntity (org.springframework.http.ResponseEntity)1