Skip to content

Commit

Permalink
feat: Create delete server
Browse files Browse the repository at this point in the history
  • Loading branch information
seujorgenochurras committed Nov 17, 2024
1 parent 2fbe497 commit 2eb685d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public ResponseEntity<?> findByName(@PathVariable String name, Principal princip
Client client = clientService.findById(clientUuid);

ServerClientAccess access = client.getServerAccesses().stream()
.filter((serverClientAccess -> serverClientAccess.getServer().getName().equals(name)))
.filter((serverClientAccess -> serverClientAccess.getServer().isActive() &&
serverClientAccess.getServer().getName().equals(name)))
.findFirst().orElse(null);

if (access == null) {
Expand Down Expand Up @@ -84,5 +85,10 @@ public ResponseEntity<?> findAllServers(Principal principal) {
return ResponseEntity.ok(client.getServerAccesses());
}


@DeleteMapping(value = "/{name}")
public ResponseEntity<?> deleteServer(@PathVariable String name) {
Server server = serverService.findByName(name);
serverService.update(server.getUuid(), new ServerDto().setActive(false));
return noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ public class ServerDto {
private String mapUrl;
private ServerProperties properties;
private Boolean open;
private Boolean active;

public Boolean getOpen() {
return open;
}

public Boolean getActive() {
return active;
}

public ServerDto setActive(Boolean active) {
this.active = active;
return this;
}

public Boolean isOpen() {
return open;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Client {
private List<ServerClientAccess> serverAccesses;

public List<ServerClientAccess> getServerAccesses() {
return serverAccesses;
return serverAccesses.stream().filter(serverClientAccess -> serverClientAccess.getServer().isActive()).toList();
}

public Client setServerAccesses(List<ServerClientAccess> serverAccesses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class Server {
@JsonInclude
private ServerProperties properties;

@Column(name = "active")
private boolean active = true;

@Autowired
@Transient
private final ServerFilesService serverFilesService = new ServerFilesService();
Expand All @@ -47,6 +50,15 @@ public boolean isOpen() {
return open;
}

public boolean isActive() {
return active;
}

public Server setActive(boolean active) {
this.active = active;
return this;
}

public Server setOpen(boolean open) {
this.open = open;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public ServerClientAccess updateAccess(Server server, Client client, UpdateAcces
}

public ServerClientAccess getAccessLevel(Server server, Client client) {
return accessRepository.findByClientUuidAndServerUuid(server.getUuid(), client.getUuid()).orElse(null);
return accessRepository.findByClientUuidAndServerUuid(client.getUuid(), server.getUuid()).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ServerService {
Expand All @@ -30,14 +29,19 @@ protected Server save(Server server) {

@Nullable
public Server findByStaticIp(String staticIp) {
Optional<Server> fetchedServer = serverRepository.findByStaticIp(staticIp);
return fetchedServer.orElse(null);
Server fetchedServer = serverRepository.findByStaticIp(staticIp).orElse(null);

if (fetchedServer == null || !fetchedServer.isActive())
return null;
return fetchedServer;
}

@Nullable
public Server findByName(String name) {
Optional<Server> fetchedServer = serverRepository.findByStaticIp("p2pcraft.connect." + name + ".xyz");
return fetchedServer.orElse(null);
Server fetchedServer = serverRepository.findByStaticIp("p2pcraft.connect." + name + ".xyz").orElse(null);
if (fetchedServer == null || !fetchedServer.isActive())
return null;
return fetchedServer;
}

@Transactional
Expand All @@ -49,7 +53,7 @@ public Server update(String uuid, ServerDto serverDto) {
if (serverDto.getStaticIp() != null) newServer.setStaticIp(serverDto.getStaticIp());
if (serverDto.getVolatileIp() != null) newServer.setVolatileIp(serverDto.getVolatileIp());
if (serverDto.getMapUrl() != null) newServer.getMapConfigurations().setMapUrl(serverDto.getMapUrl());

if (serverDto.getActive() != null) newServer.setActive(serverDto.getActive());
if (serverDto.getProperties() != null) {
serverFilesService.updateProperties(serverDto.getProperties(), newServer.getMapConfigurations().getMapUrl());
}
Expand All @@ -58,11 +62,14 @@ public Server update(String uuid, ServerDto serverDto) {
}

public Server findServerById(String uuid) {
return serverRepository.findById(uuid).orElse(null);
Server fetchedServer = serverRepository.findById(uuid).orElse(null);
if (fetchedServer == null || !fetchedServer.isActive())
return null;
return fetchedServer;
}

public List<Server> findAll() {
return serverRepository.findAll();
return serverRepository.findAll().stream().filter(Server::isActive).toList();
}

}
2 changes: 1 addition & 1 deletion p2pcraft-connection-mod/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
include(implementation("com.github.alexdlaird:java-ngrok:2.3.0"))
include(implementation("org.yaml:snakeyaml:2.2"))

implementation ("io.github.cdimascio:dotenv-java:3.0.1")
include(implementation("io.github.cdimascio:dotenv-java:3.0.1"))

// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public static String get(String configKey) {
}

private static Map<String, Object> loadYaml() {
Dotenv dotenv = Dotenv
.configure()
.directory("../")
.load();
try {
File configFile = new File(P2pCraftConnectModClient.getResourcesDirPath() + "/config.yaml");
if (!configFile.exists()) {
Dotenv dotenv = Dotenv
.configure()
.directory("../")
.load();
FileWriter fileWriter = new FileWriter(configFile);
fileWriter.write("P2PCRAFT_API_URL: \"http://127.0.0.1:8080\"\n");
fileWriter.write("P2PCRAFT_GITHUB_BOT_TOKEN: \"" + dotenv.get("P2PCRAFT_GITHUB_BOT_TOKEN") + "\"\n");
Expand Down

0 comments on commit 2eb685d

Please sign in to comment.