Added routerResponse
This commit is contained in:
@@ -5,6 +5,7 @@ import com.litoralregas.openvpn.router.RouterService;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -91,4 +92,8 @@ public class IpAllocationService {
|
|||||||
return repository.findByRouterId(routerId)
|
return repository.findByRouterId(routerId)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("No IP allocation found for router: " + 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
|
@GetMapping
|
||||||
public List<Router> getAll() {
|
public List<RouterResponse> getAll() {
|
||||||
return service.findAll();
|
return service.findAll()
|
||||||
|
.stream()
|
||||||
|
.map(router -> RouterResponse.from(
|
||||||
|
router,
|
||||||
|
ipAllocationService
|
||||||
|
.findOptionalByRouterId(router.getId())
|
||||||
|
.orElse(null)
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@@ -43,8 +51,13 @@ public class RouterController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Router getById(@PathVariable UUID id) {
|
public RouterResponse getById(@PathVariable UUID id) {
|
||||||
return service.findById(id);
|
Router router = service.findById(id);
|
||||||
|
|
||||||
|
return RouterResponse.from(
|
||||||
|
router,
|
||||||
|
ipAllocationService.findOptionalByRouterId(id).orElse(null)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@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