Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions managedvms/sparkjava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@
<configuration>
</configuration>
</plugin>
<!--
The SparkJava demo currently (2016-01-22) fails the Google Style
checks. We can uncomment this block when it is fixed.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand All @@ -101,7 +98,6 @@
<execution><goals><goal>check</goal></goals></execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

public class Main {

/**
* Starts the webapp on localhost:8080.
*/
public static void main(String[] args) {
port(8080);
UserController userController =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ResponseError(String message, String... args) {
this.message = String.format(message, (Object) args);
}

public ResponseError(Exception e) {
this.message = e.getMessage();
public ResponseError(Exception ex) {
this.message = ex.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ public class User {
private String name;
private String email;

/**
* Construct a user given a name and email. An ID is auto-generated for the user.
*/
public User(String name, String email) {
this(UUID.randomUUID().toString(), name, email);
}

/**
* Construct a user given an ID, name, and email.
*/
public User(String id, String name, String email) {
this.id = id;
this.email = email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

public class UserController {

/**
* Creates a controller that maps requests to gcloud-java functions.
*/
public UserController(final UserService userService) {
Spark.staticFileLocation("/public");

Expand All @@ -48,13 +51,13 @@ public UserController(final UserService userService) {
delete("/api/users/:id", (req, res) -> userService.deleteUser(req.params(":id")), json());

after((req, res) -> {
res.type("application/json");
});
res.type("application/json");
});

exception(IllegalArgumentException.class, (e, req, res) -> {
res.status(400);
res.body(toJson(new ResponseError(e)));
});
exception(IllegalArgumentException.class, (error, req, res) -> {
res.status(400);
res.body(toJson(new ResponseError(error)));
});
}

private static String toJson(Object object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public UserService(Datastore datastore) {
this.datastore = datastore;
}

/**
* Return a list of all users.
*/
public List<User> getAllUsers() {
Query<Entity> query =
Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + KINDNAME).build();
Expand All @@ -51,6 +54,9 @@ public List<User> getAllUsers() {
return users;
}

/**
* Create a new user and add it to Cloud Datastore.
*/
public User createUser(String name, String email) {
failIfInvalid(name, email);
User user = new User(name, email);
Expand All @@ -65,13 +71,19 @@ public User createUser(String name, String email) {
return user;
}

/**
* Delete a user from Cloud Datastore.
*/
public String deleteUser(String id) {
KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME);
Key key = keyFactory.newKey(id);
datastore.delete(key);
return "ok";
}

/**
* Updates a user in Cloud Datastore.
*/
public User updateUser(String id, String name, String email) {
failIfInvalid(name, email);
KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME);
Expand Down