Search in sources :

Example 6 with Photo

use of com.linkedin.restli.example.Photo in project rest.li by linkedin.

the class PhotoResource method find.

// find photos by title and/or format
// if both title and format are empty, any photo meets the search criteria
@Finder("titleAndOrFormat")
public List<Photo> find(@PagingContextParam PagingContext pagingContext, @QueryParam("title") @Optional String title, @QueryParam("format") @Optional PhotoFormats format) {
    final List<Photo> photos = new ArrayList<Photo>();
    int index = 0;
    final int begin = pagingContext.getStart();
    final int end = begin + pagingContext.getCount();
    final Collection<Photo> dbPhotos = _db.getData().values();
    for (Photo p : dbPhotos) {
        if (index == end) {
            break;
        } else if (index >= begin) {
            if (title == null || p.getTitle().equalsIgnoreCase(title)) {
                if (format == null || format == p.getFormat()) {
                    photos.add(p);
                }
            }
        }
        index++;
    }
    return photos;
}
Also used : ArrayList(java.util.ArrayList) Photo(com.linkedin.restli.example.Photo) Finder(com.linkedin.restli.server.annotations.Finder)

Example 7 with Photo

use of com.linkedin.restli.example.Photo in project rest.li by linkedin.

the class TestMockHttpServerFactory method runTest.

/**
   * Starts the server, makes a request, runs assertions, stops the server
   *
   * @param server the test server
   * @throws IOException
   * @throws RemoteInvocationException
   */
private void runTest(HttpServer server) throws IOException, RemoteInvocationException {
    server.start();
    Photo photoEntity = REST_CLIENT.sendRequest(PHOTOS_BUILDERS.get().id(1L).build()).getResponseEntity();
    Assert.assertEquals(photoEntity.getTitle(), "Photo 1");
    Album albumEntity = REST_CLIENT.sendRequest(ALBUMS_BUILDERS.get().id(1L).build()).getResponseEntity();
    Assert.assertEquals(albumEntity.getTitle(), "Awesome Album #1");
    server.stop();
}
Also used : Album(com.linkedin.restli.example.Album) Photo(com.linkedin.restli.example.Photo)

Example 8 with Photo

use of com.linkedin.restli.example.Photo in project rest.li by linkedin.

the class PhotoResource method update.

// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Photo entity) {
    final Photo currPhoto = _db.getData().get(key);
    if (currPhoto == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    //ID and URN are required fields, so use a dummy value to denote "empty" fields
    if ((entity.hasId() && entity.getId() != -1) || (entity.hasUrn() && !entity.getUrn().equals(""))) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo ID is not acceptable in request");
    }
    // make sure the ID in the entity is consistent with the key in the database
    entity.setId(key);
    entity.setUrn(String.valueOf(key));
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Photo(com.linkedin.restli.example.Photo)

Example 9 with Photo

use of com.linkedin.restli.example.Photo in project rest.li by linkedin.

the class PhotoResource method update.

// allow partial update to an existing photo
@Override
public UpdateResponse update(Long key, PatchRequest<Photo> patchRequest) {
    final Photo p = _db.getData().get(key);
    if (p == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    try {
        PatchApplier.applyPatch(p, patchRequest);
    } catch (DataProcessingException e) {
        return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
    }
    // photo's id and URN should not be changed
    p.setId(key);
    p.setUrn(String.valueOf(key));
    _db.getData().put(key, p);
    return new UpdateResponse(HttpStatus.S_202_ACCEPTED);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) Photo(com.linkedin.restli.example.Photo) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Example 10 with Photo

use of com.linkedin.restli.example.Photo in project rest.li by linkedin.

the class PhotoResource method batchGet.

@Override
public BatchResult<Long, Photo> batchGet(Set<Long> ids) {
    Map<Long, Photo> result = new HashMap<Long, Photo>();
    Map<Long, RestLiServiceException> errors = new HashMap<Long, RestLiServiceException>();
    for (Long key : ids) {
        if (get(key) != null) {
            result.put(key, get(key));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "No photo with id=" + key + " has been found."));
        }
    }
    return new BatchResult<Long, Photo>(result, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) Photo(com.linkedin.restli.example.Photo) BatchResult(com.linkedin.restli.server.BatchResult)

Aggregations

Photo (com.linkedin.restli.example.Photo)11 LatLong (com.linkedin.restli.example.LatLong)5 Test (org.testng.annotations.Test)5 EXIF (com.linkedin.restli.example.EXIF)4 UpdateResponse (com.linkedin.restli.server.UpdateResponse)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 DataProcessingException (com.linkedin.data.transform.DataProcessingException)1 Album (com.linkedin.restli.example.Album)1 BatchResult (com.linkedin.restli.server.BatchResult)1 CreateResponse (com.linkedin.restli.server.CreateResponse)1 PagingContext (com.linkedin.restli.server.PagingContext)1 Finder (com.linkedin.restli.server.annotations.Finder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1