added strat and startState post/delete/put endpoints
This commit is contained in:
parent
ce800d2b46
commit
e82b3416d2
@ -0,0 +1,9 @@
|
||||
package me.akito123321.valoStrats.db.repositories;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import me.akito123321.valoStrats.schemas.StratState;
|
||||
|
||||
public interface StratStateRepository extends JpaRepository<StratState, String>{
|
||||
|
||||
}
|
@ -19,17 +19,23 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import me.akito123321.valoStrats.rest.requests.GroupRequest;
|
||||
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.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.Strat;
|
||||
import me.akito123321.valoStrats.schemas.StratState;
|
||||
import me.akito123321.valoStrats.schemas.StratsUser;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/group")
|
||||
public class GroupRestAPI {
|
||||
|
||||
@Autowired
|
||||
private StratStateService stratStateService;
|
||||
|
||||
@Autowired
|
||||
private JDBCUserDetailsService userService;
|
||||
|
||||
@ -39,7 +45,7 @@ public class GroupRestAPI {
|
||||
@Autowired
|
||||
private StratService stratService;
|
||||
|
||||
@PostMapping("/")
|
||||
@PostMapping()
|
||||
public ResponseEntity<Group> createGroup(@RequestBody GroupRequest request) {
|
||||
StratsUser user = getUser();
|
||||
Group group = new Group(request.name(), user);
|
||||
@ -47,7 +53,7 @@ public class GroupRestAPI {
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<Group>> getGroups() {
|
||||
StratsUser user = getUser();
|
||||
List<Group> groups = user.getGroups();
|
||||
@ -113,7 +119,7 @@ public class GroupRestAPI {
|
||||
if (group == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
if (!group.getOwner().getUsername().equals(user.getUsername())) {
|
||||
if (!group.getOwner().equals(user)) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
|
||||
}
|
||||
|
||||
@ -144,7 +150,7 @@ public class GroupRestAPI {
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/strat")
|
||||
public ResponseEntity<Group> addStrat(@PathVariable String id, @RequestBody StratRequest strat) {
|
||||
public ResponseEntity<Group> addStrat(@PathVariable String id, @RequestBody StratRequest stratRequest) {
|
||||
StratsUser user = getUser();
|
||||
|
||||
Group group = groupService.getGroupById(id);
|
||||
@ -155,7 +161,11 @@ public class GroupRestAPI {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
|
||||
}
|
||||
|
||||
group.getStrats().add(new Strat(strat.title(), strat.description(), group, strat.stratStates()));
|
||||
Strat strat = new Strat(stratRequest.title(), stratRequest.description(), group);
|
||||
|
||||
stratService.saveStrat(strat);
|
||||
|
||||
group.getStrats().add(strat);
|
||||
|
||||
groupService.saveGroup(group);
|
||||
|
||||
@ -179,6 +189,8 @@ public class GroupRestAPI {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
stratService.removeStrat(strat);
|
||||
|
||||
group.getStrats().remove(strat);
|
||||
|
||||
groupService.saveGroup(group);
|
||||
@ -186,7 +198,7 @@ public class GroupRestAPI {
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/strat/{stratId}")
|
||||
@PutMapping("/{id}/strat/{stratId}")
|
||||
public ResponseEntity<Group> editStrat(@PathVariable String id, @PathVariable String stratId, @RequestBody StratRequest newStrat) {
|
||||
StratsUser user = getUser();
|
||||
|
||||
@ -216,6 +228,91 @@ public class GroupRestAPI {
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/strat/{stratId}/strat-state")
|
||||
public ResponseEntity<Group> addStratState(@PathVariable String id, @PathVariable String stratId , @RequestBody StratStateRequest stratStateRequest) {
|
||||
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();
|
||||
}
|
||||
|
||||
StratState stratState = new StratState(stratStateRequest.description(), strat);
|
||||
|
||||
stratStateService.saveStratState(stratState);
|
||||
|
||||
strat.getStratStates().add(stratState);
|
||||
|
||||
stratService.saveStrat(strat);
|
||||
groupService.saveGroup(group);
|
||||
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}")
|
||||
public ResponseEntity<Group> removeStratState(@PathVariable String id, @PathVariable String stratId, @PathVariable String stratStateId) {
|
||||
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();
|
||||
}
|
||||
|
||||
stratStateService.removeStratState(stratState);
|
||||
|
||||
strat.getStratStates().remove(stratState);
|
||||
|
||||
stratService.saveStrat(strat);
|
||||
groupService.saveGroup(group);
|
||||
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/strat/{stratId}/strat-state/{stratStateId}")
|
||||
public ResponseEntity<Group> editStratState(@PathVariable String id, @PathVariable String stratId, @PathVariable String stratStateId, @RequestBody StratStateRequest newStratState) {
|
||||
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();
|
||||
}
|
||||
|
||||
stratState.setDescription(newStratState.description());
|
||||
stratState.setImage(newStratState.image());
|
||||
|
||||
stratStateService.saveStratState(stratState);
|
||||
stratService.saveStrat(strat);
|
||||
groupService.saveGroup(group);
|
||||
|
||||
return ResponseEntity.ok(group);
|
||||
}
|
||||
|
||||
|
||||
private StratsUser getUser() {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
@ -0,0 +1,9 @@
|
||||
package me.akito123321.valoStrats.rest.requests;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public record StratStateRequest (
|
||||
@Size(max = 512, message = "{title longer than 50 chars}") @NotBlank String description,
|
||||
byte[] image) {
|
||||
}
|
@ -20,4 +20,8 @@ public class StratService {
|
||||
stratRepository.save(strat);
|
||||
}
|
||||
|
||||
public void removeStrat(Strat strat) {
|
||||
stratRepository.delete(strat);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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.StratStateRepository;
|
||||
import me.akito123321.valoStrats.schemas.StratState;
|
||||
|
||||
@Service
|
||||
public class StratStateService {
|
||||
|
||||
@Autowired
|
||||
private StratStateRepository stratStateRepository;
|
||||
|
||||
public StratState getStratStateById(String id) {
|
||||
return stratStateRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public void saveStratState(StratState stratState) {
|
||||
stratStateRepository.save(stratState);
|
||||
}
|
||||
|
||||
public void removeStratState(StratState stratState) {
|
||||
stratStateRepository.delete(stratState);
|
||||
}
|
||||
|
||||
}
|
@ -36,6 +36,8 @@ public class Group {
|
||||
this.owner = owner;
|
||||
this.members = new ArrayList<StratsUser>();
|
||||
this.strats = new ArrayList<Strat>();
|
||||
this.maps = new ArrayList<Map>();
|
||||
this.profiles = new ArrayList<Profile>();
|
||||
}
|
||||
|
||||
public Group() {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.akito123321.valoStrats.schemas;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.ElementCollection;
|
||||
@ -30,11 +31,13 @@ public class Strat {
|
||||
@ManyToOne
|
||||
private Map map;
|
||||
|
||||
public Strat(String title, String description, Group group, List<StratState> stratStates) {
|
||||
public Strat(String title, String description, Group group) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.group = group;
|
||||
this.stratStates = stratStates;
|
||||
this.stratStates = new ArrayList<StratState>();
|
||||
this.stratStates = new ArrayList<StratState>();
|
||||
this.playerTypes = new ArrayList<PlayerType>();
|
||||
}
|
||||
|
||||
public Strat() {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.akito123321.valoStrats.schemas;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
@ -23,16 +24,20 @@ public class StratState {
|
||||
@ManyToOne
|
||||
private Strat strat;
|
||||
|
||||
@Lob
|
||||
@Column(length = Integer.MAX_VALUE)
|
||||
private byte[] image;
|
||||
|
||||
@Lob
|
||||
@Column(length = Integer.MAX_VALUE)
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
private List<Lineup> lineups;
|
||||
|
||||
public StratState(String description, Strat strat, List<Lineup> lineups) {
|
||||
public StratState(String description, Strat strat) {
|
||||
super();
|
||||
this.description = description;
|
||||
this.strat = strat;
|
||||
this.lineups = lineups;
|
||||
this.lineups = new ArrayList<Lineup>();
|
||||
}
|
||||
|
||||
public StratState() {
|
||||
@ -59,4 +64,16 @@ public class StratState {
|
||||
return lineups;
|
||||
}
|
||||
|
||||
public void setStrat(Strat strat) {
|
||||
this.strat = strat;
|
||||
}
|
||||
|
||||
public byte[] getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(byte[] image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user