Commit d6b92f6c authored by lemin's avatar lemin

c

parent 548c2768
package com.showcase.synapse.product.aggregate;
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.product.command.CreateChannelCommand;
import com.showcase.synapse.product.event.ChannelCreatedEvent;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Aggregate
@NoArgsConstructor
@Slf4j
public class ChannelAggregate {
@AggregateIdentifier
private Integer id;
@CommandHandler
public ChannelAggregate(CreateChannelCommand command) {
log.info("[ChannelAggregate(CreateChannelCommand) > apply new ChannelCreatedEvent]");
apply(new ChannelCreatedEvent(command.getId(), command.getName(), command.getDiscountRate()));
}
@EventSourcingHandler
public void createChannel(ChannelCreatedEvent event) {
this.id = event.getId();
}
}
package com.showcase.synapse.product.aggregate; package com.showcase.synapse.product.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;
...@@ -9,14 +8,14 @@ import org.axonframework.modelling.command.AggregateIdentifier; ...@@ -9,14 +8,14 @@ import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate; import org.axonframework.spring.stereotype.Aggregate;
import com.showcase.synapse.product.command.ChangeQuantityCommand; import com.showcase.synapse.product.command.ChangeQuantityCommand;
import com.showcase.synapse.product.command.CreateChannelCommand;
import com.showcase.synapse.product.command.CreateProductCommand; import com.showcase.synapse.product.command.CreateProductCommand;
import com.showcase.synapse.product.event.ChannelCreatedEvent;
import com.showcase.synapse.product.event.ProductCreatedEvent; import com.showcase.synapse.product.event.ProductCreatedEvent;
import com.showcase.synapse.product.event.ProductQuantityChangedEvent; import com.showcase.synapse.product.event.ProductQuantityChangedEvent;
import com.showcase.synapse.product.saga.ProductSaga;
import java.math.BigDecimal;
import static org.axonframework.modelling.command.AggregateLifecycle.apply; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Aggregate @Aggregate
@NoArgsConstructor @NoArgsConstructor
......
package com.showcase.synapse.product.command;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
import java.math.BigDecimal;
import javax.persistence.Column;
@AllArgsConstructor
@Getter
public class CreateChannelCommand {
@TargetAggregateIdentifier
private final Integer id;
private final String name;
private final BigDecimal discountRate;
}
package com.showcase.synapse.product.controller; package com.showcase.synapse.product.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.showcase.synapse.product.dto.ChannelCreateDto;
import com.showcase.synapse.product.dto.ProductCreateDto; import com.showcase.synapse.product.dto.ProductCreateDto;
import com.showcase.synapse.product.entity.ChannelEntity;
import com.showcase.synapse.product.entity.ProductEntity; import com.showcase.synapse.product.entity.ProductEntity;
import com.showcase.synapse.product.service.ProductService; import com.showcase.synapse.product.service.ProductService;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
@RestController @RestController
@RequestMapping("/api/msa/product") @RequestMapping("/api/msa")
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
public class ProductController { public class ProductController {
...@@ -24,18 +30,25 @@ public class ProductController { ...@@ -24,18 +30,25 @@ public class ProductController {
this.productService = productService; this.productService = productService;
} }
@GetMapping("/") @GetMapping("/product")
public ResponseEntity<List<ProductEntity>> getProducts() throws ExecutionException, InterruptedException { public ResponseEntity<List<ProductEntity>> getProducts() throws ExecutionException, InterruptedException {
List<ProductEntity> productEntities = productService.getProducts(); List<ProductEntity> productEntities = productService.getProducts();
return ResponseEntity.ok(productEntities); return ResponseEntity.ok(productEntities);
} }
@PostMapping("/") @GetMapping("/channel")
public ResponseEntity<List<ChannelEntity>> getChannels() throws ExecutionException, InterruptedException {
List<ChannelEntity> channelEntity = productService.getChannels();
return ResponseEntity.ok(channelEntity);
}
@PostMapping("/product")
public ResponseEntity<Map<String, String>> productCreate(@RequestBody ProductCreateDto productCreateDto) { public ResponseEntity<Map<String, String>> productCreate(@RequestBody ProductCreateDto productCreateDto) {
String productId = productService.createProduct(productCreateDto.getName(), String productId = productService.createProduct(productCreateDto.getName(),
productCreateDto.getComment(), productCreateDto.getComment(),
BigDecimal.valueOf(productCreateDto.getPrice()), productCreateDto.getPrice(),
productCreateDto.getChannel()); productCreateDto.getChannel());
HashMap<String, String> m = new HashMap<>(); HashMap<String, String> m = new HashMap<>();
m.put("productId", productId); m.put("productId", productId);
...@@ -43,4 +56,16 @@ public class ProductController { ...@@ -43,4 +56,16 @@ public class ProductController {
return ResponseEntity.ok(m); return ResponseEntity.ok(m);
} }
@PostMapping("/channel")
public ResponseEntity<Map<String, String>> channelCreate(@RequestBody ChannelCreateDto channelCreateDto) {
int channelId = productService.createChannel(channelCreateDto.getId(),
channelCreateDto.getName(),
channelCreateDto.getDiscountRate());
HashMap<String, String> m = new HashMap<>();
m.put("channelId", String.valueOf(channelId));
m.put("name", channelCreateDto.getName());
return ResponseEntity.ok(m);
}
} }
package com.showcase.synapse.product.dto;
import java.math.BigDecimal;
import javax.persistence.Column;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ChannelCreateDto {
private int id;
private String name;
private BigDecimal discountRate;
}
package com.showcase.synapse.product.dto; package com.showcase.synapse.product.dto;
import java.math.BigDecimal;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -8,7 +10,8 @@ import lombok.Setter; ...@@ -8,7 +10,8 @@ import lombok.Setter;
public class ProductCreateDto { public class ProductCreateDto {
private String name; private String name;
private String comment; private String comment;
private int price; private BigDecimal price;
private int channel; private int channel;
private BigDecimal discountRate;
} }
package com.showcase.synapse.product.entity; package com.showcase.synapse.product.entity;
import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
...@@ -18,10 +20,13 @@ import lombok.Setter; ...@@ -18,10 +20,13 @@ import lombok.Setter;
@NoArgsConstructor @NoArgsConstructor
public class ChannelEntity { public class ChannelEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Id
@Column(name = "channel_id") @Column(name = "channel_id")
private Integer id; private Integer id;
@Column(name = "name") @Column(name = "name")
private String name; private String name;
@Column(name = "discount_rate")
private BigDecimal discountRate;
} }
...@@ -18,7 +18,7 @@ import lombok.Setter; ...@@ -18,7 +18,7 @@ import lombok.Setter;
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class ProductEntity{ public class ProductEntity {
@Id @Id
private String id; private String id;
......
package com.showcase.synapse.product.event;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.math.BigDecimal;
@AllArgsConstructor
@Getter
public class ChannelCreatedEvent {
private Integer id;
private String name;
private BigDecimal discountRate;
}
package com.showcase.synapse.product.event.handler; package com.showcase.synapse.product.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.product.entity.ChannelEntity; import com.showcase.synapse.product.entity.ChannelEntity;
import com.showcase.synapse.product.entity.ProductEntity; import com.showcase.synapse.product.entity.ProductEntity;
import com.showcase.synapse.product.event.ChannelCreatedEvent;
import com.showcase.synapse.product.event.ProductCreatedEvent; import com.showcase.synapse.product.event.ProductCreatedEvent;
import com.showcase.synapse.product.event.ProductQuantityChangedEvent; import com.showcase.synapse.product.event.ProductQuantityChangedEvent;
import com.showcase.synapse.product.repository.ChannelRepository;
import com.showcase.synapse.product.repository.ProductRepository; import com.showcase.synapse.product.repository.ProductRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class ProductEventHandler { public class ProductEventHandler {
private final ProductRepository productRepository; private final ProductRepository productRepository;
private final ChannelRepository channelRepository;
@EventHandler @EventHandler
protected void saveProduct(ProductCreatedEvent productCreatedEvent) { protected void saveProduct(ProductCreatedEvent productCreatedEvent) {
...@@ -36,6 +40,20 @@ public class ProductEventHandler { ...@@ -36,6 +40,20 @@ public class ProductEventHandler {
} }
@EventHandler
protected void saveChannel(ChannelCreatedEvent channelCreatedEvent) {
log.info("ChannelCreatedEvent 이벤트 받음?");
log.info("ProductEventHandler > [ChannelCreatedEvent] channelCreatedEvent");
ChannelEntity channelEntity = new ChannelEntity();
channelEntity.setId(channelCreatedEvent.getId());
channelEntity.setName(channelCreatedEvent.getName());
channelEntity.setDiscountRate(channelCreatedEvent.getDiscountRate());
channelRepository.save(channelEntity);
}
@EventHandler @EventHandler
protected void changeQuantity(ProductQuantityChangedEvent productQuantityChangedEvent) { protected void changeQuantity(ProductQuantityChangedEvent productQuantityChangedEvent) {
log.info("ProductQuantityChangedEvent 이벤트 받음?"); log.info("ProductQuantityChangedEvent 이벤트 받음?");
......
package com.showcase.synapse.product.query;
public class GetChannelQuery {
}
...@@ -5,8 +5,11 @@ import java.util.List; ...@@ -5,8 +5,11 @@ import java.util.List;
import org.axonframework.queryhandling.QueryHandler; import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.showcase.synapse.product.entity.ChannelEntity;
import com.showcase.synapse.product.entity.ProductEntity; import com.showcase.synapse.product.entity.ProductEntity;
import com.showcase.synapse.product.query.GetChannelQuery;
import com.showcase.synapse.product.query.GetProductsQuery; import com.showcase.synapse.product.query.GetProductsQuery;
import com.showcase.synapse.product.repository.ChannelRepository;
import com.showcase.synapse.product.repository.ProductRepository; import com.showcase.synapse.product.repository.ProductRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -18,6 +21,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -18,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
public class ProductQueryHandler { public class ProductQueryHandler {
private final ProductRepository productRepository; private final ProductRepository productRepository;
private final ChannelRepository channelRepository;
@QueryHandler @QueryHandler
protected List<ProductEntity> on(GetProductsQuery query) { protected List<ProductEntity> on(GetProductsQuery query) {
...@@ -25,4 +29,10 @@ public class ProductQueryHandler { ...@@ -25,4 +29,10 @@ public class ProductQueryHandler {
return productRepository.findAll(); return productRepository.findAll();
} }
@QueryHandler
protected List<ChannelEntity> on(GetChannelQuery query) {
log.info("---channel query---");
return channelRepository.findAll();
}
} }
package com.showcase.synapse.product.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.showcase.synapse.product.entity.ChannelEntity;
public interface ChannelRepository extends JpaRepository<ChannelEntity, String> {
}
...@@ -10,8 +10,11 @@ import org.axonframework.messaging.responsetypes.ResponseTypes; ...@@ -10,8 +10,11 @@ import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway; import org.axonframework.queryhandling.QueryGateway;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.showcase.synapse.product.command.CreateChannelCommand;
import com.showcase.synapse.product.command.CreateProductCommand; import com.showcase.synapse.product.command.CreateProductCommand;
import com.showcase.synapse.product.entity.ChannelEntity;
import com.showcase.synapse.product.entity.ProductEntity; import com.showcase.synapse.product.entity.ProductEntity;
import com.showcase.synapse.product.query.GetChannelQuery;
import com.showcase.synapse.product.query.GetProductsQuery; import com.showcase.synapse.product.query.GetProductsQuery;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -42,9 +45,27 @@ public class ProductService { ...@@ -42,9 +45,27 @@ public class ProductService {
return returnValue; return returnValue;
} }
public int createChannel(int id, String name, BigDecimal discountRate) {
log.info("[@Service createChannel] new CreateChannelCommand");
// command생성
CreateChannelCommand createChannelCommand = new CreateChannelCommand(
id, name, discountRate);
System.out.println("test");
// 여기
// 생성한 command전송(비동기)
int returnValue = commandGateway.sendAndWait(createChannelCommand);
System.out.printf("returnValue: %s \n", returnValue);
return returnValue;
}
public List<ProductEntity> getProducts() throws ExecutionException, InterruptedException { public List<ProductEntity> getProducts() throws ExecutionException, InterruptedException {
return queryGateway.query(new GetProductsQuery(), return queryGateway.query(new GetProductsQuery(),
ResponseTypes.multipleInstancesOf(ProductEntity.class)).get(); ResponseTypes.multipleInstancesOf(ProductEntity.class)).get();
} }
public List<ChannelEntity> getChannels() throws ExecutionException, InterruptedException {
return queryGateway.query(new GetChannelQuery(),
ResponseTypes.multipleInstancesOf(ChannelEntity.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