social-networking-library-ws/src/main/java/org/gcube/portal/social/networking/ws/lib/Likes.java

148 lines
4.5 KiB
Java

package org.gcube.portal.social.networking.ws.lib;
import com.webcohesion.enunciate.metadata.Ignore;
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
import org.gcube.portal.databook.shared.Like;
import org.gcube.portal.social.networking.ws.outputs.ResponseBean;
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import javax.validation.ValidationException;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.List;
/**
* REST interface for the social networking library (likes).
* @author Ahmed Ibrahim ISTI-CNR
*/
@Path("lib/likes")
@RequestHeaders ({
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
@RequestHeader( name = "Content-Type", description = "application/json")
})
@Ignore
public class Likes {
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Likes.class);
//library api
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("like-lib")
public Response likeLib(
@NotNull(message="like to add is missing")
@Valid
Like like
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("Writing like with likeid " + like.getKey());
boolean result = CassandraConnection.getInstance().getDatabookStore().like(like);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to write comment.", e);
responseBean.setMessage(e.getMessage());
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
}
return Response.status(status).entity(responseBean).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("unlike-lib")
public Response unlikeLib(
@QueryParam("userid")
String userid,
@QueryParam("likeid")
String likeid,
@QueryParam("feedid")
String feedid
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("Writing unlike with likeid " + likeid);
boolean result = CassandraConnection.getInstance().getDatabookStore().unlike(userid,likeid,feedid);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to write comment.", e);
responseBean.setMessage(e.getMessage());
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
}
return Response.status(status).entity(responseBean).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("get-all-liked-posts-by-user-lib")
public Response getAllLikedPostIdsByUserLib(
@QueryParam("userid")
String userid
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
List<String> likes = null;
try{
logger.info("Retrieving likes for user id " + userid);
likes = CassandraConnection.getInstance().getDatabookStore().getAllLikedPostIdsByUser(userid);
responseBean.setResult(likes);
responseBean.setMessage("");
responseBean.setSuccess(true);
}catch(Exception e){
logger.error("Unable to retrieve such likes.", e);
responseBean.setMessage(e.getMessage());
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
}
return Response.status(status).entity(responseBean).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("get-all-likes-by-post-lib")
public Response getAllLikesByPostLib(
@QueryParam("postid")
String postid
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
List<Like> likes = null;
try{
logger.info("Retrieving likes for post id " + postid);
likes = CassandraConnection.getInstance().getDatabookStore().getAllLikesByPost(postid);
responseBean.setResult(likes);
responseBean.setMessage("");
responseBean.setSuccess(true);
}catch(Exception e){
logger.error("Unable to retrieve such likes.", e);
responseBean.setMessage(e.getMessage());
responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR;
}
return Response.status(status).entity(responseBean).build();
}
}