Add router duplicate subnet validation
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user