Last active
March 12, 2016 12:26
-
-
Save taku0/ed40b5f3aa86881b36d5 to your computer and use it in GitHub Desktop.
ユーザのコレクションをJAX-RSで書いた場合とFinchで書いた場合(コンパイルしていない)
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
object Main { | |
val users: Users = new Users() | |
val api = { | |
import EndPoints._ | |
getUsers :+: addNewUser :+: putUser :+: deleteUser :+: | |
getUser:+: getUserId :+: getUserName :+:putUserName :+: | |
} | |
object EndPoints { | |
val users = "users".map { _ => users } | |
val usersAndId = users :: string | |
val getUsers = get(users) { users => | |
Ok(users.toJson) | |
} | |
val addNewUser = post(users :: body.as[JsonObject]) { (users, body) => | |
Ok(users.addNewUser(body)) | |
} | |
val putUser = put(usersAndId :: body.as[JsonObject]) { (users, id, body) => | |
users.putUser(id, body) | |
NoContent[Unit] | |
} | |
val deleteUser = delete(usersAndId) { (users, id) => | |
users.deleteUser(id) | |
NoContent | |
} | |
val user = usersAndId.map(_tupled).map { | |
case (users, id) => users.getUser(id) | |
} | |
val getUser = get(user) { user => | |
Ok(user.toJson) | |
} | |
val getUserId = get(user :: "id") { user => | |
Ok(user.getId) | |
} | |
val getUserName = get(user :: "name") { id => | |
Ok(user.getName) | |
} | |
val putUserName = put(user :: "name" :: body) { (id, name) => | |
user.setName(name) | |
NoContent | |
} | |
} | |
} |
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
@Path("/users") | |
class Users { | |
private Map<String, User> users = new ConcurrentHashMap<String, User>(); | |
@GET | |
@Produces("application/json") | |
public JsonObject toJson() { | |
return null; // TODO | |
} | |
@Path("{id}") | |
public User getUser(@PathParam("id") String id) { | |
if (users.containsKey(id)) { | |
return users.get(id); | |
} else { | |
throw new NoSuchElementException(); | |
} | |
} | |
@POST | |
@Consumes("application/json") | |
public String addNewUser(JsonObject json) { | |
String id = generateId(); | |
json.put("id", id); | |
addUser(new User(json)); | |
return id; | |
} | |
private String generateId() { | |
return null; // TODO | |
} | |
public void putUser(User user) { | |
users.put(user.getId(), user); | |
} | |
@PUT | |
@Path("{id}") | |
@Consumes("application/json") | |
public void putUser(@PathParam("id") String id, JsonObject json) { | |
if (users.containsKey(id)) { | |
json.put("id", id); | |
putUser(new User(json)); | |
} else { | |
throw new NoSuchElementException(); | |
} | |
} | |
@DELETE | |
@Path("{id}") | |
public void deleteUser(@PathParam("id") String id) { | |
if (users.remove(id) == null) { | |
throw new NoSuchElementException(); | |
} | |
} | |
} | |
class User { | |
private String id; | |
private String name; | |
public User(String id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public User(JsonObject json) { | |
// TODO | |
} | |
@GET | |
@Produces("application/json") | |
public JsonObject toJson() { | |
return null; // TODO | |
} | |
@GET | |
@Path("id") | |
@Produces("text/plain") | |
public String getId() { | |
return id; | |
} | |
@GET | |
@Path("name") | |
@Produces("text/plain") | |
public String getName() { | |
return name; | |
} | |
@PUT | |
@Path("name") | |
@Consumes("text/plain") | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment