package com.github.slamdev.microframework.http.server;

import io.undertow.server.HttpServerExchange;
import lombok.experimental.UtilityClass;

import java.util.Deque;
import java.util.Optional;
import java.util.function.Supplier;

import static io.undertow.util.StatusCodes.UNPROCESSABLE_ENTITY;

@UtilityClass
public class Params {

    public static Optional<String> query(HttpServerExchange exchange, String name) {
        return Optional
                .ofNullable(exchange.getQueryParameters().get(name))
                .map(Deque::getFirst)
                .map(String::trim)
                .map(s -> s.isEmpty() ? null : s);
    }

    public static <X extends Throwable> String requireQuery(HttpServerExchange exchange, String name, Supplier<? extends X> exceptionSupplier) throws X {
        return query(exchange, name).orElseThrow(exceptionSupplier);
    }

    public static String requireQuery(HttpServerExchange exchange, String name, String message) {
        return requireQuery(exchange, name, () -> new HttpException(UNPROCESSABLE_ENTITY, message));
    }
}

@@ -188,16 +188,47 @@
 tional;%0A
+import java.util.OptionalLong;%0A
 import j
@@ -1102,11 +1102,1626 @@
 );%0A    %7D
+%0A%0A    public static OptionalLong queryLong(HttpServerExchange exchange, String name) %7B%0A        return query(exchange, name)%0A                .map(Long::parseLong)%0A                .map(OptionalLong::of)%0A                .orElseGet(OptionalLong::empty);%0A    %7D%0A%0A    public static %3CX extends Throwable%3E long requireQueryLong(HttpServerExchange exchange, String name, Supplier%3C? extends X%3E exceptionSupplier) throws X %7B%0A        return queryLong(exchange, name).orElseThrow(exceptionSupplier);%0A    %7D%0A%0A    public static long requireQueryLong(HttpServerExchange exchange, String name, String message) %7B%0A        return requireQueryLong(exchange, name, () -%3E new HttpException(UNPROCESSABLE_ENTITY, message));%0A    %7D%0A%0A    public static %3CK extends Enum%3CK%3E%3E Optional%3CK%3E queryEnum(HttpServerExchange exchange, String name, Class%3CK%3E type) %7B%0A        return Optional%0A                .ofNullable(exchange.getQueryParameters().get(name))%0A                .map(Deque::getFirst)%0A                .map(String::trim)%0A                .map(s -%3E s.isEmpty() ? null : s)%0A                .map(v -%3E K.valueOf(type, v));%0A    %7D%0A%0A    public static %3CX extends Throwable, K extends Enum%3CK%3E%3E K requireQueryEnum(HttpServerExchange exchange, String name, Class%3CK%3E type, Supplier%3C? extends X%3E exceptionSupplier) throws X %7B%0A        return queryEnum(exchange, name, type).orElseThrow(exceptionSupplier);%0A    %7D%0A%0A    public static %3CK extends Enum%3CK%3E%3E K requireQueryEnum(HttpServerExchange exchange, String name, Class%3CK%3E type, String message) %7B%0A        return requireQueryEnum(exchange, name, type, () -%3E new HttpException(UNPROCESSABLE_ENTITY, message));%0A    %7D
 %0A%7D%0A

add utility methods to parse query params