diff options
author | Vitaly Takmazov | 2017-08-23 11:59:55 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-08-23 13:36:38 +0300 |
commit | ca2db58145c4663a22f44f5f98a67aa03fbe8d38 (patch) | |
tree | b40c01b92b15c34df57024eb812ca0d37834e62f /juick-server-jdbc/src/main/java/com/juick | |
parent | 3e4807157e3c244820dd4d5149997970530e4fcb (diff) |
server-jdbc: fix favorites count
Diffstat (limited to 'juick-server-jdbc/src/main/java/com/juick')
-rw-r--r-- | juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java | 24 |
1 files changed, 12 insertions, 12 deletions
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()); } |