Add router duplicate subnet validation

This commit is contained in:
litoral05
2026-05-05 10:20:44 +01:00
parent d0fd8d9774
commit b0faad0112
3 changed files with 30 additions and 0 deletions
@@ -0,0 +1,23 @@
package com.litoralregas.openvpn.common;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestControllerAdvice
public class ApiExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
public Map<String, String> handleIllegalArgument(IllegalArgumentException exception) {
return Map.of("error", exception.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidation(MethodArgumentNotValidException exception) {
return Map.of("error", "Validation failed");
}
}
@@ -1,7 +1,10 @@
package com.litoralregas.openvpn.router;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;
public interface RouterRepository extends JpaRepository<Router, UUID> {
boolean existsByLanSubnet(String lanSubnet);
}
@@ -20,6 +20,10 @@ public class RouterService {
}
public Router create(CreateRouterRequest request) {
if (repository.existsByLanSubnet(request.getLanSubnet())) {
throw new IllegalArgumentException("LAN subnet already exists: " + request.getLanSubnet());
}
LocalDateTime now = LocalDateTime.now();
Router router = new Router();