From 4b2821008dedf2300d44548f34a53735afc07000 Mon Sep 17 00:00:00 2001 From: Evgeniy Shishkin Date: Tue, 7 Mar 2017 17:12:02 +0300 Subject: get user blog with recommendations --- .../java/com/juick/api/controllers/Messages.java | 23 +++--------------- .../java/com/juick/service/MessagesService.java | 2 ++ .../com/juick/service/MessagesServiceImpl.java | 28 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/juick-api/src/main/java/com/juick/api/controllers/Messages.java b/juick-api/src/main/java/com/juick/api/controllers/Messages.java index 584ff3cf..7348c54a 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Messages.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Messages.java @@ -69,6 +69,7 @@ public class Messages { public ResponseEntity> getMessages( @RequestParam(required = false) String uname, @RequestParam(defaultValue = "0") int before_mid, + @RequestParam(required = false) String withrecommended, @RequestParam(required = false) String popular, @RequestParam(required = false) String media, @RequestParam(required = false) String tag) { @@ -88,6 +89,8 @@ public class Messages { } else { return NOT_FOUND; } + } else if (!StringUtils.isEmpty(withrecommended)) { + mids = messagesService.getUserBlogWithRecommendations(user.getUid(), 0, before_mid); } else { mids = messagesService.getUserBlog(user.getUid(), 0, before_mid); } @@ -131,26 +134,6 @@ public class Messages { return NOT_FOUND; } - @RequestMapping("/messages/recommended") - public ResponseEntity> doGetRecommended( - @RequestParam(defaultValue = "0") int before_mid) { - User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { - return FORBIDDEN; - } - List mids = messagesService.getUserRecommendations(vuid, before_mid); - if (mids != null && !mids.isEmpty()) { - List msgs = messagesService.getMessages(mids); - if (msgs != null && !msgs.isEmpty()) { - return ResponseEntity.ok(msgs); - } else { - return FORBIDDEN; - } - } - return NOT_FOUND; - } - @RequestMapping("/messages/set_privacy") @ResponseBody public ResponseEntity doSetPrivacy( diff --git a/juick-server/src/main/java/com/juick/service/MessagesService.java b/juick-server/src/main/java/com/juick/service/MessagesService.java index cad0f860..ad7774b7 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesService.java +++ b/juick-server/src/main/java/com/juick/service/MessagesService.java @@ -62,6 +62,8 @@ public interface MessagesService { List getUserTag(int uid, int tid, int privacy, int before); + List getUserBlogWithRecommendations(int uid, int privacy, int before); + List getUserRecommendations(int uid, int before); List getUserPhotos(int uid, int privacy, int before); diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java index 495940f6..0a35cee9 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -198,7 +198,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public boolean recommendMessage(final int mid, final int vuid) { boolean res = getJdbcTemplate().update( - "INSERT IGNORE INTO favorites(user_id, message_id) VALUES (?, ?)", + "INSERT IGNORE INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1; if (res) @@ -588,6 +588,32 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Integer.class); } + @Transactional(readOnly = true) + @Override + public List getUserBlogWithRecommendations(final int uid, final int privacy, final int before) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("privacy", privacy) + .addValue("before", before); + + return getNamedParameterJdbcTemplate().queryForList( + "SELECT message_id FROM " + + "(SELECT message_id FROM favorites " + + " WHERE user_id = :uid " + + (before > 0 ? + " AND message_id < :before " : StringUtils.EMPTY) + + " ORDER BY message_id DESC LIMIT 20) as r" + + " UNION ALL " + + "SELECT message_id FROM " + + "(SELECT message_id FROM messages WHERE user_id = :uid" + + (before > 0 ? + " AND message_id < :before" : StringUtils.EMPTY) + + " AND privacy >= :privacy ORDER BY message_id DESC LIMIT 20) as m " + + "ORDER BY message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + } + @Transactional(readOnly = true) @Override public List getUserRecommendations(final int uid, final int before) { -- cgit v1.2.3