Commit fa5082e6 authored by lemin's avatar lemin

ccc

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