/*
 * Copyright 2016, 2019, Matt Dean
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.oddcyb.items.handlers;

import com.google.gson.Gson;
import org.oddcyb.items.store.Store;
import spark.Request;
import spark.Response;
import spark.Route;

/**
 * Route that adds entries to a store.
 */
public class StoreAdd implements Route
{

    private final Store store;
    private final Gson gson = new Gson();
    
    public StoreAdd(Store store)
    {
        this.store = store;
    }
    
    @Override
    public Object handle(Request req, Response resp) throws Exception
    {
        String name = req.splat()[0];
        String json = req.body();
        
        Object exists = 
            this.store.add(name, this.gson.fromJson(json, Object.class));
        
        if ( exists == null )
        {
            resp.status(201);
            return "Created";
        }
        else
        {
            resp.status(409);
            return this.gson.toJson(exists);
        }
    }

}

@@ -630,16 +630,68 @@
 dlers;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import c
@@ -711,16 +711,17 @@
 n.Gson;%0A
+%0A
 import o
@@ -749,16 +749,17 @@
 .Store;%0A
+%0A
 import s
@@ -905,16 +905,217 @@
 oute%0A%7B%0A%0A
+    private static final String QUERY_PARAM_MODE_ADD = %22add%22;%0A    private static final String QUERY_PARAM_MODE_REPLACE = %22replace%22;%0A    private static final String QUERY_PARAM_MODE_APPEND = %22append%22;%0A%0A
     priv
@@ -1436,45 +1436,172 @@
     
-%0A        Object exists = %0A           
+String modeParam = req.queryParamOrDefault(%22mode%22, QUERY_PARAM_MODE_ADD);%0A%0A        Object object = this.gson.fromJson(json, Object.class);%0A        Object existing =
  thi
@@ -1612,57 +1612,207 @@
 ore.
+re
 a
-d
 d(name
-, this.gson.fromJson(json, O
+);%0A%0A        Object result = null;%0A%0A        if ( modeParam.equalsIgnoreCase(QUERY_PARAM_MODE_REPLACE) )%0A        %7B%0A            this.store.replace(name, o
 bject
-.class))
+);%0A            result = %22Replaced%22
 ;%0A  
@@ -1813,24 +1813,25 @@
 d%22;%0A        
+%7D
 %0A        if 
@@ -1823,24 +1823,29 @@
   %7D%0A        
+else 
 if ( exists 
@@ -1846,20 +1846,188 @@
 xist
-s == null )%0A
+ing != null && %0A                  modeParam.equalsIgnoreCase(QUERY_PARAM_MODE_APPEND) )%0A        %7B%0A            List entries = null;%0A%0A            if ( existing instanceof List )%0A    
     
@@ -2048,89 +2048,389 @@
     
-resp.status(201);%0A            return %22Created%22;%0A        %7D%0A        else%0A        %7B%0A
+    entries = (List) existing;%0A            %7D%0A            else%0A            %7B%0A                entries = new ArrayList%3CObject%3E();%0A                entries.add(existing);%0A            %7D%0A%0A            entries.add(object);%0A            this.store.replace(name, entries);%0A            result = %22Appended%22;%0A        %7D%0A        else%0A        %7B%0A            if ( existing != null )%0A            %7B%0A    
     
@@ -2467,16 +2467,20 @@
         
+    
 return t
@@ -2504,21 +2504,87 @@
 xist
-s);%0A        %7D
+ing);%0A            %7D%0A        %7D%0A%0A        resp.status(201);%0A        return result;
 %0A   

Allow 'add' ops to replace and append using a querystring param