ca.ibodrov.mica.api.model.RenderRequest Maven / Gradle / Ivy
The newest version!
package ca.ibodrov.mica.api.model;
import ca.ibodrov.mica.api.validation.ValidName;
import com.fasterxml.jackson.databind.JsonNode;
import javax.validation.ConstraintViolationException;
import java.util.Optional;
import java.util.Set;
import static java.util.Objects.requireNonNull;
public record RenderRequest(Optional viewId,
Optional viewName,
Optional parameters) {
public RenderRequest {
requireNonNull(viewId);
requireNonNull(viewName);
if (viewId.isEmpty() && viewName.isEmpty()) {
throw new ConstraintViolationException("One of viewId or viewName must be set", Set.of());
}
viewName.ifPresent(name -> {
if (!name.matches(ValidName.NAME_PATTERN)) {
throw new ConstraintViolationException("Invalid view name: " + name + ". " + ValidName.MESSAGE,
Set.of());
}
});
}
public static RenderRequest of(String viewName) {
return new RenderRequest(Optional.empty(), Optional.of(viewName), Optional.empty());
}
public static RenderRequest parameterized(String viewName, JsonNode parameters) {
return new RenderRequest(Optional.empty(), Optional.of(viewName), Optional.ofNullable(parameters));
}
}