-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemController.java
More file actions
85 lines (70 loc) · 2.55 KB
/
SystemController.java
File metadata and controls
85 lines (70 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package in.newdevpoint.bootcamp.controller;
import in.newdevpoint.bootcamp.data.SampleData;
import in.newdevpoint.bootcamp.usecase.OrderService;
import in.newdevpoint.bootcamp.usecase.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/system")
// @PreAuthorize(RoleConstants.ADMIN_CRUD)
public class SystemController {
private final Environment environment;
@Autowired SystemService systemService;
@Autowired private OrderService orderService;
public SystemController(Environment environment) {
this.environment = environment;
}
@GetMapping("/active-profile")
public String getActiveProfile() {
String googleMapKey = environment.getProperty("google.map.key");
String apiKey = environment.getProperty("API_KEY");
return "Active profile: "
+ String.join(", ", environment.getActiveProfiles())
+ "\n"
+ googleMapKey
+ "\n"
+ apiKey;
}
@GetMapping("/external-rest-api")
public Object fetchExternalApi() {
try {
return systemService.fetchExternalApi();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Data Not Found");
}
}
@GetMapping("/place-order")
public Object placeOrder() {
String orderInfo = orderService.placeOrder();
if (orderInfo == null) {
// If order failed then initiateRefund and let say we don't
// need to wait for refund operation response because it's long process and
// doesn't require anywhere it's response then we can use @Async
orderService.initiateRefund();
} else {
// After order place send email/notification to user
// We don't want to wait for response from email API
// so we will use @Async method for that
orderService.sendOrderConfirmationEmail(SampleData.emailList, orderInfo);
}
return new ResponseEntity<>(orderInfo, HttpStatus.OK);
}
@GetMapping("/read-resource-file")
public Object readResourceFile() {
String orderInfo = systemService.readFile();
return new ResponseEntity<>(orderInfo, HttpStatus.OK);
}
@GetMapping("/process")
public String processRequest() {
try {
// Simulating a long-running task
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Processed by " + Thread.currentThread().getName();
}
}