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;
}
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();
}
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);
}
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);
}
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);
}
Aggregations