edded endpoints for lineup/playertype/profile

This commit is contained in:
Akito123321 2024-06-17 12:30:29 +02:00
parent 09f4b484ca
commit 379a398bc1
9 changed files with 389 additions and 3 deletions

View File

@ -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<Lineup, String>{
}

View File

@ -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<PlayerType, String>{
}

View File

@ -17,17 +17,23 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import me.akito123321.valoStrats.rest.requests.GroupRequest; 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.MapRequest;
import me.akito123321.valoStrats.rest.requests.PlayerTypeRequest;
import me.akito123321.valoStrats.rest.requests.StratRequest; import me.akito123321.valoStrats.rest.requests.StratRequest;
import me.akito123321.valoStrats.rest.requests.StratStateRequest; import me.akito123321.valoStrats.rest.requests.StratStateRequest;
import me.akito123321.valoStrats.rest.services.GroupService; 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.MapService;
import me.akito123321.valoStrats.rest.services.PlayerTypeService;
import me.akito123321.valoStrats.rest.services.ProfileService; import me.akito123321.valoStrats.rest.services.ProfileService;
import me.akito123321.valoStrats.rest.services.StratService; import me.akito123321.valoStrats.rest.services.StratService;
import me.akito123321.valoStrats.rest.services.StratStateService; import me.akito123321.valoStrats.rest.services.StratStateService;
import me.akito123321.valoStrats.rest.util.JDBCUserDetailsService; import me.akito123321.valoStrats.rest.util.JDBCUserDetailsService;
import me.akito123321.valoStrats.schemas.Group; import me.akito123321.valoStrats.schemas.Group;
import me.akito123321.valoStrats.schemas.Lineup;
import me.akito123321.valoStrats.schemas.Map; import me.akito123321.valoStrats.schemas.Map;
import me.akito123321.valoStrats.schemas.PlayerType;
import me.akito123321.valoStrats.schemas.Profile; import me.akito123321.valoStrats.schemas.Profile;
import me.akito123321.valoStrats.schemas.Strat; import me.akito123321.valoStrats.schemas.Strat;
import me.akito123321.valoStrats.schemas.StratState; import me.akito123321.valoStrats.schemas.StratState;
@ -37,6 +43,12 @@ import me.akito123321.valoStrats.schemas.StratsUser;
@RequestMapping("/api/group") @RequestMapping("/api/group")
public class GroupRestAPI { public class GroupRestAPI {
@Autowired
private PlayerTypeService playerTypeService;
@Autowired
private LineupService lineupService;
@Autowired @Autowired
private ProfileService profileService; private ProfileService profileService;
@ -179,6 +191,9 @@ public class GroupRestAPI {
if(stratRequest.success() != null) { if(stratRequest.success() != null) {
strat.setSuccess(stratRequest.success()); strat.setSuccess(stratRequest.success());
} }
if(!group.getStratTypes().contains(stratRequest.stratType())) {
group.getStratTypes().add(stratRequest.stratType());
}
stratService.saveStrat(strat); stratService.saveStrat(strat);
@ -240,6 +255,9 @@ public class GroupRestAPI {
if(newStrat.success() != null) { if(newStrat.success() != null) {
strat.setSuccess(newStrat.success()); strat.setSuccess(newStrat.success());
} }
if(!group.getStratTypes().contains(newStrat.stratType())) {
group.getStratTypes().add(newStrat.stratType());
}
stratService.saveStrat(strat); stratService.saveStrat(strat);
@ -394,6 +412,187 @@ public class GroupRestAPI {
return ResponseEntity.ok(group); return ResponseEntity.ok(group);
} }
@PostMapping("/{id}/strat/{stratId}/player-type")
public ResponseEntity<Group> 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<Group> 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<Group> 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<Group> 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<Group> 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<Group> 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") @PostMapping("/{id}/strat/{stratId}/strat-state")
public ResponseEntity<Group> addStratState(@PathVariable String id, @PathVariable String stratId , @RequestBody StratStateRequest stratStateRequest) { public ResponseEntity<Group> addStratState(@PathVariable String id, @PathVariable String stratId , @RequestBody StratStateRequest stratStateRequest) {
StratsUser user = getUser(); StratsUser user = getUser();
@ -479,6 +678,97 @@ public class GroupRestAPI {
return ResponseEntity.ok(group); return ResponseEntity.ok(group);
} }
@PostMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}/lineup")
public ResponseEntity<Group> 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<Group> 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<Group> 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() { private StratsUser getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Authentication auth = SecurityContextHolder.getContext().getAuthentication();

View File

@ -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) {}

View File

@ -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) {
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
package me.akito123321.valoStrats.schemas; package me.akito123321.valoStrats.schemas;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
@ -22,10 +23,10 @@ public class PlayerType {
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private List<Profile> profiles; private List<Profile> profiles;
public PlayerType(String name, List<Profile> profiles, String task) { public PlayerType(String name, String task) {
super(); super();
this.name = name; this.name = name;
this.profiles = profiles; this.profiles = new ArrayList<Profile>();
this.task = task; this.task = task;
} }

View File

@ -40,7 +40,6 @@ public class Strat {
this.group = group; this.group = group;
this.stratType = stratType; this.stratType = stratType;
this.stratStates = new ArrayList<StratState>(); this.stratStates = new ArrayList<StratState>();
this.stratStates = new ArrayList<StratState>();
this.playerTypes = new ArrayList<PlayerType>(); this.playerTypes = new ArrayList<PlayerType>();
} }
@ -100,4 +99,12 @@ public class Strat {
this.success = success; this.success = success;
} }
public List<PlayerType> getPlayerTypes() {
return playerTypes;
}
public void setPlayerTypes(List<PlayerType> playerTypes) {
this.playerTypes = playerTypes;
}
} }