Created
December 20, 2010 12:45
-
-
Save armed/748341 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import play.*; | |
import play.libs.WS.HttpResponse; | |
import play.mvc.*; | |
import play.mvc.results.NotFound; | |
import util.APICallResult; | |
import util.TaskDeserializer; | |
import util.TaskSerializer; | |
import java.lang.reflect.Type; | |
import java.util.*; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
import models.*; | |
public class Tasks extends Controller { | |
private static void renderWithSerializer(Object o) { | |
renderJSON(o, new TaskSerializer()); | |
} | |
public static void list() { | |
renderWithSerializer(APICallResult.create(Task.findAll())); | |
} | |
public static void get(Long id) { | |
Task t = Task.findById(id); | |
if (t != null) { | |
renderWithSerializer(APICallResult.create(t)); | |
} else { | |
notFound(String.format("Task (id == %s) not found", id)); | |
} | |
} | |
public static void saveOrUpdate(Task task) { | |
if (task != null) { | |
if (task.id == null) { | |
if (task.create()) { | |
response.status = Http.StatusCode.CREATED; | |
response.setHeader( | |
"Location", | |
Router.reverse( | |
String.format("Tasks.get(%s)", task.id)) | |
.toString()); | |
} else { | |
error(String.format("Task (id == %s) could not be created", | |
task.id)); | |
} | |
} else { | |
task.merge(); | |
} | |
} | |
} | |
public static void delete(Long id) { | |
Task t = Task.findById(id); | |
if (t != null) { | |
t.delete(); | |
} else { | |
notFound(String.format("Task (id == %s) not found", id)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment