generated from CodelyTV/java-basic-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint to publish raw domain events
- Loading branch information
1 parent
c577764
commit 27d8146
Showing
6 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
apps/main/tv/codely/apps/mooc/backend/controller/playground/DomainEventPostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters