diff options
author | Vitaly Takmazov | 2017-12-19 15:44:50 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-12-19 15:44:50 +0300 |
commit | eab80ba585ae61a3ddef7a785a87ead7f39e9efe (patch) | |
tree | 6cf2a06d35184a490c6f768cb8a10c89d3ff43e0 /juick-server-jdbc/src | |
parent | 720dbdbfcd0d9561bc55deafa2c1c1563d942001 (diff) |
jdbc: do not delete last jid
Diffstat (limited to 'juick-server-jdbc/src')
-rw-r--r-- | juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java | 7 | ||||
-rw-r--r-- | juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java | 8 |
2 files changed, 14 insertions, 1 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java index b8835c7f..6196af86 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java @@ -703,7 +703,12 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Transactional @Override public boolean deleteJID(int uid, String jid) { - return getJdbcTemplate().update("DELETE FROM jids WHERE user_id=? AND jid=?", uid, jid) > 0; + return getNamedParameterJdbcTemplate().update("DELETE FROM jids " + + "WHERE (SELECT COUNT(*) cnt FROM (select user_id, jid FROM jids j) c WHERE user_id=:uid) > 1 " + + "AND user_id=:uid AND jid=:jid", + new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("jid", jid)) > 0; } @Transactional 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 23e01fd2..d9b0f1c3 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 @@ -170,4 +170,12 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { assertEquals("tags string should be empty", StringUtils.EMPTY, msg4.getTagsString()); messagesService.deleteMessage(user_id, mid4); } + @Test + public void lastJidShouldNotBeDeleted() { + int ugnich_id = userService.createUser("hugnich", "x"); + jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "firstjid@localhost", 1); + jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "secondjid@localhost", 1); + assertThat(userService.deleteJID(ugnich_id, "secondjid@localhost"), equalTo(true)); + assertThat(userService.deleteJID(ugnich_id, "firstjid@localhost"), equalTo(false)); + } } |