diff options
author | Vitaly Takmazov | 2019-04-06 21:53:13 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-04-06 23:10:23 +0300 |
commit | 384c61ceae3301c6bc92ee6f591ed9d186b15204 (patch) | |
tree | c337cb96e242d81794999435b526e423a3fa6415 /src/main/java/com/juick/service | |
parent | 347674599f4ee8e2682c8ab7a80d87e0080f310c (diff) |
Message properties
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r-- | src/main/java/com/juick/service/MessagesService.java | 3 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 39 |
2 files changed, 41 insertions, 1 deletions
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index 080319cc..b29337b2 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -140,4 +140,7 @@ public interface MessagesService { boolean replyExists(URI replyUri); boolean deleteReply(URI userUri, URI replyUri); + + String getMessageProperty(Integer mid, String key); + void setMessageProperty(Integer mid, String key, String value); } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index def6324a..3e09d204 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -33,6 +33,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.ConnectionCallback; @@ -1162,21 +1163,57 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } } + @Transactional @Override public boolean updateReplyUri(Message reply, URI replyUri) { return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=0 WHERE message_id=? AND reply_id=?", replyUri.toASCIIString(), reply.getMid(), reply.getRid()) > 0; } + @Transactional(readOnly = true) @Override public boolean replyExists(URI replyUri) { return jdbcTemplate.queryForList("SELECT reply_id FROM replies WHERE reply_uri=?", Integer.class, replyUri.toASCIIString()).size() > 0; } + @Transactional @Override public boolean deleteReply(URI userUri, URI replyUri) { - return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?", + return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?", userUri.toASCIIString(), replyUri.toASCIIString()) > 0; } + + @Transactional(readOnly = true) + @Override + public String getMessageProperty(Integer mid, String key) { + List<String> results = jdbcTemplate.queryForList( + "SELECT property_value FROM messages_properties WHERE message_id=? AND property_key=?", + String.class, mid, key); + if (results.size() > 0) { + return results.get(0); + } + return StringUtils.EMPTY; + } + + @Transactional + @Override + public void setMessageProperty(Integer mid, String key, String value) { + SqlParameterSource parameterSource = new MapSqlParameterSource() + .addValue("mid", mid) + .addValue("key", key) + .addValue("value", value); + if (StringUtils.isNotEmpty(value)) { + try { + getNamedParameterJdbcTemplate().update("INSERT INTO messages_properties(message_id, property_key, property_value) " + + "VALUES(:mid, :key, :value)", parameterSource); + } catch (DataIntegrityViolationException ex) { + getNamedParameterJdbcTemplate().update("UPDATE messages_properties SET property_value=:value " + + "WHERE message_id=:mid AND property_key=:key", parameterSource); + } + } else { + getNamedParameterJdbcTemplate().update("DELETE FROM messages_properties " + + "WHERE message_id=:mid AND property_key=:key", parameterSource); + } + } } |