Skip to content

Commit

Permalink
feat: add endpoint to publish raw domain events
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 13, 2023
1 parent c577764 commit 27d8146
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
2 changes: 1 addition & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down
18 changes: 1 addition & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down Expand Up @@ -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:
File renamed without changes.
20 changes: 20 additions & 0 deletions etc/http/publish_domain_events.http
Original file line number Diff line number Diff line change
@@ -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": {
}
}
}
8 changes: 8 additions & 0 deletions src/shared/main/tv/codely/shared/domain/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public static String jsonEncode(HashMap<String, Serializable> map) {
}
}

public static String jsonEncode(Object map) {
try {
return new ObjectMapper().writeValueAsString(map);
} catch (JsonProcessingException e) {
return "";
}
}

public static HashMap<String, Serializable> jsonDecode(String body) {
try {
return new ObjectMapper().readValue(body, HashMap.class);
Expand Down

0 comments on commit 27d8146

Please sign in to comment.