From a28f045836c1f0cadbbfcbaf85cb03b7ed643039 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 22 Dec 2017 12:35:19 +0300 Subject: jdbc: write last reply time to message --- .../main/java/com/juick/service/MessagesServiceImpl.java | 3 ++- .../test/java/com/juick/service/MessageServiceTest.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'juick-server-jdbc') 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 721bfb4e..f579577f 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 @@ -190,7 +190,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ if (ridnew > 0) getJdbcTemplate().update( - "UPDATE messages SET replies = replies + 1 WHERE message_id = ?", + "UPDATE messages, (SELECT ts FROM replies) as r " + + "SET messages.replies = messages.replies + 1, messages.updated = r.ts WHERE message_id = ?", mid); return ridnew; } 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 0f6f7c7a..6bab2b1c 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 @@ -32,12 +32,15 @@ import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; +import java.sql.Timestamp; +import java.time.Instant; import java.util.ArrayList; import java.util.Collections; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertEquals; /** @@ -188,4 +191,17 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { assertThat(emailService.deleteEmail(ugnich_id, "second@localhost"), equalTo(true)); assertThat(emailService.deleteEmail(ugnich_id, "first@localhost"), equalTo(false)); } + @Test + public void messageUpdatedTimeShouldMatchLastReplyTime() { + int ugnich_id = userService.createUser("hugnich4", "x"); + int mid = messagesService.createMessage(ugnich_id, "yo", null, null); + Instant ts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?", + Timestamp.class, mid).toInstant(); + int rid = messagesService.createReply(mid, 0, ugnich_id, "people", null); + Instant rts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?", + Timestamp.class, mid).toInstant(); + assertThat(rts, greaterThan(ts)); + Message msg = messagesService.getReply(mid, rid); + assertThat(rts, equalTo(msg.getTimestamp())); + } } -- cgit v1.2.3