-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRESTController.java
More file actions
157 lines (138 loc) · 5.88 KB
/
RESTController.java
File metadata and controls
157 lines (138 loc) · 5.88 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package io.papermc.patchroulette.controller;
import io.papermc.patchroulette.model.PatchId;
import io.papermc.patchroulette.service.PatchService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RESTController {
private final PatchService patchService;
@Autowired
public RESTController(final PatchService patchService) {
this.patchService = patchService;
}
public record PatchInfo(String path, Integer size) {}
@PreAuthorize("hasRole('PATCH')")
@GetMapping(
value = "/get-available-patches",
produces = "application/json"
)
public ResponseEntity<List<PatchInfo>> getAvailablePatches(@RequestParam final String minecraftVersion) {
return ResponseEntity.ok(
this.patchService.getAvailablePatches(minecraftVersion).stream()
.map(patch -> new PatchInfo(patch.getPath(), patch.getSize()))
.toList()
);
}
public record PatchDetails(String path, String status, String responsibleUser, Integer size) {}
@PreAuthorize("hasRole('PATCH')")
@GetMapping(
value = "/get-all-patches",
produces = "application/json"
)
public ResponseEntity<List<PatchDetails>> getAllPatches(@RequestParam final String minecraftVersion) {
return ResponseEntity.ok(
this.patchService.getAllPatches(minecraftVersion).stream()
.map(patch -> new PatchDetails(patch.getPath(), patch.getStatus().name(), patch.getResponsibleUser(), patch.getSize()))
.toList()
);
}
public record Patches(String minecraftVersion, List<PatchInfo> patches) {}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/set-patches",
consumes = "application/json",
produces = "text/plain"
)
public ResponseEntity<String> setPatches(@RequestBody final Patches input) {
this.patchService.setPatches(input.minecraftVersion(), input.patches());
return ResponseEntity.ok("Patches set.");
}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/clear-patches",
consumes = "text/plain",
produces = "text/plain"
)
public ResponseEntity<String> clearPatches(@RequestBody final String minecraftVersion) {
this.patchService.clearPatches(minecraftVersion);
return ResponseEntity.ok("Patches cleared.");
}
private String getUser(final Authentication authentication) {
return authentication.getName();
}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/start-patch",
consumes = "application/json",
produces = "text/plain"
)
public ResponseEntity<String> startPatch(final Authentication auth, @RequestBody final PatchId input) {
final String user = this.getUser(auth);
final List<String> result = this.patchService.startWorkOnPatches(input.getMinecraftVersion(), List.of(input.getPath()), user);
if (result.isEmpty()) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("Patch not available.");
}
return ResponseEntity.ok("Patch started.");
}
public record PatchList(String minecraftVersion, List<String> patches) {}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/start-patches",
consumes = "application/json",
produces = "application/json"
)
public ResponseEntity<?> startPatch(final Authentication auth, @RequestBody final PatchList input) {
final String user = this.getUser(auth);
final List<String> result = this.patchService.startWorkOnPatches(input.minecraftVersion(), input.patches(), user);
if (result.isEmpty()) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("None of the patches are available.");
}
return ResponseEntity.ok(result);
}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/complete-patch",
consumes = "application/json",
produces = "text/plain"
)
public ResponseEntity<String> completePatch(final Authentication auth, @RequestBody final PatchId input) {
final String user = this.getUser(auth);
this.patchService.finishWorkOnPatch(input, user);
return ResponseEntity.ok("Patch finished.");
}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/cancel-patch",
consumes = "application/json",
produces = "text/plain"
)
public ResponseEntity<String> cancelPatch(@RequestBody final PatchId input) {
this.patchService.cancelWorkOnPatch(input);
return ResponseEntity.ok("Patch cancelled.");
}
@PreAuthorize("hasRole('PATCH')")
@PostMapping(
value = "/undo-patch",
consumes = "application/json",
produces = "text/plain"
)
public ResponseEntity<String> undoPatch(final Authentication auth, @RequestBody final PatchId input) {
final String user = this.getUser(auth);
this.patchService.undoPatch(input, user);
return ResponseEntity.ok("Patch moved to WIP.");
}
@ExceptionHandler(IllegalStateException.class)
public ResponseEntity<String> handleException(final IllegalStateException e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
}
}