add order service and view for list
liquibase view not add
This commit is contained in:
@ -10,4 +10,5 @@ public class CreateOrderCommand {
|
||||
private Long orderId;
|
||||
private String orderName;
|
||||
private String description;
|
||||
private Long userId;
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.example.mmad.testapp.command;
|
||||
|
||||
import com.example.mmad.testapp.entity.OrderEntity;
|
||||
import com.example.mmad.testapp.event.OrderCreatedEvent;
|
||||
import com.example.mmad.testapp.repository.UserRepository;
|
||||
import com.example.mmad.testapp.repository.OrderRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -11,22 +11,24 @@ import org.springframework.stereotype.Service;
|
||||
public class OrderCommandHandler {
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final UserRepository userRepository;
|
||||
private final OrderRepository orderRepository;
|
||||
|
||||
public OrderCommandHandler(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
public OrderCommandHandler(OrderRepository orderRepository) {
|
||||
this.orderRepository = orderRepository;
|
||||
}
|
||||
|
||||
public void createUser(CreateOrderCommand createUserCommand) {
|
||||
public void createUser(CreateOrderCommand createOrderCommand) {
|
||||
OrderEntity orderEntity = new OrderEntity();
|
||||
orderEntity.setId(createUserCommand.getOrderId());
|
||||
orderEntity.setOrderName(createUserCommand.getOrderName());
|
||||
orderEntity.setDescription(createUserCommand.getDescription());
|
||||
// userRepository.save(orderEntity);
|
||||
orderEntity.setId(createOrderCommand.getOrderId());
|
||||
orderEntity.setOrderName(createOrderCommand.getOrderName());
|
||||
orderEntity.setDescription(createOrderCommand.getDescription());
|
||||
orderEntity.setUserId(createOrderCommand.getUserId());
|
||||
orderRepository.save(orderEntity);
|
||||
OrderCreatedEvent event = new OrderCreatedEvent(
|
||||
orderEntity.getId(),
|
||||
orderEntity.getOrderName(),
|
||||
orderEntity.getDescription()
|
||||
orderEntity.getDescription(),
|
||||
orderEntity.getUserId()
|
||||
);
|
||||
applicationEventPublisher.publishEvent(event);
|
||||
}
|
||||
|
38
src/main/java/com/example/mmad/testapp/controller/OrderController.java
Executable file
38
src/main/java/com/example/mmad/testapp/controller/OrderController.java
Executable file
@ -0,0 +1,38 @@
|
||||
package com.example.mmad.testapp.controller;
|
||||
|
||||
import com.example.mmad.testapp.command.CreateOrderCommand;
|
||||
import com.example.mmad.testapp.command.OrderCommandHandler;
|
||||
import com.example.mmad.testapp.entity.view.ViewOrder;
|
||||
import com.example.mmad.testapp.service.ViewOrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
public class OrderController {
|
||||
|
||||
private OrderCommandHandler orderCommandHandler;
|
||||
private ViewOrderService viewOrderService;
|
||||
|
||||
@Autowired
|
||||
public void setPersonService(OrderCommandHandler orderCommandHandler, ViewOrderService viewOrderService) {
|
||||
this.orderCommandHandler = orderCommandHandler;
|
||||
this.viewOrderService = viewOrderService;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public ResponseEntity<List<ViewOrder>> list() {
|
||||
return ResponseEntity.ok(viewOrderService.listInfo());
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:3001")
|
||||
@PostMapping("/create")
|
||||
public ResponseEntity<?> create(@Valid @RequestBody CreateOrderCommand orderCommand) {
|
||||
orderCommandHandler.createUser(orderCommand);
|
||||
return ResponseEntity.ok(200);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import static jakarta.persistence.GenerationType.SEQUENCE;
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(schema = "testapp", name = "order")
|
||||
@Table(schema = "TEST2", name = "order")
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ -22,4 +22,6 @@ public class OrderEntity {
|
||||
private String orderName;
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
@Column(name="user_id")
|
||||
private Long userId;
|
||||
}
|
||||
|
25
src/main/java/com/example/mmad/testapp/entity/OrderQueryEntity.java
Executable file
25
src/main/java/com/example/mmad/testapp/entity/OrderQueryEntity.java
Executable file
@ -0,0 +1,25 @@
|
||||
package com.example.mmad.testapp.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import static jakarta.persistence.GenerationType.SEQUENCE;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(schema = "TEST2", name = "ORDER_QUERY")
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class OrderQueryEntity {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
@Column(name = "order_name")
|
||||
private String orderName;
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
@Column(name = "user_id")
|
||||
private Long userId;
|
||||
}
|
@ -8,7 +8,7 @@ import static jakarta.persistence.GenerationType.SEQUENCE;
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(schema = "testapp", name = "person")
|
||||
@Table(schema = "TEST2", name = "person")
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
|
24
src/main/java/com/example/mmad/testapp/entity/view/ViewOrder.java
Executable file
24
src/main/java/com/example/mmad/testapp/entity/view/ViewOrder.java
Executable file
@ -0,0 +1,24 @@
|
||||
package com.example.mmad.testapp.entity.view;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import static jakarta.persistence.GenerationType.SEQUENCE;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(schema = "TEST2", name = "order_view")
|
||||
@Immutable
|
||||
public class ViewOrder {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
@Column(name = "order_name")
|
||||
private String orderName;
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
@Column(name="user_id")
|
||||
private Long userId;
|
||||
}
|
@ -4,11 +4,14 @@ public class OrderCreatedEvent {
|
||||
private final Long orderId;
|
||||
private final String orderName;
|
||||
private final String description;
|
||||
private final Long userId;
|
||||
|
||||
public OrderCreatedEvent(Long orderId, String orderName, String description) {
|
||||
//todo change to annotation getter setter
|
||||
public OrderCreatedEvent(Long orderId, String orderName, String description, Long userId) {
|
||||
this.orderId = orderId;
|
||||
this.orderName = orderName;
|
||||
this.description = description;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
@ -22,4 +25,8 @@ public class OrderCreatedEvent {
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,27 @@
|
||||
package com.example.mmad.testapp.query;
|
||||
|
||||
import com.example.mmad.testapp.entity.OrderQueryEntity;
|
||||
import com.example.mmad.testapp.event.OrderCreatedEvent;
|
||||
import com.example.mmad.testapp.repository.OrderReadRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserQueryHandler {
|
||||
@Autowired
|
||||
private OrderReadRepository orderReadRepository;
|
||||
|
||||
@Async("taskExecutor")
|
||||
@EventListener
|
||||
public void handelUserCreated(OrderCreatedEvent event) {
|
||||
OrderQueryEntity orderQueryEntity = new OrderQueryEntity();
|
||||
orderQueryEntity.setId(event.getOrderId());
|
||||
orderQueryEntity.setOrderName(event.getOrderName());
|
||||
orderQueryEntity.setDescription(event.getDescription());
|
||||
orderQueryEntity.setUserId(event.getUserId());
|
||||
orderReadRepository.save(orderQueryEntity);
|
||||
System.out.println("Received user created event" + event.getOrderId());
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.example.mmad.testapp.repository;
|
||||
|
||||
import com.example.mmad.testapp.entity.OrderQueryEntity;
|
||||
import com.example.mmad.testapp.entity.PersonEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserReadRepository extends JpaRepository<PersonEntity, Long> {
|
||||
public interface OrderReadRepository extends JpaRepository<OrderQueryEntity, Long> {
|
||||
}
|
@ -5,6 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<OrderEntity, Long> {
|
||||
public interface OrderRepository extends JpaRepository<OrderEntity, Long> {
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.mmad.testapp.repository;
|
||||
|
||||
import com.example.mmad.testapp.entity.view.ViewOrder;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ViewOrderRepository extends JpaRepository<ViewOrder, Long> {
|
||||
List<ViewOrder> findAll();
|
||||
}
|
25
src/main/java/com/example/mmad/testapp/service/ViewOrderService.java
Executable file
25
src/main/java/com/example/mmad/testapp/service/ViewOrderService.java
Executable file
@ -0,0 +1,25 @@
|
||||
package com.example.mmad.testapp.service;
|
||||
|
||||
import com.example.mmad.testapp.entity.view.ViewOrder;
|
||||
import com.example.mmad.testapp.repository.ViewOrderRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ViewOrderService {
|
||||
|
||||
private final ViewOrderRepository viewOrderRepository;
|
||||
|
||||
public ViewOrderService(ViewOrderRepository viewOrderRepository) {
|
||||
this.viewOrderRepository = viewOrderRepository;
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ViewOrder> listInfo() {
|
||||
return viewOrderRepository.findAll();
|
||||
}
|
||||
|
||||
}
|
@ -27,4 +27,12 @@
|
||||
</createTable>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="1.2-3-mmad" author="mmad">
|
||||
<addColumn tableName="order">
|
||||
<column name="user_id" type="number(19)" remarks="شناسه کاربر">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</addColumn>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
Reference in New Issue
Block a user