From 2eb685d14971579ff2ca98a2041d663b111ddd82 Mon Sep 17 00:00:00 2001 From: seujorgenochurras Date: Sun, 17 Nov 2024 19:37:00 -0300 Subject: [PATCH] feat: Create delete server --- .../controller/server/ServerController.java | 10 ++++++-- .../p2pApi/api/dto/server/ServerDto.java | 14 +++++++++++ .../p2pApi/domain/model/client/Client.java | 2 +- .../p2pApi/domain/model/server/Server.java | 12 ++++++++++ .../domain/service/server/AccessService.java | 2 +- .../domain/service/server/ServerService.java | 23 ++++++++++++------- p2pcraft-connection-mod/build.gradle | 2 +- .../p2pcraftmod/client/config/ConfigFile.java | 8 +++---- 8 files changed, 56 insertions(+), 17 deletions(-) diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/controller/server/ServerController.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/controller/server/ServerController.java index c41d6db..e8670d6 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/controller/server/ServerController.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/controller/server/ServerController.java @@ -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) { @@ -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(); + } } diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/dto/server/ServerDto.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/dto/server/ServerDto.java index ea32351..10d44ba 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/dto/server/ServerDto.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/api/dto/server/ServerDto.java @@ -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; diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/client/Client.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/client/Client.java index 24459ce..f198d56 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/client/Client.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/client/Client.java @@ -32,7 +32,7 @@ public class Client { private List serverAccesses; public List getServerAccesses() { - return serverAccesses; + return serverAccesses.stream().filter(serverClientAccess -> serverClientAccess.getServer().isActive()).toList(); } public Client setServerAccesses(List serverAccesses) { diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/server/Server.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/server/Server.java index 66945c0..ff4fea2 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/server/Server.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/model/server/Server.java @@ -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(); @@ -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; diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/AccessService.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/AccessService.java index e838284..4cda049 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/AccessService.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/AccessService.java @@ -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); } } diff --git a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/ServerService.java b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/ServerService.java index 5694ceb..f5b5058 100644 --- a/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/ServerService.java +++ b/p2pCraft-api/src/main/java/io/github/seujorgenochurras/p2pApi/domain/service/server/ServerService.java @@ -10,7 +10,6 @@ import org.springframework.stereotype.Service; import java.util.List; -import java.util.Optional; @Service public class ServerService { @@ -30,14 +29,19 @@ protected Server save(Server server) { @Nullable public Server findByStaticIp(String staticIp) { - Optional 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 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 @@ -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()); } @@ -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 findAll() { - return serverRepository.findAll(); + return serverRepository.findAll().stream().filter(Server::isActive).toList(); } } diff --git a/p2pcraft-connection-mod/build.gradle b/p2pcraft-connection-mod/build.gradle index f148132..e84d548 100644 --- a/p2pcraft-connection-mod/build.gradle +++ b/p2pcraft-connection-mod/build.gradle @@ -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}" diff --git a/p2pcraft-connection-mod/src/main/java/io/github/seujorgenochurras/p2pcraftmod/client/config/ConfigFile.java b/p2pcraft-connection-mod/src/main/java/io/github/seujorgenochurras/p2pcraftmod/client/config/ConfigFile.java index b8b63bd..db2ed60 100644 --- a/p2pcraft-connection-mod/src/main/java/io/github/seujorgenochurras/p2pcraftmod/client/config/ConfigFile.java +++ b/p2pcraft-connection-mod/src/main/java/io/github/seujorgenochurras/p2pcraftmod/client/config/ConfigFile.java @@ -25,13 +25,13 @@ public static String get(String configKey) { } private static Map 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");