diff --git a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java index e29522d..5441bd8 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -520,6 +520,34 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { } return toReturn; } + + /** + * helper method that return whether the user + * @param userid user identifier + * @param feedid the feed identifier + * @return true if the feed id liked already + */ + private boolean isFeedLiked(String userid, String feedid) { + OperationResult> result = null; + try { + result = conn.getKeyspace().prepareQuery(cf_UserLikedFeeds) + .getKeySlice(userid) + .execute(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + + // Iterate rows and their columns looking for the feeid id + for (Row row : result.getResult()) { + for (Column column : row.getColumns()) { + if (column.getStringValue().compareTo(feedid)==0) + return true; + } + } + return false; + } + + /** * helper method that retrieve all the feed Ids belonging to an application * @param appid application identifier @@ -1309,25 +1337,31 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { e.printStackTrace(); return false; } - // Inserting data - MutationBatch m = conn.getKeyspace().prepareMutationBatch(); - //an entry in the feed CF - m.withRow(cf_Likes, like.getKey().toString()) - .putColumn("Timestamp", like.getTime().getTime()+"", null) - .putColumn("Userid", like.getUserid(), null) - .putColumn("Feedid",like.getFeedid(), null) - .putColumn("FullName",like.getFullName(), null) - .putColumn("ThumbnailURL", like.getThumbnailURL(), null); - //and an entry in the UserLikesCF - m.withRow(cf_UserLikedFeeds, like.getUserid()).putColumn(like.getKey(), like.getFeedid(), null); + if (isFeedLiked(like.getUserid(), feedId)) { + _log.info("User " + like.getUserid() + " already liked Feed " + feedId); + return true; + } + else { + // Inserting data + MutationBatch m = conn.getKeyspace().prepareMutationBatch(); + //an entry in the feed CF + m.withRow(cf_Likes, like.getKey().toString()) + .putColumn("Timestamp", like.getTime().getTime()+"", null) + .putColumn("Userid", like.getUserid(), null) + .putColumn("Feedid",like.getFeedid(), null) + .putColumn("FullName",like.getFullName(), null) + .putColumn("ThumbnailURL", like.getThumbnailURL(), null); + //and an entry in the UserLikesCF + m.withRow(cf_UserLikedFeeds, like.getUserid()).putColumn(like.getKey(), like.getFeedid(), null); - try { - m.execute(); - } catch (ConnectionException e) { - e.printStackTrace(); - return false; - } - return updateFeedLikesCount(toLike, true); + try { + m.execute(); + } catch (ConnectionException e) { + e.printStackTrace(); + return false; + } + return updateFeedLikesCount(toLike, true); + } } /** * {@inheritDoc} @@ -1721,7 +1755,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { if (column.getStringValue().compareTo(status[i].toString())==0) invitesIds.add(column.getName()); } - + } } else {