diff --git a/src/main/java/com/litoralregas/openvpn/openvpn/IpAllocationService.java b/src/main/java/com/litoralregas/openvpn/openvpn/IpAllocationService.java index 392cec1..67493d4 100644 --- a/src/main/java/com/litoralregas/openvpn/openvpn/IpAllocationService.java +++ b/src/main/java/com/litoralregas/openvpn/openvpn/IpAllocationService.java @@ -5,6 +5,7 @@ import com.litoralregas.openvpn.router.RouterService; import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.Optional; import java.util.UUID; @Service @@ -91,4 +92,8 @@ public class IpAllocationService { return repository.findByRouterId(routerId) .orElseThrow(() -> new IllegalArgumentException("No IP allocation found for router: " + routerId)); } + + public Optional findOptionalByRouterId(UUID routerId) { + return repository.findByRouterId(routerId); + } } \ No newline at end of file diff --git a/src/main/java/com/litoralregas/openvpn/router/RouterController.java b/src/main/java/com/litoralregas/openvpn/router/RouterController.java index 44f94b5..e1060d6 100644 --- a/src/main/java/com/litoralregas/openvpn/router/RouterController.java +++ b/src/main/java/com/litoralregas/openvpn/router/RouterController.java @@ -33,8 +33,16 @@ public class RouterController { } @GetMapping - public List getAll() { - return service.findAll(); + public List getAll() { + return service.findAll() + .stream() + .map(router -> RouterResponse.from( + router, + ipAllocationService + .findOptionalByRouterId(router.getId()) + .orElse(null) + )) + .toList(); } @PostMapping @@ -43,8 +51,13 @@ public class RouterController { } @GetMapping("/{id}") - public Router getById(@PathVariable UUID id) { - return service.findById(id); + public RouterResponse getById(@PathVariable UUID id) { + Router router = service.findById(id); + + return RouterResponse.from( + router, + ipAllocationService.findOptionalByRouterId(id).orElse(null) + ); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/litoralregas/openvpn/router/RouterResponse.java b/src/main/java/com/litoralregas/openvpn/router/RouterResponse.java new file mode 100644 index 0000000..c2f4e92 --- /dev/null +++ b/src/main/java/com/litoralregas/openvpn/router/RouterResponse.java @@ -0,0 +1,29 @@ +package com.litoralregas.openvpn.router; + +import com.litoralregas.openvpn.openvpn.IpAllocation; + +import java.util.UUID; + +public record RouterResponse( + UUID id, + String name, + String serialNumber, + String lanIp, + String lanSubnet, + RouterStatus status, + String clientName, + String vpnIp +) { + public static RouterResponse from(Router router, IpAllocation allocation) { + return new RouterResponse( + router.getId(), + router.getName(), + router.getSerialNumber(), + router.getLanIp(), + router.getLanSubnet(), + router.getStatus(), + allocation != null ? allocation.getClientName() : null, + allocation != null ? allocation.getVpnIp() : null + ); + } +} \ No newline at end of file