-
Notifications
You must be signed in to change notification settings - Fork 13
/
CreateStockCommandHandler.java
62 lines (53 loc) · 2.31 KB
/
CreateStockCommandHandler.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
package com.kloia.sample.commands;
import com.kloia.eventapis.api.CommandHandler;
import com.kloia.eventapis.api.EventRepository;
import com.kloia.eventapis.api.ViewQuery;
import com.kloia.eventapis.common.EventKey;
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.command.CreateStockCommandDto;
import com.kloia.sample.dto.event.StockCreatedEvent;
import com.kloia.sample.model.Stock;
import com.kloia.sample.model.StockState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
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;
/**
* Created by zeldalozdemir on 23/02/2017.
*/
@Slf4j
@RestController
public class CreateStockCommandHandler implements CommandHandler<Stock, CreateStockCommandDto> {
private final static String name = "CREATE_STOCK";
private final static String CREATED = "CREATED";
private final EventRepository eventRepository;
private final ViewQuery<Stock> orderQuery;
@Autowired
public CreateStockCommandHandler(EventRepository eventRepository, ViewQuery<Stock> orderQuery) {
this.eventRepository = eventRepository;
this.orderQuery = orderQuery;
}
@Override
@RequestMapping(value = "/stock/v1/create", method = RequestMethod.POST)
public EventKey execute(@RequestBody CreateStockCommandDto dto) throws Exception {
StockCreatedEvent stockCreatedEvent = new StockCreatedEvent();
BeanUtils.copyProperties(dto,stockCreatedEvent);
return eventRepository.recordAndPublish(stockCreatedEvent);
}
@Component
public static class CreateStockSpec extends EntityFunctionSpec<Stock, StockCreatedEvent> {
public CreateStockSpec() {
super((stock, event) -> {
StockCreatedEvent eventData = event.getEventData();
stock.setRemainingStock(eventData.getRemainingStock());
stock.setStockName(eventData.getStockName());
stock.setState(StockState.INUSE);
return stock;
});
}
}
}