|
| 1 | +package io.axoniq.demo.orderfulfillment.controller; |
| 2 | + |
| 3 | +import io.axoniq.demo.orderfulfillment.history.JdbcWorkflowHistoryRepository; |
| 4 | +import io.axoniq.demo.orderfulfillment.history.WorkflowHistoryUpdates; |
| 5 | +import io.axoniq.framework.workflow.dsl.api.WorkflowStatus; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.MediaType; |
| 8 | +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| 9 | +import org.springframework.web.bind.annotation.GetMapping; |
| 10 | +import org.springframework.web.bind.annotation.PathVariable; |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestParam; |
| 13 | +import org.springframework.web.bind.annotation.RestController; |
| 14 | +import org.springframework.web.server.ResponseStatusException; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +@RestController |
| 20 | +@RequestMapping("/api/workflows") |
| 21 | +public class WorkflowHistoryController { |
| 22 | + |
| 23 | + private final JdbcWorkflowHistoryRepository history; |
| 24 | + private final WorkflowHistoryUpdates updates; |
| 25 | + |
| 26 | + public WorkflowHistoryController(JdbcWorkflowHistoryRepository history, WorkflowHistoryUpdates updates) { |
| 27 | + this.history = history; |
| 28 | + this.updates = updates; |
| 29 | + } |
| 30 | + |
| 31 | + @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
| 32 | + public SseEmitter stream() { |
| 33 | + return updates.subscribe(); |
| 34 | + } |
| 35 | + |
| 36 | + @GetMapping |
| 37 | + public Map<String, Object> list(@RequestParam(defaultValue = "") String search, |
| 38 | + @RequestParam(required = false) WorkflowStatus status, |
| 39 | + @RequestParam(defaultValue = "50") int limit, |
| 40 | + @RequestParam(defaultValue = "0") int offset) { |
| 41 | + if (limit < 1 || limit > 200 || offset < 0) { |
| 42 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Use limit 1–200 and offset >= 0"); |
| 43 | + } |
| 44 | + return history.list(search, status == null ? "" : status.name(), limit, offset); |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping("/{workflowId}") |
| 48 | + public Object detail(@PathVariable String workflowId) { |
| 49 | + return history.detail(workflowId) |
| 50 | + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Workflow not found")); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/{workflowId}/events") |
| 54 | + public List<Map<String, Object>> events(@PathVariable String workflowId, |
| 55 | + @RequestParam(defaultValue = "0") long after, |
| 56 | + @RequestParam(defaultValue = "200") int limit) { |
| 57 | + if (after < 0 || limit < 1 || limit > 500) { |
| 58 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Use limit 1–500 and after >= 0"); |
| 59 | + } |
| 60 | + detail(workflowId); |
| 61 | + return history.timeline(workflowId, after, limit); |
| 62 | + } |
| 63 | +} |
0 commit comments