diff --git a/src/main/java/me/akito123321/valoStrats/db/repositories/LineupRepository.java b/src/main/java/me/akito123321/valoStrats/db/repositories/LineupRepository.java new file mode 100644 index 0000000..f9d9f02 --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/db/repositories/LineupRepository.java @@ -0,0 +1,9 @@ +package me.akito123321.valoStrats.db.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; + +import me.akito123321.valoStrats.schemas.Lineup; + +public interface LineupRepository extends JpaRepository{ + +} diff --git a/src/main/java/me/akito123321/valoStrats/db/repositories/PlayerTypeRepository.java b/src/main/java/me/akito123321/valoStrats/db/repositories/PlayerTypeRepository.java new file mode 100644 index 0000000..63841d2 --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/db/repositories/PlayerTypeRepository.java @@ -0,0 +1,9 @@ +package me.akito123321.valoStrats.db.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; + +import me.akito123321.valoStrats.schemas.PlayerType; + +public interface PlayerTypeRepository extends JpaRepository{ + +} 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 d8fc34a..522f46f 100644 --- a/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java +++ b/src/main/java/me/akito123321/valoStrats/rest/controller/GroupRestAPI.java @@ -17,17 +17,23 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import me.akito123321.valoStrats.rest.requests.GroupRequest; +import me.akito123321.valoStrats.rest.requests.LineupRequest; import me.akito123321.valoStrats.rest.requests.MapRequest; +import me.akito123321.valoStrats.rest.requests.PlayerTypeRequest; import me.akito123321.valoStrats.rest.requests.StratRequest; import me.akito123321.valoStrats.rest.requests.StratStateRequest; import me.akito123321.valoStrats.rest.services.GroupService; +import me.akito123321.valoStrats.rest.services.LineupService; import me.akito123321.valoStrats.rest.services.MapService; +import me.akito123321.valoStrats.rest.services.PlayerTypeService; import me.akito123321.valoStrats.rest.services.ProfileService; import me.akito123321.valoStrats.rest.services.StratService; import me.akito123321.valoStrats.rest.services.StratStateService; import me.akito123321.valoStrats.rest.util.JDBCUserDetailsService; import me.akito123321.valoStrats.schemas.Group; +import me.akito123321.valoStrats.schemas.Lineup; import me.akito123321.valoStrats.schemas.Map; +import me.akito123321.valoStrats.schemas.PlayerType; import me.akito123321.valoStrats.schemas.Profile; import me.akito123321.valoStrats.schemas.Strat; import me.akito123321.valoStrats.schemas.StratState; @@ -37,6 +43,12 @@ import me.akito123321.valoStrats.schemas.StratsUser; @RequestMapping("/api/group") public class GroupRestAPI { + @Autowired + private PlayerTypeService playerTypeService; + + @Autowired + private LineupService lineupService; + @Autowired private ProfileService profileService; @@ -179,6 +191,9 @@ public class GroupRestAPI { if(stratRequest.success() != null) { strat.setSuccess(stratRequest.success()); } + if(!group.getStratTypes().contains(stratRequest.stratType())) { + group.getStratTypes().add(stratRequest.stratType()); + } stratService.saveStrat(strat); @@ -240,6 +255,9 @@ public class GroupRestAPI { if(newStrat.success() != null) { strat.setSuccess(newStrat.success()); } + if(!group.getStratTypes().contains(newStrat.stratType())) { + group.getStratTypes().add(newStrat.stratType()); + } stratService.saveStrat(strat); @@ -394,6 +412,187 @@ public class GroupRestAPI { return ResponseEntity.ok(group); } + @PostMapping("/{id}/strat/{stratId}/player-type") + public ResponseEntity addPlayerType(@PathVariable String id, @PathVariable String stratId , @RequestBody PlayerTypeRequest playerTypeRequest) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + + if (group == null | strat == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat)) { + return ResponseEntity.badRequest().build(); + } + + PlayerType playerType = new PlayerType(playerTypeRequest.name(), playerTypeRequest.task()); + + playerTypeService.savePlayerType(playerType); + + strat.getPlayerTypes().add(playerType); + + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @DeleteMapping("/{id}/strat/{stratId}/player-type/{playerTypeId}") + public ResponseEntity removePlayerType(@PathVariable String id, @PathVariable String stratId, @PathVariable String playerTypeId) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + PlayerType playerType = playerTypeService.getPlayerTypeById(playerTypeId); + + if (group == null | strat == null | playerType == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getPlayerTypes().contains(playerType)) { + return ResponseEntity.badRequest().build(); + } + + playerTypeService.removePlayerType(playerType); + + strat.getPlayerTypes().remove(playerType); + + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @PutMapping("/{id}/strat/{stratId}/player-type/{playerTypeId}") + public ResponseEntity editPlayerType(@PathVariable String id, @PathVariable String stratId, @PathVariable String playerTypeId, @RequestBody PlayerTypeRequest newPlayerType) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + PlayerType playerType = playerTypeService.getPlayerTypeById(playerTypeId); + + if (group == null | strat == null | playerType == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getPlayerTypes().contains(playerType)) { + return ResponseEntity.badRequest().build(); + } + + playerType.setName(newPlayerType.name()); + playerType.setTask(newPlayerType.task()); + + playerTypeService.savePlayerType(playerType); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @PostMapping("/{id}/strat/{stratId}/player-type/{playerTypeId}/profile") + public ResponseEntity addProfile(@PathVariable String id, @PathVariable String stratId, @PathVariable String playerTypeId , @RequestBody ProfileRequest profileRequest) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + PlayerType playerType = playerTypeService.getPlayerTypeById(playerTypeId); + + if (group == null | strat == null| playerTypeId == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) && !strat.getPlayerTypes().contains(playerType)) { + return ResponseEntity.badRequest().build(); + } + + Profile profile = new Profile(profileRequest.name(), profileRequest.image()); + + profileService.saveProfile(profile); + + playerType.getProfiles().add(profile); + + playerTypeService.savePlayerType(playerType); + stratService.saveStrat(strat); + + if(!group.getProfiles().contains(profile)) { + group.getProfiles().add(profile); + } + + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @DeleteMapping("/{id}/strat/{stratId}/player-type/{playerTypeId}/profile/{profileId}") + public ResponseEntity removeProfile(@PathVariable String id, @PathVariable String stratId, @PathVariable String playerTypeId, @PathVariable String profileId) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + PlayerType playerType = playerTypeService.getPlayerTypeById(playerTypeId); + Profile profile = profileService.getProfileById(profileId); + + if (group == null | strat == null | playerType == null | profile == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getPlayerTypes().contains(playerType) | !playerType.getProfiles().contains(profile)) { + return ResponseEntity.badRequest().build(); + } + + profileService.removeProfile(profile); + + playerType.getProfiles().remove(profile); + + playerTypeService.savePlayerType(playerType); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @PutMapping("/{id}/strat/{stratId}/player-type/{playerTypeId}profile/{profileId}") //TODO remove privius from playerType and create new and add to group if not existend in group + public ResponseEntity editProfile(@PathVariable String id, @PathVariable String stratId, @PathVariable String playerTypeId, @PathVariable String profileId, @RequestBody ProfileRequest newProfile) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + PlayerType playerType = playerTypeService.getPlayerTypeById(playerTypeId); + Profile profile = profileService.getProfileById(profileId); + + if (group == null | strat == null | playerType == null | profile == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getPlayerTypes().contains(playerType) | !playerType.getProfiles().contains(profile)) { + return ResponseEntity.badRequest().build(); + } + + profile.setName(newProfile.name()); + profile.setImage(newProfile.image()); + + profileService.saveProfile(profile); + playerTypeService.savePlayerType(playerType); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + @PostMapping("/{id}/strat/{stratId}/strat-state") public ResponseEntity addStratState(@PathVariable String id, @PathVariable String stratId , @RequestBody StratStateRequest stratStateRequest) { StratsUser user = getUser(); @@ -479,6 +678,97 @@ public class GroupRestAPI { return ResponseEntity.ok(group); } + @PostMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}/lineup") + public ResponseEntity addLineup(@PathVariable String id, @PathVariable String stratId, @PathVariable String stratStateId, @RequestBody LineupRequest lineupRequest) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + StratState stratState = stratStateService.getStratStateById(stratStateId); + + if (group == null | strat == null | stratState == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) && !strat.getStratStates().contains(stratState)) { + return ResponseEntity.badRequest().build(); + } + + Lineup lineup = new Lineup(lineupRequest.description(), lineupRequest.image()); + + lineupService.saveLineup(lineup); + + stratState.getLineups().add(lineup); + + stratStateService.saveStratState(stratState); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @DeleteMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}/lineup/{lineupId}") + public ResponseEntity removeLineup(@PathVariable String id, @PathVariable String stratId, @PathVariable String stratStateId, @PathVariable String linupId) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + StratState stratState = stratStateService.getStratStateById(stratStateId); + Lineup lineup = lineupService.getLineupById(linupId); + + if (group == null | strat == null | stratState == null | lineup == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getStratStates().contains(stratState) | !stratState.getLineups().contains(lineup)) { + return ResponseEntity.badRequest().build(); + } + + lineupService.removeLineup(lineup); + + stratState.getLineups().remove(lineup); + + stratService.saveStrat(strat); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + + @PutMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}/lineup/{lineupId}") + public ResponseEntity editLineup(@PathVariable String id, @PathVariable String stratId, @PathVariable String stratStateId, @PathVariable String lineupId, @RequestBody LineupRequest newLineup) { + StratsUser user = getUser(); + + Group group = groupService.getGroupById(id); + Strat strat = stratService.getStratById(stratId); + StratState stratState = stratStateService.getStratStateById(stratStateId); + Lineup lineup = lineupService.getLineupById(lineupId); + + if (group == null | strat == null | stratState == null | lineup == null) { + return ResponseEntity.notFound().build(); + } + if (!group.getMembers().contains(user)) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + if(!group.getStrats().contains(strat) | !strat.getStratStates().contains(stratState) | !stratState.getLineups().contains(lineup)) { + return ResponseEntity.badRequest().build(); + } + + lineup.setDescription(newLineup.description()); + lineup.setLineupImage(newLineup.image()); + + lineupService.saveLineup(lineup); + stratStateService.saveStratState(stratState); + stratService.saveStrat(strat); + groupService.saveGroup(group); + + return ResponseEntity.ok(group); + } + private StratsUser getUser() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); diff --git a/src/main/java/me/akito123321/valoStrats/rest/requests/LineupRequest.java b/src/main/java/me/akito123321/valoStrats/rest/requests/LineupRequest.java new file mode 100644 index 0000000..e9a0f97 --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/rest/requests/LineupRequest.java @@ -0,0 +1,8 @@ +package me.akito123321.valoStrats.rest.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +public record LineupRequest( + @Size(max = 50, message = "{name longer than 50 chars}") @NotBlank String description, + byte[] image) {} diff --git a/src/main/java/me/akito123321/valoStrats/rest/requests/PlayerTypeRequest.java b/src/main/java/me/akito123321/valoStrats/rest/requests/PlayerTypeRequest.java new file mode 100644 index 0000000..88edd5c --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/rest/requests/PlayerTypeRequest.java @@ -0,0 +1,8 @@ +package me.akito123321.valoStrats.rest.requests; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +public record PlayerTypeRequest(@Size(max = 50, message = "{title longer than 50 chars}") @NotBlank String name, + @Size(max = 50, message = "{title longer than 50 chars}") @NotBlank String task) { +} diff --git a/src/main/java/me/akito123321/valoStrats/rest/services/LineupService.java b/src/main/java/me/akito123321/valoStrats/rest/services/LineupService.java new file mode 100644 index 0000000..57aba9a --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/rest/services/LineupService.java @@ -0,0 +1,27 @@ +package me.akito123321.valoStrats.rest.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import me.akito123321.valoStrats.db.repositories.LineupRepository; +import me.akito123321.valoStrats.schemas.Lineup; + +@Service +public class LineupService { + + @Autowired + private LineupRepository lineupRepository; + + public Lineup getLineupById(String id) { + return lineupRepository.findById(id).orElse(null); + } + + public void saveLineup(Lineup lineup) { + lineupRepository.save(lineup); + } + + public void removeLineup(Lineup lineup) { + lineupRepository.delete(lineup); + } + +} diff --git a/src/main/java/me/akito123321/valoStrats/rest/services/PlayerTypeService.java b/src/main/java/me/akito123321/valoStrats/rest/services/PlayerTypeService.java new file mode 100644 index 0000000..1dd7c85 --- /dev/null +++ b/src/main/java/me/akito123321/valoStrats/rest/services/PlayerTypeService.java @@ -0,0 +1,27 @@ +package me.akito123321.valoStrats.rest.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import me.akito123321.valoStrats.db.repositories.PlayerTypeRepository; +import me.akito123321.valoStrats.schemas.PlayerType; + +@Service +public class PlayerTypeService { + + @Autowired + private PlayerTypeRepository playerTypeRepository; + + public PlayerType getPlayerTypeById(String id) { + return playerTypeRepository.findById(id).orElse(null); + } + + public void savePlayerType(PlayerType playerType) { + playerTypeRepository.save(playerType); + } + + public void removePlayerType(PlayerType playerType) { + playerTypeRepository.delete(playerType); + } + +} diff --git a/src/main/java/me/akito123321/valoStrats/schemas/PlayerType.java b/src/main/java/me/akito123321/valoStrats/schemas/PlayerType.java index 8c2a398..53c0025 100644 --- a/src/main/java/me/akito123321/valoStrats/schemas/PlayerType.java +++ b/src/main/java/me/akito123321/valoStrats/schemas/PlayerType.java @@ -1,5 +1,6 @@ package me.akito123321.valoStrats.schemas; +import java.util.ArrayList; import java.util.List; import jakarta.persistence.Entity; @@ -22,10 +23,10 @@ public class PlayerType { @ManyToMany(fetch = FetchType.EAGER) private List profiles; - public PlayerType(String name, List profiles, String task) { + public PlayerType(String name, String task) { super(); this.name = name; - this.profiles = profiles; + this.profiles = new ArrayList(); this.task = task; } diff --git a/src/main/java/me/akito123321/valoStrats/schemas/Strat.java b/src/main/java/me/akito123321/valoStrats/schemas/Strat.java index 3110139..9571ca6 100644 --- a/src/main/java/me/akito123321/valoStrats/schemas/Strat.java +++ b/src/main/java/me/akito123321/valoStrats/schemas/Strat.java @@ -40,7 +40,6 @@ public class Strat { this.group = group; this.stratType = stratType; this.stratStates = new ArrayList(); - this.stratStates = new ArrayList(); this.playerTypes = new ArrayList(); } @@ -99,5 +98,13 @@ public class Strat { public void setSuccess(int success) { this.success = success; } + + public List getPlayerTypes() { + return playerTypes; + } + + public void setPlayerTypes(List playerTypes) { + this.playerTypes = playerTypes; + } }