Skip to content

Commit

Permalink
Fix incomplete result
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvord committed Mar 23, 2020
1 parent 8422534 commit b6b62d3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/de/hzg/wpi/xenv/beamtimedb/Beamtimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

Expand All @@ -29,6 +29,10 @@
public class Beamtimes {
private final Logger logger = LoggerFactory.getLogger(Beamtimes.class);

private String getUsernameOrDefault(String fieldName, Document document){
return Optional.ofNullable((Document) document.get(fieldName)).orElse(new Document("username", "unknown")).getString("username");
}

@GET
@GZIP
public void get(@Context MongoDatabase mongoClient, @Suspended final AsyncResponse response){
Expand All @@ -37,9 +41,9 @@ public void get(@Context MongoDatabase mongoClient, @Suspended final AsyncRespon
.map(document ->
new Document("id", document.get("_id").toString())
.append("beamtimeId", document.getString("beamtimeId"))
.append("applicant", ((Document) document.get("applicant")).getString("username"))
.append("leader", ((Document) document.get("leader")).getString("username"))
.append("pi", ((Document) document.get("pi")).getString("username"))
.append("applicant", getUsernameOrDefault("applicant", document))
.append("leader", getUsernameOrDefault("leader", document))
.append("pi", getUsernameOrDefault("pi", document))
)
.into(Lists.newArrayList(),
(result, throwable) -> {
Expand Down

0 comments on commit b6b62d3

Please sign in to comment.