Skip to content

Commit e356586

Browse files
committed
feat: allow bulk patch starts
1 parent 912d318 commit e356586

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/main/java/io/papermc/patchroulette/controller/RESTController.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,28 @@ private String getUser(final Authentication authentication) {
9090
)
9191
public ResponseEntity<String> startPatch(final Authentication auth, @RequestBody final PatchId input) {
9292
final String user = this.getUser(auth);
93-
this.patchService.startWorkOnPatch(input, user);
93+
final List<String> result = this.patchService.startWorkOnPatches(input.getMinecraftVersion(), List.of(input.getPath()), user);
94+
if (result.isEmpty()) {
95+
return ResponseEntity.status(HttpStatus.CONFLICT).body("Patch not available.");
96+
}
9497
return ResponseEntity.ok("Patch started.");
9598
}
9699

100+
@PreAuthorize("hasRole('PATCH')")
101+
@PostMapping(
102+
value = "/start-patches",
103+
consumes = "application/json",
104+
produces = "application/json"
105+
)
106+
public ResponseEntity<?> startPatch(final Authentication auth, @RequestBody final Patches input) {
107+
final String user = this.getUser(auth);
108+
final List<String> result = this.patchService.startWorkOnPatches(input.minecraftVersion(), input.paths(), user);
109+
if (result.isEmpty()) {
110+
return ResponseEntity.status(HttpStatus.CONFLICT).body("None of the patches are available.");
111+
}
112+
return ResponseEntity.ok(result);
113+
}
114+
97115
@PreAuthorize("hasRole('PATCH')")
98116
@PostMapping(
99117
value = "/complete-patch",

src/main/java/io/papermc/patchroulette/model/PatchId.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ public class PatchId implements Serializable {
1313
public PatchId() {
1414
}
1515

16+
public String getMinecraftVersion() {
17+
return minecraftVersion;
18+
}
19+
20+
public String getPath() {
21+
return path;
22+
}
23+
1624
@JsonCreator
1725
public PatchId(@JsonProperty("minecraftVersion") final String minecraftVersion,
1826
@JsonProperty("path") final String path) {

src/main/java/io/papermc/patchroulette/service/PatchService.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.papermc.patchroulette.model.PatchId;
55
import io.papermc.patchroulette.model.Status;
66
import io.papermc.patchroulette.repository.PatchRepository;
7+
8+
import java.util.ArrayList;
79
import java.util.List;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.stereotype.Service;
@@ -41,17 +43,23 @@ public List<Patch> getAllPatches(final String minecraftVersion) {
4143
}
4244

4345
@Transactional
44-
public void startWorkOnPatch(final PatchId patchId, final String user) {
45-
final Patch patch = this.patchRepository.getReferenceById(patchId);
46-
if (patch.getStatus() != Status.AVAILABLE) {
47-
throw new IllegalStateException("Patch " + patchId + " is not available");
48-
}
49-
if (patch.getResponsibleUser() != null) {
50-
throw new IllegalStateException("Patch " + patchId + " is already claimed by another user");
46+
public List<String> startWorkOnPatches(final String minecraftVersion, final List<String> patches, final String user) {
47+
final List<String> startedPatches = new ArrayList<>();
48+
for (final String path : patches) {
49+
final PatchId patchId = new PatchId(minecraftVersion, path);
50+
final Patch patch = this.patchRepository.getReferenceById(patchId);
51+
if (patch.getStatus() != Status.AVAILABLE) {
52+
continue;
53+
}
54+
if (patch.getResponsibleUser() != null) {
55+
continue;
56+
}
57+
patch.setStatus(Status.WIP);
58+
patch.setResponsibleUser(user);
59+
this.patchRepository.save(patch);
60+
startedPatches.add(path);
5161
}
52-
patch.setStatus(Status.WIP);
53-
patch.setResponsibleUser(user);
54-
this.patchRepository.save(patch);
62+
return startedPatches;
5563
}
5664

5765
@Transactional

0 commit comments

Comments
 (0)