From bae5135bd82fb89cbae5e5ab858bf61f520dd6f4 Mon Sep 17 00:00:00 2001 From: Akito123321 Date: Mon, 23 Sep 2024 20:14:03 +0200 Subject: [PATCH] progress --- .../valoStrats/rest/controller/GroupRestAPI.java | 13 +++++++++++-- .../valoStrats/rest/requests/StratRequest.java | 4 +++- .../valoStrats/rest/util/GlobalErrorHandler.java | 10 ++++++++++ .../me/akito123321/valoStrats/schemas/Strat.java | 13 +++++++++++-- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/akito123321/valoStrats/rest/util/GlobalErrorHandler.java diff --git a/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java b/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java index 6fabe0d..15be96e 100644 --- a/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java +++ b/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java @@ -137,6 +137,7 @@ public class GroupRestAPI { @PostMapping("/{id}/strat-type") public ResponseEntity addStratType(@PathVariable String id, @RequestBody String typeName) { StratsUser user = getUser(); + typeName = typeName.replace("\"", ""); Group group = groupService.getGroupById(id); if (group == null) { @@ -218,6 +219,8 @@ public class GroupRestAPI { StratsUser user = getUser(); Group group = groupService.getGroupById(id); + Map map = mapService.getMapById(stratRequest.mapId()); + if (group == null) { return ResponseEntity.notFound().build(); } @@ -225,7 +228,7 @@ public class GroupRestAPI { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } - Strat strat = new Strat(stratRequest.title(), stratRequest.description(), group, stratRequest.stratType()); + Strat strat = new Strat(stratRequest.title(), stratRequest.description(), group, stratRequest.stratType(), map); if(stratRequest.attempts() != null) { strat.setAttempts(stratRequest.attempts()); @@ -233,7 +236,7 @@ public class GroupRestAPI { if(stratRequest.success() != null) { strat.setSuccess(stratRequest.success()); } - if(!group.getStratTypes().contains(stratRequest.stratType())) { + if(stratRequest.stratType() != null && stratRequest.stratType().length() > 0 && !group.getStratTypes().contains(stratRequest.stratType())) { group.getStratTypes().add(stratRequest.stratType()); } @@ -277,6 +280,8 @@ public class GroupRestAPI { Group group = groupService.getGroupById(id); Strat strat = stratService.getStratById(stratId); + Map map = mapService.getMapById(newStrat.mapId()); + if (group == null | strat == null) { return ResponseEntity.notFound().build(); } @@ -291,6 +296,7 @@ public class GroupRestAPI { strat.setTitle(newStrat.title()); strat.setStratStates(newStrat.stratStates()); strat.setStratType(newStrat.stratType()); + if(newStrat.attempts() != null) { strat.setAttempts(newStrat.attempts()); } @@ -300,6 +306,9 @@ public class GroupRestAPI { if(!group.getStratTypes().contains(newStrat.stratType())) { group.getStratTypes().add(newStrat.stratType()); } + if(map != null) { + strat.setMap(map); + } stratService.saveStrat(strat); diff --git a/src/main/java/me/akito123321/valoStrats/rest/requests/StratRequest.java b/src/main/java/me/akito123321/valoStrats/rest/requests/StratRequest.java index 6d34278..2e8b902 100644 --- a/src/main/java/me/akito123321/valoStrats/rest/requests/StratRequest.java +++ b/src/main/java/me/akito123321/valoStrats/rest/requests/StratRequest.java @@ -5,12 +5,14 @@ import java.util.List; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import me.akito123321.valoStrats.schemas.Map; import me.akito123321.valoStrats.schemas.StratState; public record StratRequest( @Size(max = 50, message = "{title can not be longer than 50 chars}") @NotBlank String title, @Size(max = 512, message = "{description can not be longer than 50 chars}") @NotBlank String description, - @Size(max = 50, message = "{title stratType can not be than 50 chars}") @NotBlank String stratType, + @Size(max = 50, message = "{title stratType can not be than 50 chars}") String stratType, + @NotNull String mapId, @NotNull List stratStates, Integer attempts, Integer success) { diff --git a/src/main/java/me/akito123321/valoStrats/rest/util/GlobalErrorHandler.java b/src/main/java/me/akito123321/valoStrats/rest/util/GlobalErrorHandler.java new file mode 100644 index 0000000..7dacf93 --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/rest/util/GlobalErrorHandler.java @@ -0,0 +1,10 @@ +package me.akito123321.valoStrats.rest.util; + +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@RestControllerAdvice +public class GlobalErrorHandler extends ResponseEntityExceptionHandler { + +} + diff --git a/src/main/java/me/akito123321/valoStrats/schemas/Strat.java b/src/main/java/me/akito123321/valoStrats/schemas/Strat.java index cbfd060..6a62f3f 100644 --- a/src/main/java/me/akito123321/valoStrats/schemas/Strat.java +++ b/src/main/java/me/akito123321/valoStrats/schemas/Strat.java @@ -31,14 +31,15 @@ public class Strat { @OneToMany(fetch = FetchType.EAGER) private List playerTypes = new ArrayList(); - @ManyToOne + @ManyToOne(fetch = FetchType.EAGER) private Map map; - public Strat(String title, String description, Group group, String stratType) { + public Strat(String title, String description, Group group, String stratType, Map map) { this.title = title; this.description = description; this.group = group; this.stratType = stratType; + this.map = map; } public Strat() { @@ -97,6 +98,14 @@ public class Strat { this.success = success; } + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + public List getPlayerTypes() { return playerTypes; }