Last active
August 29, 2015 14:12
-
-
Save ivanursul/6d918d4c72188844e9aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 org.ivanursul.spark; | |
| import static spark.Spark.delete; | |
| import static spark.Spark.get; | |
| import static spark.Spark.post; | |
| import static spark.Spark.put; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import org.ivanursul.spark.dto.Person; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| public class App { | |
| private static Long incrId = 1L; | |
| private static Map<Long, Person> personsMap = new HashMap<Long, Person>(); | |
| private static Gson gson = new GsonBuilder().create(); | |
| public static void main( final String[] args ) { | |
| get("/persons", (request, response) -> { | |
| Long id = Long.valueOf(request.queryParams("id")); | |
| Person person = personsMap.get(id); | |
| return gson.toJson(person); | |
| }); | |
| post("/persons", (request, response) -> { | |
| Long generatedId = getId(); | |
| Person person = gson.fromJson(request.body(), Person.class); | |
| person.setId(generatedId); | |
| personsMap.put(generatedId, person); | |
| return gson.toJson(person); | |
| }); | |
| put("/persons", (request, response) -> { | |
| Long id = Long.valueOf(request.queryParams("id")); | |
| Person person = gson.fromJson(request.body(), Person.class); | |
| personsMap.put(id, person); | |
| return gson.toJson(person); | |
| }); | |
| delete("/persons", (request, response) -> { | |
| Long id = Long.valueOf(request.queryParams("id")); | |
| personsMap.remove(id); | |
| return "Person removed"; | |
| }); | |
| } | |
| private static Long getId() { | |
| synchronized (incrId) { | |
| return incrId++; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment