package com.drabiter.iona.route;

import java.lang.reflect.Field;
import java.net.HttpURLConnection;

import spark.Request;
import spark.Response;

import com.drabiter.iona.db.Database;
import com.drabiter.iona.http.ContentType;
import com.drabiter.iona.model.ModelCache;
import com.drabiter.iona.model.Property;
import com.drabiter.iona.utils.JsonUtil;
import com.drabiter.iona.utils.ModelUtil;
import com.j256.ormlite.dao.Dao;

public class PutRoute<T, I> extends BasicRoute<T, I> {

    public PutRoute(Class<T> modelClass, Class<I> idClass) {
        super(modelClass, idClass);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object handle(Request request, Response response) throws Exception {
        String body = request.body();
        String id = request.params("id");

        if (body == null || id == null) return null;

        T instance = JsonUtil.get().fromJson(body, modelClass);

        if (instance == null) return null;

        Property property = ModelCache.get().cache().get(ModelUtil.getEndpoint(modelClass));
        Field idField = property.getIdField();
        idField.setAccessible(true);
        idField.set(instance, castId(id));

        Dao<T, I> dao = (Dao<T, I>) Database.get().getDao(modelClass);
        int affected = dao.update(instance);

        if (affected == 0) return null; // TODO return properly

        response(response, HttpURLConnection.HTTP_OK, ContentType.JSON);

        return JsonUtil.get().toJson(instance);
    }

}

@@ -1322,49 +1322,17 @@
  == 
-0) return null; // TODO return properly%0A%0A
+1) %7B%0A    
     
@@ -1400,17 +1400,20 @@
 .JSON);%0A
-%0A
+    
         
@@ -1448,21 +1448,329 @@
 (instance);%0A
+        %7D else if (affected %3E 1) %7B%0A            response(response, HttpURLConnection.HTTP_CONFLICT, ContentType.TEXT);%0A            return %22Conflict resources%22;%0A        %7D else %7B%0A            response(response, HttpURLConnection.HTTP_GONE, ContentType.TEXT);%0A            return %22No resource modified%22;%0A        %7D%0A
     %7D%0A%0A%7D%0A

Add more PUT responses