Skip to content

Instantly share code, notes, and snippets.

@mmts1007 mmts1007/TaskSpark.java
Last active Oct 4, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.