Commit fa5082e6 authored by lemin's avatar lemin

ccc

parent 90826b75
package com.showcase.synapse.common.command; package com.showcase.synapse.common.command;
import lombok.AllArgsConstructor; import java.io.Serializable;
import lombok.Getter;
import org.axonframework.modelling.command.TargetAggregateIdentifier; import org.axonframework.modelling.command.TargetAggregateIdentifier;
import java.math.BigDecimal; import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class CreateDeliveryCommand { public class CreateDeliveryCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier @TargetAggregateIdentifier
private final String id; private String deliveryOrderId;
private final String name; private String salesOrderId;
private final String comment; private String whId;
private final BigDecimal price; private String orderComment;
// private final Integer quantity; private Integer orderQuantity;
private String orderName;
} }
package com.showcase.synapse.delivery.aggregate; package com.showcase.synapse.delivery.aggregate;
import lombok.NoArgsConstructor; import static org.axonframework.modelling.command.AggregateLifecycle.apply;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler; import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler; import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier; import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate; import org.axonframework.spring.stereotype.Aggregate;
import com.showcase.synapse.common.command.ChangeDeliveryStatusCommand;
import com.showcase.synapse.common.command.CreateDeliveryCommand; import com.showcase.synapse.common.command.CreateDeliveryCommand;
import com.showcase.synapse.delivery.event.DeliveryCreatedEvent; import com.showcase.synapse.delivery.event.DeliveryCreatedEvent;
import com.showcase.synapse.delivery.event.DeliveryQuantityChangedEvent;
import com.showcase.synapse.delivery.saga.DeliverySaga;
import java.math.BigDecimal;
import static org.axonframework.modelling.command.AggregateLifecycle.apply; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Aggregate @Aggregate
@NoArgsConstructor @NoArgsConstructor
...@@ -24,32 +19,21 @@ import static org.axonframework.modelling.command.AggregateLifecycle.apply; ...@@ -24,32 +19,21 @@ import static org.axonframework.modelling.command.AggregateLifecycle.apply;
public class DeliveryAggregate { public class DeliveryAggregate {
@AggregateIdentifier @AggregateIdentifier
private String id; private String deliveryOrderId;
// private int quantiy; private int orderQuantity;
@CommandHandler @CommandHandler
public DeliveryAggregate(CreateDeliveryCommand command) { public DeliveryAggregate(CreateDeliveryCommand command) {
log.info("[DeliveryAggregate(CreateDeliveryCommand) > apply new DeliveryCreatedEvent]"); log.info("[DeliveryAggregate(CreateDeliveryCommand) > apply new DeliveryCreatedEvent]");
apply(new DeliveryCreatedEvent(command.getId(), command.getName(), command.getComment(), command.getPrice())); apply(new DeliveryCreatedEvent(command.getDeliveryOrderId(), command.getSalesOrderId(), command.getWhId(),
} command.getOrderName(), command.getOrderComment(), command.getOrderQuantity()));
@EventSourcingHandler
public void createProduct(DeliveryCreatedEvent event) {
this.id = event.getProductId();
// this.quantiy = event.getQuantity();
}
@CommandHandler
public void changeQuantity(ChangeDeliveryStatusCommand command) {
log.info("[@CommandHandler ProductchangeQuantity]");
// if(this.quantiy < command.getQuantity()) throw new IllegalArgumentException("tfy ");
// apply(new DeliveryQuantityChangedEvent(command.getProductId(), this.quantiy - command.getQuantity()));
} }
@EventSourcingHandler @EventSourcingHandler
public void changeQuantity(DeliveryQuantityChangedEvent event) { public void createDelivery(DeliveryCreatedEvent event) {
// this.quantiy = event.getQuantity(); this.deliveryOrderId = event.getDeliveryOrderId();
this.orderQuantity = event.getOrderQuantity();
} }
} }
...@@ -14,31 +14,31 @@ import java.util.Map; ...@@ -14,31 +14,31 @@ import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@RestController @RestController
@RequestMapping("/api/msa/product") @RequestMapping("/api/msa/delivery")
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
public class DeliveryController { public class DeliveryController {
final DeliveryService productService; final DeliveryService deliveryService;
public DeliveryController(DeliveryService productService) { public DeliveryController(DeliveryService deliveryService) {
this.productService = productService; this.deliveryService = deliveryService;
} }
@GetMapping("/") @GetMapping("/")
public ResponseEntity<List<DeliveryEntity>> getProducts() throws ExecutionException, InterruptedException { public ResponseEntity<List<DeliveryEntity>> getDeliveryList() throws ExecutionException, InterruptedException {
List<DeliveryEntity> productEntities = productService.getProducts(); List<DeliveryEntity> deliveryEntity = deliveryService.getDeliveryList();
return ResponseEntity.ok(productEntities); return ResponseEntity.ok(deliveryEntity);
} }
@PostMapping("/") @PostMapping("/")
public ResponseEntity<Map<String, String>> productCreate(@RequestBody DeliveryCreateDto productCreateDto) { public ResponseEntity<Map<String, String>> deliveryOrderCreate(@RequestBody DeliveryCreateDto deliveryCreateDto) {
String productId = productService.createProduct(productCreateDto.getName(), String deliveryId = deliveryService.createDeliveryOrder(deliveryCreateDto.getSalesOrderId(),
productCreateDto.getComment(), deliveryCreateDto.getWhId());
BigDecimal.valueOf(productCreateDto.getPrice()));
HashMap<String, String> m = new HashMap<>(); HashMap<String, String> m = new HashMap<>();
m.put("productId", productId); m.put("deliveryId", deliveryId);
m.put("productName", productCreateDto.getName()); m.put("salesOrderId", deliveryCreateDto.getSalesOrderId());
m.put("getWhId", deliveryCreateDto.getWhId());
return ResponseEntity.ok(m); return ResponseEntity.ok(m);
} }
......
...@@ -6,8 +6,7 @@ import lombok.Setter; ...@@ -6,8 +6,7 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class DeliveryCreateDto { public class DeliveryCreateDto {
private String name; private String salesOrderId;
private String comment; private String whId;
private int price;
} }
...@@ -16,8 +16,10 @@ import java.math.BigDecimal; ...@@ -16,8 +16,10 @@ import java.math.BigDecimal;
@NoArgsConstructor @NoArgsConstructor
public class DeliveryEntity { public class DeliveryEntity {
@Id @Id
private String id; private String deliveryOrderId;
private String name; private String salesOrderId;
private String comment; private String whId;
private BigDecimal price; private String orderName;
private String orderComment;
private Integer orderQuantity;
} }
...@@ -8,8 +8,10 @@ import java.math.BigDecimal; ...@@ -8,8 +8,10 @@ import java.math.BigDecimal;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class DeliveryCreatedEvent { public class DeliveryCreatedEvent {
private String productId; private String deliveryOrderId;
private String name; private String salesOrderId;
private String comment; private String whId;
private BigDecimal price; private String orderName;
private String orderComment;
private Integer orderQuantity;
} }
package com.showcase.synapse.delivery.event;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class DeliveryQuantityChangedEvent {
private String productId;
private int quantity;
}
package com.showcase.synapse.delivery.event.handler; package com.showcase.synapse.delivery.event.handler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.eventhandling.EventHandler; import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.showcase.synapse.delivery.entity.DeliveryEntity; import com.showcase.synapse.delivery.entity.DeliveryEntity;
import com.showcase.synapse.delivery.event.DeliveryCreatedEvent; import com.showcase.synapse.delivery.event.DeliveryCreatedEvent;
import com.showcase.synapse.delivery.event.DeliveryQuantityChangedEvent;
import com.showcase.synapse.delivery.repository.DeliveryRepository; import com.showcase.synapse.delivery.repository.DeliveryRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class DeliveryEventHandler { public class DeliveryEventHandler {
private final DeliveryRepository productRepository; private final DeliveryRepository deliveryRepository;
@EventHandler @EventHandler
protected void saveProduct(DeliveryCreatedEvent productCreatedEvent) { protected void saveDeliveryOrder(DeliveryCreatedEvent DeliveryCreatedEvent) {
log.info("DeliveryCreatedEvent 이벤트 받음?"); log.info("DeliveryCreatedEvent 이벤트 받음?");
log.info("DeliveryEventHandler > [DeliveryCreatedEvent] productCreatedEvent"); log.info("DeliveryEventHandler > [DeliveryCreatedEvent]");
DeliveryEntity productEntity = new DeliveryEntity(); DeliveryEntity deliveryEntity = new DeliveryEntity();
productEntity.setId(productCreatedEvent.getProductId()); deliveryEntity.setDeliveryOrderId(DeliveryCreatedEvent.getDeliveryOrderId());
productEntity.setName(productCreatedEvent.getName()); deliveryEntity.setSalesOrderId(DeliveryCreatedEvent.getSalesOrderId());
productEntity.setComment(productCreatedEvent.getComment()); deliveryEntity.setOrderComment(DeliveryCreatedEvent.getOrderComment());
productEntity.setPrice(productCreatedEvent.getPrice()); deliveryEntity.setWhId(DeliveryCreatedEvent.getWhId());
deliveryEntity.setOrderName(DeliveryCreatedEvent.getOrderName());
deliveryEntity.setOrderQuantity(DeliveryCreatedEvent.getOrderQuantity());
productRepository.save(productEntity); deliveryRepository.save(deliveryEntity);
} }
@EventHandler
protected void changeQuantity(DeliveryQuantityChangedEvent productQuantityChangedEvent) {
log.info("DeliveryQuantityChangedEvent 이벤트 받음?");
log.info("[DeliveryQuantityChangedEvent]");
DeliveryEntity productEntity = productRepository.findById(productQuantityChangedEvent.getProductId()).get();
// log.info("[{}] quantity:{}", productEntity.getName(), productEntity.getQuentity());
// productEntity.setQuentity(productQuantityChangedEvent.getQuantity());
productRepository.save(productEntity);
}
} }
...@@ -28,21 +28,21 @@ public class DeliveryService { ...@@ -28,21 +28,21 @@ public class DeliveryService {
this.queryGateway = queryGateway; this.queryGateway = queryGateway;
} }
public String createProduct(String name, String comment, BigDecimal price) { public String createDeliveryOrder(String salesOrderId, String whId) {
log.info("[@Service createProduct] new CreateDeliveryCommand"); log.info("[@Service createDeliveryOrder] new CreateDeliveryCommand");
// command생성 // command생성
CreateDeliveryCommand createProductCommand = new CreateDeliveryCommand( CreateDeliveryCommand createDeliveryCommand = new CreateDeliveryCommand(
UUID.randomUUID().toString(), name, comment, price UUID.randomUUID().toString(), salesOrderId, whId, "Direct api call orderComment", 0, "Direct api call orderName"
); );
System.out.println("test");
// 여기 // 여기
// 생성한 command전송(비동기) // 생성한 command전송(비동기)
String returnValue = commandGateway.sendAndWait(createProductCommand); log.info("[CreateDeliveryCommand] command전송(비동기)");
String returnValue = commandGateway.sendAndWait(createDeliveryCommand);
System.out.printf("returnValue: %s \n", returnValue); System.out.printf("returnValue: %s \n", returnValue);
return returnValue; return returnValue;
} }
public List<DeliveryEntity> getProducts() throws ExecutionException, InterruptedException { public List<DeliveryEntity> getDeliveryList() throws ExecutionException, InterruptedException {
return queryGateway.query(new GetDeliveryQuery(), return queryGateway.query(new GetDeliveryQuery(),
ResponseTypes.multipleInstancesOf(DeliveryEntity.class)).get(); ResponseTypes.multipleInstancesOf(DeliveryEntity.class)).get();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment