Skip to content
This repository has been archived by the owner on Mar 2, 2023. It is now read-only.

Commit

Permalink
Version 1.2.0 - Support for patch and put
Browse files Browse the repository at this point in the history
  • Loading branch information
bradcypert committed Dec 16, 2015
1 parent 58a8d73 commit cc7e484
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.bradcypert'
version '1.1.2'
version '1.2.0'

sourceCompatibility = 1.8

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bradcypert/ginger/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Methods {
String[] value() default {"GET", "PUT", "POST", "DELETE"};
String[] value() default {"GET", "PUT", "POST", "DELETE", "PATCH"};
}
10 changes: 6 additions & 4 deletions src/main/java/com/bradcypert/ginger/Model.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.bradcypert.ginger;

public interface Model {
default String save(PropertyMap map) { return "{\"Save is set up\": false}"; };
default String fetch(String id) { return "{\"Fetch is set up\": false}"; };
default String fetchAll() { return "{\"FetchAll is set up\": false}"; };
default String remove(String id) { return "{\"Remove is set up\": false}"; };
default String save(PropertyMap map) { return "{\"Save is set up\": false}"; }
default String fetch(String id) { return "{\"Fetch is set up\": false}"; }
default String fetchAll() { return "{\"FetchAll is set up\": false}"; }
default String remove(String id) { return "{\"Remove is set up\": false}"; }
default String update(String id, PropertyMap map) { return "{\"Remove is set up\": false}"; }
default String replace(String id, PropertyMap map) { return "{\"Remove is set up\": false}"; }
}
50 changes: 34 additions & 16 deletions src/main/java/com/bradcypert/ginger/Resource.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.bradcypert.ginger;

import com.bradcypert.ginger.helpers.RequestHelpers;
import spark.Request;
import spark.Response;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

import spark.Request;
import spark.Response;
import static spark.Spark.*;

public class Resource {
Expand All @@ -22,8 +19,8 @@ public class Resource {

public Resource(Class clazz) {
this.resourceClass = clazz;
this.exposedProperties = new ArrayList<String>();
this.methods = new ArrayList<String>();
this.exposedProperties = new ArrayList<>();
this.methods = new ArrayList<>();
this.basePath = "";

try {
Expand Down Expand Up @@ -62,25 +59,28 @@ public void generateRoutes() {
break;
case "POST":
post(path+"/", this::handlePost);
break;
case "PATCH":
patch(path+"/", this::handlePatch);
}
}
}

private void generateExposedProperties() {
Field[] fields = this.resourceClass.getDeclaredFields();
Arrays.asList(fields).forEach(field ->
Arrays.asList(field.getDeclaredAnnotations()).forEach(annotation -> {
if (annotation.toString().endsWith("ginger.Exposed()")) {
this.exposedProperties.add(field.getName());
}
})
Arrays.asList(field.getDeclaredAnnotations()).forEach(annotation -> {
if (annotation.toString().endsWith("ginger.Exposed()")) {
this.exposedProperties.add(field.getName());
}
})
);
}

private void generateResourceMethods() {
Annotation[] annotations = this.resourceClass.getAnnotations();
Arrays.asList(annotations).forEach(annotation ->
Arrays.asList(((Methods) annotation).value()).forEach(value -> this.methods.add(value))
Arrays.asList(((Methods) annotation).value()).forEach(this.methods::add)
);
}

Expand All @@ -98,9 +98,16 @@ private String handleGet(spark.Request request, Response response) throws Except
return (String) fetch.invoke(instance, request.params("id"));
}

private String handlePut(spark.Request request, Response response) {
private String handlePut(spark.Request request, Response response) throws Exception {
Method put = this.resourceClass.getMethod("replace", String.class);
response.type("application/json");
return "";
if(!this.exposedProperties.isEmpty() && !RequestHelpers.containsParams(request, this.exposedProperties)){
response.status(400);
return "{error: 'missing required parameters'}";
} else {
response.status(200);
return (String) put.invoke(instance, request.params("id"), new PropertyMap(request, this.exposedProperties));
}
}

private String handleDelete(spark.Request request, Response response) throws Exception {
Expand All @@ -110,6 +117,18 @@ private String handleDelete(spark.Request request, Response response) throws Exc
return (String) delete.invoke(instance, request.params("id"));
}

private String handlePatch(Request request, Response response) throws Exception{
Method patch = this.resourceClass.getMethod("update", String.class);
response.type("application/json");
if(!this.exposedProperties.isEmpty() && !RequestHelpers.containsParams(request, this.exposedProperties)){
response.status(400);
return "{error: 'missing requiredf parameters'}";
} else {
response.status(200);
return (String) patch.invoke(instance, request.params("id"), new PropertyMap(request, this.exposedProperties));
}
}

private String handlePost(spark.Request request, Response response) throws Exception {
Method save = this.resourceClass.getMethod("save", PropertyMap.class);
response.type("application/json");
Expand All @@ -121,7 +140,6 @@ private String handlePost(spark.Request request, Response response) throws Excep
response.status(201);
return (String) save.invoke(instance, new PropertyMap(request, this.exposedProperties));
}

}

}

0 comments on commit cc7e484

Please sign in to comment.