Added routerResponse
This commit is contained in:
@@ -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<IpAllocation> findOptionalByRouterId(UUID routerId) {
|
||||
return repository.findByRouterId(routerId);
|
||||
}
|
||||
}
|
||||
@@ -33,8 +33,16 @@ public class RouterController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Router> getAll() {
|
||||
return service.findAll();
|
||||
public List<RouterResponse> 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}")
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user