@RequestMapping(value = VIDEO_LIKEDBY_PATH, method = RequestMethod.GET) public @ResponseBody Set<String> likedbyVideo(@PathVariable(VIDEO_ID) long id) { Video v = videos.findOne(id); if (v == null) { throw new ResourceNotFoundException(); } return v.getLikedUsers(); }
private void setLike(long id, String user, HttpServletResponse res, boolean isLike) { Video v = videos.findOne(id); int code = HttpServletResponse.SC_OK; if (v == null) { code = HttpServletResponse.SC_NOT_FOUND; } else { if (isLike ? v.addLikedUser(user) : v.removeLikedUser(user)) { videos.save(v); } else { code = HttpServletResponse.SC_BAD_REQUEST; } } res.setStatus(code); }
@Test public void testLikeCount() throws Exception { Video v; // Add the video v = readWriteVideoSvcUser1.addVideo(video); long id = v.getId(); // Like the video readWriteVideoSvcUser1.likeVideo(id); // Get the video again v = readWriteVideoSvcUser1.getVideoById(id); // Make sure the like count is 1 assertTrue(v.getLikes() == 1); // Unlike the video readWriteVideoSvcUser1.unlikeVideo(v.getId()); // Get the video again v = readWriteVideoSvcUser1.getVideoById(v.getId()); // Make sure the like count is 0 assertTrue(v.getLikes() == 0); }