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;
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;
......@@ -9,14 +8,14 @@ import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate;
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.event.ChannelCreatedEvent;
import com.showcase.synapse.product.event.ProductCreatedEvent;
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
@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;
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.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.entity.ChannelEntity;
import com.showcase.synapse.product.entity.ProductEntity;
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
@RequestMapping("/api/msa/product")
@RequestMapping("/api/msa")
@CrossOrigin(origins = "*")
public class ProductController {
......@@ -24,18 +30,25 @@ public class ProductController {
this.productService = productService;
}
@GetMapping("/")
@GetMapping("/product")
public ResponseEntity<List<ProductEntity>> getProducts() throws ExecutionException, InterruptedException {
List<ProductEntity> productEntities = productService.getProducts();
return ResponseEntity.ok(productEntities);
}
@GetMapping("/channel")
public ResponseEntity<List<ChannelEntity>> getChannels() throws ExecutionException, InterruptedException {
List<ChannelEntity> channelEntity = productService.getChannels();
return ResponseEntity.ok(channelEntity);
}
@PostMapping("/")
@PostMapping("/product")
public ResponseEntity<Map<String, String>> productCreate(@RequestBody ProductCreateDto productCreateDto) {
String productId = productService.createProduct(productCreateDto.getName(),
productCreateDto.getComment(),
BigDecimal.valueOf(productCreateDto.getPrice()),
productCreateDto.getPrice(),
productCreateDto.getChannel());
HashMap<String, String> m = new HashMap<>();
m.put("productId", productId);
......@@ -43,4 +56,16 @@ public class ProductController {
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;
import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;
......@@ -8,7 +10,8 @@ import lombok.Setter;
public class ProductCreateDto {
private String name;
private String comment;
private int price;
private BigDecimal price;
private int channel;
private BigDecimal discountRate;
}
package com.showcase.synapse.product.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
......@@ -18,10 +20,13 @@ import lombok.Setter;
@NoArgsConstructor
public class ChannelEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "channel_id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "discount_rate")
private BigDecimal discountRate;
}
......@@ -18,7 +18,7 @@ import lombok.Setter;
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProductEntity{
public class ProductEntity {
@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;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Component;
import com.showcase.synapse.product.entity.ChannelEntity;
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.ProductQuantityChangedEvent;
import com.showcase.synapse.product.repository.ChannelRepository;
import com.showcase.synapse.product.repository.ProductRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Component
@AllArgsConstructor
@Slf4j
public class ProductEventHandler {
private final ProductRepository productRepository;
private final ChannelRepository channelRepository;
@EventHandler
protected void saveProduct(ProductCreatedEvent productCreatedEvent) {
......@@ -32,9 +36,23 @@ public class ProductEventHandler {
ce.setId(productCreatedEvent.getChannel());
productEntity.setChannel(ce);
productRepository.save(productEntity);
productRepository.save(productEntity);
}
@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
protected void changeQuantity(ProductQuantityChangedEvent productQuantityChangedEvent) {
......
package com.showcase.synapse.product.query;
public class GetChannelQuery {
}
......@@ -5,8 +5,11 @@ import java.util.List;
import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Component;
import com.showcase.synapse.product.entity.ChannelEntity;
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.repository.ChannelRepository;
import com.showcase.synapse.product.repository.ProductRepository;
import lombok.RequiredArgsConstructor;
......@@ -18,11 +21,18 @@ import lombok.extern.slf4j.Slf4j;
public class ProductQueryHandler {
private final ProductRepository productRepository;
private final ChannelRepository channelRepository;
@QueryHandler
protected List<ProductEntity> on(GetProductsQuery query) {
log.info("---product query---");
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;
import org.axonframework.queryhandling.QueryGateway;
import org.springframework.stereotype.Service;
import com.showcase.synapse.product.command.CreateChannelCommand;
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.query.GetChannelQuery;
import com.showcase.synapse.product.query.GetProductsQuery;
import lombok.extern.slf4j.Slf4j;
......@@ -41,10 +44,28 @@ public class ProductService {
System.out.printf("returnValue: %s \n", 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 {
return queryGateway.query(new GetProductsQuery(),
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