Last active
October 4, 2015 04:44
-
-
Save mmts1007/247abdc95d81b72bbfaa 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
import static spark.Spark.after; | |
import static spark.Spark.delete; | |
import static spark.Spark.get; | |
import static spark.Spark.halt; | |
import static spark.Spark.post; | |
import static spark.Spark.put; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Optional; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import spark.ResponseTransformer; | |
public class TaskSpark { | |
public static class Task { | |
public Task() { | |
} | |
public Task(String title, String content) { | |
this.title = title; | |
this.content = content; | |
} | |
public Task(Long id, String title, String content) { | |
this.id = id; | |
this.title = title; | |
this.content = content; | |
} | |
public Long id; | |
public String title; | |
public String content; | |
} | |
static class TaskService { | |
Map<Long, Task> taskStorage = new HashMap<>(); | |
long sequence = 0L; | |
List<Task> all() { | |
return new ArrayList<>(taskStorage.values()); | |
} | |
Task find(long id) { | |
return taskStorage.get(id); | |
} | |
Task create(Task task) { | |
if (task == null) { | |
throw new IllegalArgumentException("Argument is required"); | |
} | |
sequence++; | |
task.id = sequence; | |
taskStorage.put(sequence, task); | |
return task; | |
} | |
Optional<Task> update(Task task) { | |
if (task == null) { | |
throw new IllegalArgumentException("Argument is required"); | |
} | |
taskStorage.put(task.id, task); | |
return Optional.of(task); | |
} | |
boolean delete(long id) { | |
return taskStorage.remove(id) != null; | |
} | |
} | |
public static void main(String[] args) { | |
TaskService taskService = new TaskService(); | |
get("/tasks", (req, res) -> { | |
return taskService.all(); | |
} , new JsonTransformer()); | |
get("/tasks/:id", (req, res) -> { | |
if (!req.params("id").matches("\\d+")) { | |
halt(400, "Bad Request"); | |
} | |
long id = Long.valueOf(req.params("id")); | |
Task task = taskService.find(id); | |
if (task == null) { | |
halt(404, "Not found"); | |
} | |
return task; | |
} , new JsonTransformer()); | |
post("/tasks", (req, res) -> { | |
Task task = taskService.create(new Task(req.queryParams("title"), req.queryParams("content"))); | |
res.status(201); | |
return task; | |
} , new JsonTransformer()); | |
put("/tasks/:id", (req, res) -> { | |
if (!req.params("id").matches("\\d+")) { | |
halt(400, "Bad Request"); | |
} | |
long id = Long.valueOf(req.params("id")); | |
Optional<Task> task = taskService | |
.update(new Task(id, req.queryParams("title"), req.queryParams("content"))); | |
res.status(200); | |
return task.get(); | |
} , new JsonTransformer()); | |
delete("/tasks/:id", (req, res) -> { | |
if (!req.params("id").matches("\\d+")) { | |
halt(400, "Bad Request"); | |
} | |
Long id = Long.valueOf(req.params("id")); | |
boolean isDeleted = taskService.delete(id); | |
if (isDeleted) { | |
halt(204, "No content"); | |
} | |
halt(404, "Not found"); | |
return null; | |
}); | |
after((request, response) -> { | |
response.type("application/json"); | |
}); | |
} | |
static class JsonTransformer implements ResponseTransformer { | |
ObjectMapper mapper = new ObjectMapper(); | |
@Override | |
public String render(Object model) throws Exception { | |
return mapper.writeValueAsString(model); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment