From 5812cbafc57658120b0d2370342efe897d39661f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 19 Dec 2017 16:36:16 +0300 Subject: do not delete last email --- .../src/main/java/com/juick/service/EmailServiceImpl.java | 8 +++++++- .../src/test/java/com/juick/service/MessageServiceTest.java | 12 +++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'juick-server-jdbc') diff --git a/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java index c42641df..57aa59f7 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java @@ -20,6 +20,7 @@ package com.juick.service; import org.apache.commons.lang3.StringUtils; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -57,7 +58,12 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService { @Override public boolean deleteEmail(Integer userId, String account) { - return getJdbcTemplate().update("DELETE FROM emails WHERE user_id=? AND email=?", userId, account) > 0; + return getNamedParameterJdbcTemplate().update("DELETE FROM emails " + + "WHERE (SELECT COUNT(*) cnt FROM (select user_id, email FROM emails e) c WHERE user_id=:uid) > 1 " + + "AND user_id=:uid AND email=:email", + new MapSqlParameterSource() + .addValue("uid", userId) + .addValue("email", account)) > 0; } @Override 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 d9b0f1c3..0f6f7c7a 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 @@ -62,6 +62,8 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { private CrosspostService crosspostService; @Inject private PMQueriesService pmQueriesService; + @Inject + private EmailService emailService; static int ugnichId, freefdId, mid; @@ -172,10 +174,18 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { } @Test public void lastJidShouldNotBeDeleted() { - int ugnich_id = userService.createUser("hugnich", "x"); + int ugnich_id = userService.createUser("hugnich2", "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)); } + @Test + public void lastEmailShouldNotBeDeleted() { + int ugnich_id = userService.createUser("hugnich3", "x"); + jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "first@localhost"); + jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "second@localhost"); + assertThat(emailService.deleteEmail(ugnich_id, "second@localhost"), equalTo(true)); + assertThat(emailService.deleteEmail(ugnich_id, "first@localhost"), equalTo(false)); + } } -- cgit v1.2.3