|
| 1 | +/* |
| 2 | + * Copyright 2025 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.springboot4.examples.producer; |
| 15 | + |
| 16 | +import io.dapr.spring.workflows.config.EnableDaprWorkflows; |
| 17 | +import io.dapr.springboot.examples.producer.workflow.CustomerWorkflow; |
| 18 | +import io.dapr.workflows.client.DaprWorkflowClient; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.web.bind.annotation.GetMapping; |
| 23 | +import org.springframework.web.bind.annotation.PostMapping; |
| 24 | +import org.springframework.web.bind.annotation.RequestBody; |
| 25 | +import org.springframework.web.bind.annotation.RestController; |
| 26 | + |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +@RestController |
| 32 | +@EnableDaprWorkflows |
| 33 | +public class CustomersRestController { |
| 34 | + |
| 35 | + |
| 36 | + private final Logger logger = LoggerFactory.getLogger(CustomersRestController.class); |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private DaprWorkflowClient daprWorkflowClient; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private CustomerStore customerStore; |
| 43 | + |
| 44 | + @GetMapping("/") |
| 45 | + public String root() { |
| 46 | + return "OK"; |
| 47 | + } |
| 48 | + |
| 49 | + private Map<String, String> customersWorkflows = new HashMap<>(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Track customer endpoint. |
| 53 | + * |
| 54 | + * @param customer provided customer to track |
| 55 | + * @return confirmation that the workflow instance was created for a given customer |
| 56 | + */ |
| 57 | + @PostMapping("/customers") |
| 58 | + public String trackCustomer(@RequestBody Customer customer) { |
| 59 | + String instanceId = daprWorkflowClient.scheduleNewWorkflow(CustomerWorkflow.class, customer); |
| 60 | + logger.info("Workflow instance " + instanceId + " started"); |
| 61 | + customersWorkflows.put(customer.getCustomerName(), instanceId); |
| 62 | + return "New Workflow Instance created for Customer: " + customer.getCustomerName(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Request customer follow-up. |
| 67 | + * @param customer associated with a workflow instance |
| 68 | + * @return confirmation that the follow-up was requested |
| 69 | + */ |
| 70 | + @PostMapping("/customers/followup") |
| 71 | + public String customerNotification(@RequestBody Customer customer) { |
| 72 | + logger.info("Customer follow-up requested: " + customer.getCustomerName()); |
| 73 | + String workflowIdForCustomer = customersWorkflows.get(customer.getCustomerName()); |
| 74 | + if (workflowIdForCustomer == null || workflowIdForCustomer.isEmpty()) { |
| 75 | + return "There is no workflow associated with customer: " + customer.getCustomerName(); |
| 76 | + } else { |
| 77 | + daprWorkflowClient.raiseEvent(workflowIdForCustomer, "CustomerReachOut", customer); |
| 78 | + return "Customer Follow-up requested"; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public Collection<Customer> getCustomers() { |
| 84 | + return customerStore.getCustomers(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +} |
| 89 | + |
0 commit comments