package io.quarkus.cache.runtime.devconsole;

import java.util.Optional;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.quarkus.cache.Cache;
import io.quarkus.cache.CaffeineCache;
import io.quarkus.cache.runtime.CaffeineCacheSupplier;
import io.quarkus.cache.runtime.caffeine.CaffeineCacheImpl;
import io.quarkus.devconsole.runtime.spi.DevConsolePostHandler;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.vertx.http.runtime.devmode.Json;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.ext.web.RoutingContext;

@Recorder
public class CacheDevConsoleRecorder {
    public Handler<RoutingContext> clearCacheHandler() {
        return new DevConsolePostHandler() {
            int code;
            String message = "";

            @Override
            protected void handlePost(RoutingContext event, MultiMap form) throws Exception {
                String cacheName = form.get("name");
                Optional<Cache> cache = CaffeineCacheSupplier.cacheManager().getCache(cacheName);
                if (cache.isPresent() && cache.get() instanceof CaffeineCache) {
                    CaffeineCacheImpl caffeineCache = (CaffeineCacheImpl) cache.get();

                    String action = form.get("action");
                    if (action.equalsIgnoreCase("clearCache")) {
                        caffeineCache.invalidateAll();
                    }
                    this.code = HttpResponseStatus.OK.code();
                    this.message = createResponseMessage(caffeineCache);
                    return;
                } else {
                    String errorMessage = "Cache for " + cacheName + " not found";
                    this.code = HttpResponseStatus.NOT_FOUND.code();
                    this.message = createResponseError(cacheName, errorMessage);
                }
            }

            @Override
            protected void actionSuccess(RoutingContext event) {
                event.response().setStatusCode(this.code);
                event.response().end(this.message);
            }

            private String createResponseMessage(CaffeineCacheImpl cache) {
                Json.JsonObjectBuilder object = Json.object();
                object.put("name", cache.getName());
                object.put("size", cache.getSize());
                return object.build();
            }

            private String createResponseError(String name, String error) {
                Json.JsonObjectBuilder object = Json.object();
                object.put("name", name);
                object.put("error", error);
                return object.build();
            }
        };
    }
}

@@ -39,16 +39,238 @@
 nsole;%0A%0A
+import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;%0Aimport static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;%0Aimport static io.netty.handler.codec.http.HttpResponseStatus.OK;%0A%0A
 import j
@@ -948,63 +948,8 @@
 () %7B
-%0A            int code;%0A            String message = %22%22;
 %0A%0A  
@@ -1046,25 +1046,8 @@
 orm)
- throws Exception
  %7B%0A 
@@ -1539,18 +1539,141 @@
 ateAll()
-;%0A
+.subscribe().with(ignored -%3E %7B%0A                            endResponse(event, OK, createResponseMessage(caffeineCache));%0A    
         
@@ -1677,32 +1677,34 @@
                %7D
+);
 %0A               
@@ -1712,84 +1712,103 @@
     
-this.code = HttpResponseStatus.OK.code();%0A                    this.message =
+%7D else if (action.equalsIgnoreCase(%22refresh%22)) %7B%0A                        endResponse(event, OK,
  cre
@@ -1840,16 +1840,17 @@
 neCache)
+)
 ;%0A      
@@ -1867,15 +1867,226 @@
     
-return;
+%7D else %7B%0A                        String errorMessage = %22Invalid action: %22 + action;%0A                        endResponse(event, INTERNAL_SERVER_ERROR, createResponseError(cacheName, errorMessage));%0A                    %7D
 %0A   
@@ -2214,91 +2214,37 @@
     
-this.code = HttpResponseStatus.NOT_FOUND.code();%0A                    this.message =
+endResponse(event, NOT_FOUND,
  cre
@@ -2280,24 +2280,25 @@
 rrorMessage)
+)
 ;%0A          
@@ -2336,58 +2336,32 @@
     
-@Override%0A            protec
+priva
 te
-d
  void 
-actionSuccess
+endResponse
 (Rou
@@ -2377,16 +2377,59 @@
 xt event
+, HttpResponseStatus status, String message
 ) %7B%0A    
@@ -2475,17 +2475,21 @@
 ode(
-thi
+statu
 s.code
+()
 );%0A 
@@ -2528,13 +2528,8 @@
 end(
-this.
 mess
@@ -2541,32 +2541,134 @@
 %0A            %7D%0A%0A
+            @Override%0A            protected void actionSuccess(RoutingContext event) %7B%0A            %7D%0A%0A
             priv

Fix cache clear from dev UI