added maps and profiles post/delete/put endpoints

This commit is contained in:
Akito123321 2024-06-15 19:53:26 +02:00
parent 31e61c6762
commit 09f4b484ca
8 changed files with 260 additions and 11 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.Map;
public interface MapRepository extends JpaRepository<Map, 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.Profile;
public interface ProfileRepository extends JpaRepository<Profile, String>{
}

View File

@ -17,13 +17,18 @@ 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.MapRequest;
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.MapService;
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.Map;
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;
import me.akito123321.valoStrats.schemas.StratsUser; import me.akito123321.valoStrats.schemas.StratsUser;
@ -32,6 +37,12 @@ import me.akito123321.valoStrats.schemas.StratsUser;
@RequestMapping("/api/group") @RequestMapping("/api/group")
public class GroupRestAPI { public class GroupRestAPI {
@Autowired
private ProfileService profileService;
@Autowired
private MapService mapService;
@Autowired @Autowired
private StratStateService stratStateService; private StratStateService stratStateService;
@ -183,16 +194,15 @@ public class GroupRestAPI {
StratsUser user = getUser(); StratsUser user = getUser();
Group group = groupService.getGroupById(id); Group group = groupService.getGroupById(id);
if (group == null) { Strat strat = stratService.getStratById(stratId);
if (group == null | strat == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
if (!group.getMembers().contains(user)) { if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
} }
if (!group.getStrats().contains(strat)) {
Strat strat = stratService.getStratById(stratId); return ResponseEntity.badRequest().build();
if (strat == null) {
return ResponseEntity.notFound().build();
} }
stratService.removeStrat(strat); stratService.removeStrat(strat);
@ -209,17 +219,14 @@ public class GroupRestAPI {
StratsUser user = getUser(); StratsUser user = getUser();
Group group = groupService.getGroupById(id); Group group = groupService.getGroupById(id);
if (group == null) { Strat strat = stratService.getStratById(stratId);
if (group == null | strat == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
if (!group.getMembers().contains(user)) { if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
} }
if (!group.getStrats().contains(strat)) {
Strat strat = stratService.getStratById(stratId);
if (strat == null){
return ResponseEntity.notFound().build();
}if (!group.getStrats().contains(strat)) {
return ResponseEntity.badRequest().build(); return ResponseEntity.badRequest().build();
} }
@ -241,6 +248,152 @@ public class GroupRestAPI {
return ResponseEntity.ok(group); return ResponseEntity.ok(group);
} }
@PostMapping("/{id}/map")
public ResponseEntity<Group> addMap(@PathVariable String id, @RequestBody MapRequest mapRequest) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
if (group == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
Map map = new Map(mapRequest.name(), mapRequest.image());
mapService.saveMap(map);
group.getMaps().add(map);
groupService.saveGroup(group);
return ResponseEntity.ok(group);
}
@DeleteMapping("/{id}/map/{mapId}")
public ResponseEntity<Group> removeMap(@PathVariable String id, @PathVariable String mapId) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
Map map = mapService.getMapById(mapId);
if (group == null | map == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (!group.getMaps().contains(map)) {
return ResponseEntity.badRequest().build();
}
mapService.removeMap(map);
group.getMaps().remove(map);
groupService.saveGroup(group);
return ResponseEntity.ok(group);
}
@PutMapping("/{id}/map/{mapId}")
public ResponseEntity<Group> editMap(@PathVariable String id, @PathVariable String mapId, @RequestBody MapRequest newMap) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
Map map = mapService.getMapById(mapId);
if (group == null | map == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (!group.getMaps().contains(map)) {
return ResponseEntity.badRequest().build();
}
map.setName(newMap.name());
map.setImage(newMap.image());
mapService.saveMap(map);
groupService.saveGroup(group);
return ResponseEntity.ok(group);
}
@PostMapping("/{id}/profile")
public ResponseEntity<Group> addProfile(@PathVariable String id, @RequestBody ProfileRequest profileRequest) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
if (group == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
Profile profile = new Profile(profileRequest.name(), profileRequest.image());
profileService.saveProfile(profile);
group.getProfiles().add(profile);
groupService.saveGroup(group);
return ResponseEntity.ok(group);
}
@DeleteMapping("/{id}/profile/{profileId}")
public ResponseEntity<Group> removeProfile(@PathVariable String id, @PathVariable String profileId) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
Profile profile = profileService.getProfileById(profileId);
if (group == null | profile == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (!group.getProfiles().contains(profile)) {
return ResponseEntity.badRequest().build();
}
profileService.removeProfile(profile);
group.getProfiles().remove(profile);
groupService.saveGroup(group);
return ResponseEntity.ok(group);
}
@PutMapping("/{id}/profile/{profileId}")
public ResponseEntity<Group> editProfile(@PathVariable String id, @PathVariable String profileId, @RequestBody MapRequest newMap) {
StratsUser user = getUser();
Group group = groupService.getGroupById(id);
Profile profile = profileService.getProfileById(profileId);
if (group == null | profile == null) {
return ResponseEntity.notFound().build();
}
if (!group.getMembers().contains(user)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (!group.getProfiles().contains(profile)) {
return ResponseEntity.badRequest().build();
}
profile.setName(newMap.name());
profile.setImage(newMap.image());
profileService.saveProfile(profile);
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();

View File

@ -0,0 +1,8 @@
package me.akito123321.valoStrats.rest.controller;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public record ProfileRequest(
@Size(max = 50, message = "{name longer than 50 chars}") @NotBlank String name,
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 MapRequest(
@Size(max = 50, message = "{name longer than 50 chars}") @NotBlank String name,
byte[] image) {}

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.MapRepository;
import me.akito123321.valoStrats.schemas.Map;
@Service
public class MapService {
@Autowired
private MapRepository mapRepository;
public Map getMapById(String id) {
return mapRepository.findById(id).orElse(null);
}
public void saveMap(Map map) {
mapRepository.save(map);
}
public void removeMap(Map map) {
mapRepository.delete(map);
}
}

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.ProfileRepository;
import me.akito123321.valoStrats.schemas.Profile;
@Service
public class ProfileService {
@Autowired
private ProfileRepository profileRepository;
public Profile getProfileById(String id) {
return profileRepository.findById(id).orElse(null);
}
public void saveProfile(Profile profile) {
profileRepository.save(profile);
}
public void removeProfile(Profile profile) {
profileRepository.delete(profile);
}
}

View File

@ -77,4 +77,12 @@ public class Group {
return stratTypes; return stratTypes;
} }
public List<Map> getMaps() {
return maps;
}
public List<Profile> getProfiles() {
return profiles;
}
} }