-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PaymentService.java
executable file
·98 lines (82 loc) · 3.19 KB
/
PaymentService.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
package payment.producer;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
/**
*
* @author pthomas3
*/
@Configuration
@EnableAutoConfiguration
public class PaymentService {
@RestController
@RequestMapping("/payments")
class PaymentController {
private final AtomicInteger counter = new AtomicInteger();
private final Map<Integer, Payment> payments = new ConcurrentHashMap();
@PostMapping
public Payment create(@RequestBody Payment payment) {
int id = counter.incrementAndGet();
payment.setId(id);
payments.put(id, payment);
return payment;
}
@PutMapping("/{id:.+}")
public Payment update(@PathVariable("id") int id, @RequestBody Payment payment) {
payments.put(id, payment);
return payment;
}
@GetMapping
public Collection<Payment> list() {
return payments.values();
}
@GetMapping("/{id:.+}")
public Payment get(@PathVariable("id") int id) {
Payment payment = payments.get(id);
if (payment == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return payment;
}
@DeleteMapping("/{id:.+}")
public void delete(@PathVariable("id") int id) {
Payment payment = payments.remove(id);
if (payment == null) {
throw new RuntimeException("payment not found, id: " + id);
}
}
}
public static ConfigurableApplicationContext start(int port) {
return SpringApplication.run(PaymentService.class, new String[]{"--server.port=" + port});
}
public static void stop(ConfigurableApplicationContext context) {
SpringApplication.exit(context, () -> 0);
}
public static int getPort(ConfigurableApplicationContext context) {
ServerStartedInitializingBean ss = context.getBean(ServerStartedInitializingBean.class);
return ss.getLocalPort();
}
@Bean
public ServerStartedInitializingBean getInitializingBean() {
return new ServerStartedInitializingBean();
}
public static void main(String[] args) {
start(8090);
}
}