implement base of cqrs pattern with applicationEventPublisher for command and query inside one microservice
structure of package
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
package com.example.mmad.testapp.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CreateUserCommand {
|
||||
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String email;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.example.mmad.testapp.command;
|
||||
|
||||
import com.example.mmad.testapp.entity.UserEntity;
|
||||
import com.example.mmad.testapp.event.UserCreatedEvent;
|
||||
import com.example.mmad.testapp.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserCommandHandler {
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserCommandHandler(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public void createUser(CreateUserCommand createUserCommand) {
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setId(createUserCommand.getUserId());
|
||||
userEntity.setUserName(createUserCommand.getUserName());
|
||||
userEntity.setEmail(createUserCommand.getEmail());
|
||||
// userRepository.save(userEntity);
|
||||
UserCreatedEvent event = new UserCreatedEvent(
|
||||
userEntity.getId(),
|
||||
userEntity.getUserName(),
|
||||
userEntity.getEmail()
|
||||
);
|
||||
applicationEventPublisher.publishEvent(event);
|
||||
}
|
||||
}
|
35
src/main/java/com/example/mmad/testapp/controller/UserController.java
Executable file
35
src/main/java/com/example/mmad/testapp/controller/UserController.java
Executable file
@ -0,0 +1,35 @@
|
||||
package com.example.mmad.testapp.controller;
|
||||
|
||||
import com.example.mmad.testapp.command.CreateUserCommand;
|
||||
import com.example.mmad.testapp.command.UserCommandHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
private UserCommandHandler userCommandHandler;
|
||||
|
||||
@Autowired
|
||||
public void setPersonService(UserCommandHandler userCommandHandler) {
|
||||
this.userCommandHandler = userCommandHandler;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public String listPosts() {
|
||||
return "userHandler";
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:3001")
|
||||
@PostMapping("/create")
|
||||
public ResponseEntity<?> create(@Valid @RequestBody CreateUserCommand userCommand) {
|
||||
userCommandHandler.createUser(userCommand);
|
||||
return ResponseEntity.ok(200);
|
||||
}
|
||||
}
|
25
src/main/java/com/example/mmad/testapp/entity/UserEntity.java
Executable file
25
src/main/java/com/example/mmad/testapp/entity/UserEntity.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 = "testapp", name = "user")
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserEntity {
|
||||
|
||||
@Id
|
||||
@SequenceGenerator(name = "seqTest", sequenceName = "testapp.person_seq", allocationSize = 1)
|
||||
@GeneratedValue(strategy = SEQUENCE, generator = "seqTest")
|
||||
private Long id;
|
||||
@Column(name = "user_name")
|
||||
private String userName;
|
||||
@Column(name = "email")
|
||||
private String email;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.example.mmad.testapp.event;
|
||||
|
||||
public class UserCreatedEvent {
|
||||
private final Long userId;
|
||||
private final String username;
|
||||
private final String email;
|
||||
|
||||
public UserCreatedEvent(Long userId, String username, String email) {
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.mmad.testapp.query;
|
||||
|
||||
import com.example.mmad.testapp.event.UserCreatedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserQueryHandler {
|
||||
@EventListener
|
||||
public void handelUserCreated(UserCreatedEvent event) {
|
||||
System.out.println("Received user created event" + event.getUserId());
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.example.mmad.testapp.query;
|
||||
|
||||
public class UserReadModel {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.mmad.testapp.repository;
|
||||
|
||||
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> {
|
||||
}
|
10
src/main/java/com/example/mmad/testapp/repository/UserRepository.java
Executable file
10
src/main/java/com/example/mmad/testapp/repository/UserRepository.java
Executable file
@ -0,0 +1,10 @@
|
||||
package com.example.mmad.testapp.repository;
|
||||
|
||||
import com.example.mmad.testapp.entity.UserEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
|
||||
}
|
Reference in New Issue
Block a user