diff --git a/apps/main/tv/codely/apps/mooc/backend/controller/playground/DomainEventPostController.java b/apps/main/tv/codely/apps/mooc/backend/controller/playground/DomainEventPostController.java new file mode 100644 index 00000000..b221dc36 --- /dev/null +++ b/apps/main/tv/codely/apps/mooc/backend/controller/playground/DomainEventPostController.java @@ -0,0 +1,55 @@ +package tv.codely.apps.mooc.backend.controller.playground; + +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessagePropertiesBuilder; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import tv.codely.shared.domain.Utils; + +import java.io.Serializable; +import java.util.HashMap; + +@RestController +record DomainEventPostController(RabbitTemplate rabbitTemplate) { + @PostMapping(value = "/domain-events") + public ResponseEntity index(@RequestBody Request request) { + System.out.println(request.eventName()); + + var serializedEvent = Utils.jsonEncode(request.eventRaw()); + + Message message = new Message( + serializedEvent.getBytes(), + MessagePropertiesBuilder.newInstance().setContentEncoding("utf-8").setContentType("application/json").build() + ); + + rabbitTemplate.send("domain_events", request.eventName(), message); + + return new ResponseEntity<>(HttpStatus.CREATED); + } +} + +final class Request { + + private String eventName; + private Object eventRaw; + + public void setEventName(String eventName) { + this.eventName = eventName; + } + + public void setEventRaw(Object eventRaw) { + this.eventRaw = eventRaw; + } + + String eventName() { + return eventName; + } + + Object eventRaw() { + return eventRaw; + } +} diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml index 654ffd12..d2c1c99c 100755 --- a/docker-compose.ci.yml +++ b/docker-compose.ci.yml @@ -23,7 +23,7 @@ services: platform: linux/amd64 restart: unless-stopped ports: - - "5630:5672" + - "5672:5672" - "8090:15672" environment: - RABBITMQ_DEFAULT_USER=codely diff --git a/docker-compose.yml b/docker-compose.yml index 3da6fcb7..2dd4b00c 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,7 @@ services: platform: linux/amd64 restart: unless-stopped ports: - - "5630:5672" + - "5672:5672" - "8090:15672" environment: - RABBITMQ_DEFAULT_USER=codely @@ -91,23 +91,7 @@ services: - backoffice_elasticsearch command: ["./gradlew", "bootRun", "--args", "mooc_backend server"] - test_server_java: - container_name: codely-java_ddd_example-test_server - build: - context: . - dockerfile: Dockerfile - restart: unless-stopped - volumes: - - .:/app:delegated - - test_gradle_cache:/app/.gradle - depends_on: - - shared_mysql - - shared_rabbitmq - - backoffice_elasticsearch - tty: true - volumes: backoffice_backend_gradle_cache: backoffice_frontend_gradle_cache: mooc_backend_gradle_cache: - test_gradle_cache: diff --git a/doc/endpoints/backoffice_frontend.http b/etc/http/backoffice_frontend.http similarity index 100% rename from doc/endpoints/backoffice_frontend.http rename to etc/http/backoffice_frontend.http diff --git a/etc/http/publish_domain_events.http b/etc/http/publish_domain_events.http new file mode 100644 index 00000000..d6a19315 --- /dev/null +++ b/etc/http/publish_domain_events.http @@ -0,0 +1,20 @@ +POST http://localhost:8030/domain-events +Content-Type: application/json + +{ + "eventName": "course.created", + "eventRaw": { + "data": { + "id": "{{$random.uuid}}", + "type": "course.created", + "occurred_on": "2023-11-14 10:00:00", + "attributes": { + "id": "{{$random.uuid}}", + "name": "Demo course", + "duration": "2 days" + } + }, + "meta": { + } + } +} diff --git a/src/shared/main/tv/codely/shared/domain/Utils.java b/src/shared/main/tv/codely/shared/domain/Utils.java index 53dbc3ea..a3a56a6f 100644 --- a/src/shared/main/tv/codely/shared/domain/Utils.java +++ b/src/shared/main/tv/codely/shared/domain/Utils.java @@ -28,6 +28,14 @@ public static String jsonEncode(HashMap map) { } } + public static String jsonEncode(Object map) { + try { + return new ObjectMapper().writeValueAsString(map); + } catch (JsonProcessingException e) { + return ""; + } + } + public static HashMap jsonDecode(String body) { try { return new ObjectMapper().readValue(body, HashMap.class);