Search in sources :

Example 1 with Command

use of com.kloia.eventapis.api.Command 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 Command

use of com.kloia.eventapis.api.Command in project eventapis by kloiasoft.

the class ReturnPaymentCommand method execute.

@RequestMapping(value = "/payment/{paymentId}/return", method = RequestMethod.POST)
@Command
public EventKey execute(@PathVariable("paymentId") String paymentId, @RequestBody @Valid ReturnPaymentCommandDto dto) throws Exception {
    dto.setPaymentId(paymentId);
    Payment payment = paymentViewQuery.queryEntity(dto.getPaymentId());
    if (payment.getState() == PaymentState.PAID) {
        PaymentReturnedEvent paymentReturnedEvent = new PaymentReturnedEvent(payment.getOrderId(), payment.getAmount());
        return eventRepository.recordAndPublish(payment, paymentReturnedEvent);
    } else
        throw new EventStoreException("Payment state is not valid for this Operation: " + dto);
}
Also used : PaymentReturnedEvent(com.kloia.sample.dto.event.PaymentReturnedEvent) EventStoreException(com.kloia.eventapis.exception.EventStoreException) Payment(com.kloia.sample.model.Payment) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Command

use of com.kloia.eventapis.api.Command in project eventapis by kloiasoft.

the class CreateOrderCommand method execute.

@RequestMapping(value = "/order/create", method = RequestMethod.POST)
@Command
public EventKey execute(@RequestBody @Valid CreateOrderCommandDto dto) throws Exception {
    OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent();
    BeanUtils.copyProperties(dto, orderCreatedEvent);
    return eventRepository.recordAndPublish(orderCreatedEvent);
}
Also used : OrderCreatedEvent(com.kloia.sample.dto.event.OrderCreatedEvent) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Command

use of com.kloia.eventapis.api.Command in project eventapis by kloiasoft.

the class ProcessOrderCommand method process.

@RequestMapping(value = "/order/{orderId}/process", method = RequestMethod.POST)
@Command
public EventKey process(@PathVariable("orderId") String orderId, @RequestBody @Valid ProcessOrderCommandDto dto) throws Exception {
    dto.setOrderId(orderId);
    Orders order = orderQuery.queryEntity(dto.getOrderId());
    if (order.getState() == OrderState.INITIAL) {
        ReserveStockEvent reserveStockEvent = new ReserveStockEvent(order.getStockId(), order.getOrderAmount(), dto.getPaymentInformation());
        log.info("Template account saved: " + dto);
        return eventRepository.recordAndPublish(order, reserveStockEvent);
    } else
        throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Also used : Orders(com.kloia.sample.model.Orders) EventStoreException(com.kloia.eventapis.exception.EventStoreException) ReserveStockEvent(com.kloia.sample.dto.event.ReserveStockEvent) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Command

use of com.kloia.eventapis.api.Command in project eventapis by kloiasoft.

the class CreateStockCommandHandler method execute.

@RequestMapping(value = "/stock/create", method = RequestMethod.POST)
@Command
public EventKey execute(@RequestBody CreateStockCommandDto dto) throws Exception {
    StockCreatedEvent stockCreatedEvent = new StockCreatedEvent();
    BeanUtils.copyProperties(dto, stockCreatedEvent);
    EventKey eventKey = eventRepository.recordAndPublish(stockCreatedEvent);
    try {
        addStockCommandHandler.execute(eventKey.getEntityId(), new AddStockCommandDto(dto.getRemainingStock(), eventKey.getEntityId()));
    } catch (Exception e) {
        log.warn("Sub Command Failed:" + e.getMessage());
    }
    return eventKey;
}
Also used : StockCreatedEvent(com.kloia.sample.dto.event.StockCreatedEvent) AddStockCommandDto(com.kloia.sample.dto.command.AddStockCommandDto) EventKey(com.kloia.eventapis.common.EventKey) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Command (com.kloia.eventapis.api.Command)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 EventKey (com.kloia.eventapis.common.EventKey)2 EventStoreException (com.kloia.eventapis.exception.EventStoreException)2 AddStockCommandDto (com.kloia.sample.dto.command.AddStockCommandDto)1 OrderCreatedEvent (com.kloia.sample.dto.event.OrderCreatedEvent)1 PaymentReturnedEvent (com.kloia.sample.dto.event.PaymentReturnedEvent)1 ReserveStockEvent (com.kloia.sample.dto.event.ReserveStockEvent)1 StockAddedEvent (com.kloia.sample.dto.event.StockAddedEvent)1 StockCreatedEvent (com.kloia.sample.dto.event.StockCreatedEvent)1 Orders (com.kloia.sample.model.Orders)1 Payment (com.kloia.sample.model.Payment)1 Stock (com.kloia.sample.model.Stock)1