fixed unlike method

This commit is contained in:
Massimiliano Assante 2023-02-09 15:31:11 +01:00
parent 2ebd2b7f8f
commit 8b58a29d9f
4 changed files with 85 additions and 22 deletions

View File

@ -0,0 +1,60 @@
package org.gcube.portal.social.networking.ws.inputs;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Like Bean object
*/
public class LikeInputBean {
@JsonProperty("likeid")
@NotNull(message="likeid cannot be null")
@Size(message="likeid cannot be empty", min=1)
private String likeid;
@JsonProperty("postid")
@NotNull(message="postid cannot be null")
@Size(message="postid cannot be empty", min=1)
private String postid;
public LikeInputBean() {
super();
}
/**
* @param likeid
* @param postid
*/
public LikeInputBean(String likeid, String postid) {
super();
this.likeid = likeid;
this.postid = postid;
}
public String getLikeid() {
return likeid;
}
public void setLikeid(String likeid) {
this.likeid = likeid;
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
@Override
public String toString() {
return "LikeInputBean [likeid=" + likeid + ", postid=" + postid + "]";
}
}

View File

@ -7,35 +7,37 @@ import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* Post id object * Application id object
*/ */
//@ApiModel(description="An object containing the app_id field", value="Application")
public class PostId { public class PostId {
@JsonProperty("postid") @JsonProperty("postId")
@NotNull(message="postid cannot be null") @NotNull(message="postId cannot be null")
@Size(message="postid cannot be empty", min=1) @Size(message="postId cannot be empty", min=1)
private String postid; private String postId;
public PostId() { public PostId() {
super(); super();
} }
public PostId(String postid) { public PostId(String postId) {
super(); super();
this.postid = postid; this.postId = postId;
} }
public String getPostId() { public String getPostId() {
return postid; return postId;
} }
public void setPostId(String postid) { public void setPostId(String postId) {
this.postid = postid; this.postId = postId;
} }
@Override @Override
public String toString() { public String toString() {
return "PostId [postid=" + postid + "]"; return "PostId [postId=" + postId + "]";
} }
} }

View File

@ -20,6 +20,7 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.shared.Like; import org.gcube.portal.databook.shared.Like;
import org.gcube.portal.social.networking.ws.inputs.LikeInputBean;
import org.gcube.portal.social.networking.ws.inputs.PostId; import org.gcube.portal.social.networking.ws.inputs.PostId;
import org.gcube.portal.social.networking.ws.outputs.ResponseBean; import org.gcube.portal.social.networking.ws.outputs.ResponseBean;
import org.gcube.portal.social.networking.ws.utils.CassandraConnection; import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
@ -102,18 +103,18 @@ public class Likes {
public Response like( public Response like(
@NotNull(message="Post to like is missing") @NotNull(message="Post to like is missing")
@Valid @Valid
PostId postId) throws ValidationException { PostId post) throws ValidationException {
Caller caller = AuthorizationProvider.instance.get(); Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId(); String username = caller.getClient().getId();
logger.debug("Request of like coming from user " + username); logger.debug("Request of like coming from user " + username);
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
ResponseBean responseBean = new ResponseBean(); ResponseBean responseBean = new ResponseBean();
Status status = Status.OK; Status status = Status.OK;
boolean likeResultOperation = SocialUtils.like(username, postId.getPostId(), context); boolean likeResultOperation = SocialUtils.like(username, post.getPostId(), context);
if (likeResultOperation) if (likeResultOperation)
logger.info("Added like OK to postId " + postId.getPostId()); logger.info("Added like OK to postId " + post.getPostId());
else { else {
logger.error("Unable to like this post"+ postId.getPostId()); logger.error("Unable to like this post"+ post.getPostId());
responseBean.setMessage("Unable to like, something went wrong please see server log"); responseBean.setMessage("Unable to like, something went wrong please see server log");
responseBean.setSuccess(false); responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
@ -142,17 +143,17 @@ public class Likes {
public Response unlike( public Response unlike(
@NotNull(message="Post to unlike is missing") @NotNull(message="Post to unlike is missing")
@Valid @Valid
PostId postId) throws ValidationException { LikeInputBean likeInputBean) throws ValidationException {
Caller caller = AuthorizationProvider.instance.get(); Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId(); String username = caller.getClient().getId();
logger.debug("Request of unlike coming from user " + username); logger.debug("Request of unlike coming from user " + username);
ResponseBean responseBean = new ResponseBean(); ResponseBean responseBean = new ResponseBean();
Status status = Status.OK; Status status = Status.OK;
boolean likeResultOperation = SocialUtils.unlike(username, postId.getPostId()); boolean likeResultOperation = SocialUtils.unlike(username, likeInputBean.getLikeid(), likeInputBean.getPostid());
if (likeResultOperation) if (likeResultOperation)
logger.info("Unlike OK to postId " + postId.getPostId()); logger.info("Unlike OK to postId " + likeInputBean.getPostid());
else { else {
logger.error("Unable to unlike this post"+ postId.getPostId()); logger.error("Unable to unlike this post"+ likeInputBean.getPostid());
responseBean.setMessage("Unable to unlike, something went wrong please see server log"); responseBean.setMessage("Unable to unlike, something went wrong please see server log");
responseBean.setSuccess(false); responseBean.setSuccess(false);
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;

View File

@ -698,7 +698,7 @@ public class SocialUtils {
return likeCommitResult && resultNotifyLike; return likeCommitResult && resultNotifyLike;
} }
public static boolean unlike(String username, String postid) { public static boolean unlike(String username, String likeid, String postid) {
boolean unlikeCommitResult = false; boolean unlikeCommitResult = false;
// retrieve user information // retrieve user information
GCubeUser user; GCubeUser user;
@ -713,7 +713,7 @@ public class SocialUtils {
String thumbnailURL = user.getUserAvatarURL(); String thumbnailURL = user.getUserAvatarURL();
try { try {
unlikeCommitResult = CassandraConnection.getInstance().getDatabookStore().unlike(username, fullName, thumbnailURL); unlikeCommitResult = CassandraConnection.getInstance().getDatabookStore().unlike(username, likeid, postid);
} catch (Exception e) { } catch (Exception e) {
logger.error("Post not Found for this like ot could not unlike the post " + e.getMessage()); logger.error("Post not Found for this like ot could not unlike the post " + e.getMessage());
return false; return false;