-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProcessOrderCommand.java
80 lines (72 loc) · 3.35 KB
/
ProcessOrderCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.kloia.sample.commands;
import com.kloia.eventapis.api.CommandHandler;
import com.kloia.eventapis.api.EventRepository;
import com.kloia.eventapis.api.RollbackSpec;
import com.kloia.eventapis.api.ViewQuery;
import com.kloia.eventapis.common.EventKey;
import com.kloia.eventapis.exception.EventStoreException;
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.command.ProcessOrderCommandDto;
import com.kloia.sample.dto.event.ReserveStockEvent;
import com.kloia.sample.model.Order;
import com.kloia.sample.model.OrderState;
import com.kloia.sample.model.PaymentInformation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* Created by zeldalozdemir on 23/02/2017.
*/
@Slf4j
@RestController
public class ProcessOrderCommand implements CommandHandler<Order, ProcessOrderCommandDto> {
private final EventRepository eventRepository;
private final ViewQuery<Order> orderQuery;
@Autowired
public ProcessOrderCommand(EventRepository eventRepository, ViewQuery<Order> orderQuery) {
this.eventRepository = eventRepository;
this.orderQuery = orderQuery;
}
@RequestMapping(value = "/order/v1/{orderId}/process", method = RequestMethod.POST)
public EventKey execute(@PathVariable("orderId") String orderId, @RequestBody @Valid ProcessOrderCommandDto dto) throws Exception {
dto.setOrderId(orderId);
return this.execute(dto);
}
@Override
public EventKey execute(@RequestBody ProcessOrderCommandDto dto) throws Exception {
Order 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);
}
@Component
public static class ProcessOrderSpec extends EntityFunctionSpec<Order, ReserveStockEvent> {
public ProcessOrderSpec() {
super((order, event) -> {
ReserveStockEvent eventData = event.getEventData();
PaymentInformation paymentInformation = eventData.getPaymentInformation();
order.setPaymentAddress(paymentInformation.getPaymentAddress());
order.setAmount(paymentInformation.getAmount());
order.setCardInformation(paymentInformation.getCardInformation());
order.setState(OrderState.PROCESSING);
return order;
});
}
}
@Component
public static class ProcessOrderRollback implements RollbackSpec<ReserveStockEvent>{
@Override
public void rollback(ReserveStockEvent event) {
log.warn("ProcessOrderRollback for :"+event);
}
}
}