From ca2db58145c4663a22f44f5f98a67aa03fbe8d38 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 23 Aug 2017 11:59:55 +0300 Subject: server-jdbc: fix favorites count --- juick-core/src/main/java/com/juick/User.java | 9 +++++--- juick-server-jdbc/build.gradle | 1 + .../com/juick/service/MessagesServiceImpl.java | 24 +++++++++++----------- .../java/com/juick/service/MessageServiceTest.java | 17 ++++++++++++++- src/test/java/com/juick/tests/ApiTests.java | 4 ++++ 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/juick-core/src/main/java/com/juick/User.java b/juick-core/src/main/java/com/juick/User.java index 26c5642b..1ee8c920 100644 --- a/juick-core/src/main/java/com/juick/User.java +++ b/juick-core/src/main/java/com/juick/User.java @@ -21,9 +21,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; -import javax.xml.bind.annotation.*; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; +import java.io.Serializable; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -31,7 +34,7 @@ import java.util.List; */ @XmlRootElement(name = "user", namespace = "http://juick.com/user") @XmlAccessorType() -public class User { +public class User implements Serializable { private int uid; private String name; private Object avatar; diff --git a/juick-server-jdbc/build.gradle b/juick-server-jdbc/build.gradle index 17ca88a9..9b19bcb4 100644 --- a/juick-server-jdbc/build.gradle +++ b/juick-server-jdbc/build.gradle @@ -41,6 +41,7 @@ dependencies { providedRuntime "commons-fileupload:commons-fileupload:1.3.3" + testCompile project(path: ':juick-core', configuration: 'testArtifacts') testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" testCompile "junit:junit:${rootProject.junitVersion}" testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 225845fe..caffda14 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -214,15 +214,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override public boolean recommendMessage(final int mid, final int vuid) { - boolean res = getJdbcTemplate().update( - "INSERT IGNORE INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", - vuid, mid) == 1; - - if (res) - getJdbcTemplate().update( - "UPDATE messages SET likes = likes + 1 WHERE message_id = ?", - mid); - return res; + int wasDeleted = getJdbcTemplate() + .update("DELETE FROM favorites WHERE user_id=? AND message_id=?", vuid, mid); + return wasDeleted == 0 + && getJdbcTemplate() + .update("INSERT INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1; } @Transactional(readOnly = true) @@ -292,11 +288,13 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "messages.ts," + "messages.readonly, messages.privacy, messages.replies," + "messages.attach, messages.place_id, messages.lat," - + "messages.lon, messages.likes, messages.hidden," + + "messages.lon, COUNT(favorites.user_id) as likes, messages.hidden," + "txt.tags, txt.repliesby, txt.txt, '' as q FROM messages " + "INNER JOIN users ON messages.user_id = users.id " + "INNER JOIN messages_txt AS txt " + "ON messages.message_id = txt.message_id " + + "LEFT JOIN favorites " + + "ON messages.message_id = favorites.message_id " + "WHERE messages.message_id = ?", new MessageMapper(), mid); @@ -709,12 +707,14 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "messages.ts," + "messages.readonly,messages.privacy,messages.replies," + "messages.attach,messages.place_id,messages.lat," - + "messages.lon,messages.likes,messages.hidden," + + "messages.lon,COUNT(favorites.user_id) AS likes,messages.hidden," + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q " + "FROM (messages INNER JOIN messages_txt " + "ON messages.message_id=messages_txt.message_id) " + "INNER JOIN users ON messages.user_id=users.id " - + "WHERE messages.message_id IN (:ids) ORDER BY messages.message_id DESC", + + "LEFT JOIN favorites " + + "ON messages.message_id = favorites.message_id " + + "WHERE messages.message_id IN (:ids) GROUP BY messages.message_id ORDER BY messages.message_id DESC", new MapSqlParameterSource("ids", mids), new MessageMapper()); } diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index fed5dad8..fae938cc 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -24,6 +24,9 @@ import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import javax.inject.Inject; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + /** * Created by aalexeev on 11/25/16. */ @@ -31,10 +34,22 @@ import javax.inject.Inject; public class MessageServiceTest extends AbstractJUnit4SpringContextTests { @Inject private MessagesService messagesService; - + @Inject + private UserService userService; @Test public void getMyFeed() { messagesService.getMyFeed(1, 1000000); } + @Test + public void likesCountShouldMatch() { + int uid = userService.createUser("ugnich", "secret"); + int mid = messagesService.createMessage(uid, "test", null, null); + int mid2 = messagesService.createMessage(uid, "test2", null, null); + assertThat(messagesService.recommendMessage(mid, uid), equalTo(true)); + assertThat(messagesService.getMessage(mid).getLikes(), equalTo(1)); + assertThat(messagesService.recommendMessage(mid, uid), equalTo(false)); + assertThat(messagesService.getMessage(mid).getLikes(), equalTo(0)); + assertThat(messagesService.getAll(uid, 0).size(), equalTo(2)); + } } diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 8a6fc49e..7001d3f0 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -227,6 +227,10 @@ public class ApiTests { User readerUser = userService.getUserByUID(readerUid).orElse(new User()); assertEquals("should be subscribed", "Subscribed", juickProtocol.getReply(readerUser, "S #" + mid)); + /* TODO: move from juick-legacy + assertEquals("should be favorited", "Message added to your recommendations", + juickProtocol.getReply(readerUser, "! #" + mid)); + */ assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments(mid, uid).size()); assertEquals("should be subscribed", "Subscribed", -- cgit v1.2.3