package org.mitallast.queue.rest.action.queues.create;

import com.google.inject.Inject;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.mitallast.queue.action.ActionListener;
import org.mitallast.queue.action.queues.create.CreateQueueRequest;
import org.mitallast.queue.action.queues.create.CreateQueueResponse;
import org.mitallast.queue.client.Client;
import org.mitallast.queue.common.settings.ImmutableSettings;
import org.mitallast.queue.common.settings.Settings;
import org.mitallast.queue.rest.BaseRestHandler;
import org.mitallast.queue.rest.RestController;
import org.mitallast.queue.rest.RestRequest;
import org.mitallast.queue.rest.RestSession;
import org.mitallast.queue.rest.response.StatusRestResponse;

public class RestCreateQueueAction extends BaseRestHandler {

    @Inject
    public RestCreateQueueAction(Settings settings, Client client, RestController controller) {
        super(settings, client);
        controller.registerHandler(HttpMethod.PUT, "/{queue}", this);
        controller.registerHandler(HttpMethod.POST, "/{queue}", this);
    }

    @Override
    public void handleRequest(RestRequest request, final RestSession session) {
        CreateQueueRequest createQueueRequest = new CreateQueueRequest(request.param("queue"));
        createQueueRequest.setSettings(ImmutableSettings.EMPTY);

        client.queues().createQueue(createQueueRequest, new ActionListener<CreateQueueResponse>() {
            @Override
            public void onResponse(CreateQueueResponse response) {
                session.sendResponse(new StatusRestResponse(HttpResponseStatus.CREATED));
            }

            @Override
            public void onFailure(Throwable e) {
                session.sendResponse(e);
            }
        });
    }
}

@@ -520,32 +520,95 @@
 tings.Settings;%0A
+import org.mitallast.queue.queues.QueueAlreadyExistsException;%0A
 import org.mital
@@ -838,16 +838,77 @@
 esponse;
+%0Aimport org.mitallast.queue.rest.response.StringRestResponse;
 %0A%0Apublic
@@ -1869,24 +1869,236 @@
 owable e) %7B%0A
+                if (e instanceof QueueAlreadyExistsException) %7B%0A                    session.sendResponse(new StringRestResponse(HttpResponseStatus.CONFLICT, %22Queue already exists%22));%0A                %7D else %7B%0A    
             
@@ -2126,16 +2126,34 @@
 nse(e);%0A
+                %7D%0A
         

fix rest create queue action