Skip to content

Instantly share code, notes, and snippets.

@scottbyrns
Created May 19, 2012 10:15
Show Gist options
  • Save scottbyrns/2730353 to your computer and use it in GitHub Desktop.
Save scottbyrns/2730353 to your computer and use it in GitHub Desktop.
package com.ideas.api.server.services;
import com.ideas.dao.LocationDAO;
import org.apache.commons.logging.Log;
import org.apache.cxf.jaxrs.ext.MessageContext;
import redis.clients.jedis.Jedis;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
/**
* Copyright (C) 2012 by Scott Byrns
* http://github.com/scottbyrns
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* <p/>
* Created 5/15/12 2:19 AM
*/
@Path("/location")
@Produces(MediaType.APPLICATION_JSON)
public class LocationService extends BaseService
{
private static final LocationDAO locationDAO = new LocationDAO();
@Context
private MessageContext context;
@GET
@Path("/set-with-latitude:{latitude}-and-longitude:{longitude}")
public APIResponse set (@PathParam("latitude") Long latitude, @PathParam("longitude") Long longitude)
{
String locationJSON = "";
Jedis jedis = new Jedis("localhost");
jedis.connect();
if (jedis.isConnected()) {
long uid = jedis.incr("latest-location-id");
locationJSON = "{\"id\": \"" + uid + "\", \"latitude\": \"" + latitude + "\", \"longitude\", \"" + longitude + "\"}";
// jedis.rpush("locations", "" + uid);
jedis.set("location-with-id-" + uid, locationJSON);
}
else {
}
APIResponse apiResponse = new APIResponse(locationJSON, 200);
return apiResponse;
}
@GET
@Path("/get-latest")
public APIResponse get ()
{
APIResponse apiResponse = createNewAPIResponse();
Jedis jedis = new Jedis("localhost");
jedis.connect();
if (jedis.isConnected()) {
apiResponse.setData(jedis.get("location-with-id-" + jedis.get("latest-location-id")));
}
return apiResponse;
}
@GET
@Path("/get-location-with-id-{id}")
public APIResponse getLocationById (@PathParam("id") Long id)
{
APIResponse apiResponse = createNewAPIResponse();
Jedis jedis = new Jedis("localhost");
jedis.connect();
if (jedis.isConnected()) {
apiResponse.setData(jedis.get("location-with-id-" + id));
}
return apiResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment