Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
[#35] 팔로우한 모든 계정의 게시물 조회(메인) 기능 구현
Browse files Browse the repository at this point in the history
- 내가 팔로우한 계정의 게시물만 볼 수 있도록 구현
  • Loading branch information
cyj199637 committed Oct 19, 2020
1 parent 7e8d64f commit db6e42d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public ResponseEntity<List<Post>> getUserFeed(@RequestParam(value = "userId") St
return new ResponseEntity<>(posts, HttpStatus.OK);
}

@GetMapping("/my-follows")
@CheckLogin
public ResponseEntity<List<Post>> getPostsOfAllFollows(@CurrentUser User currentUser) {

List<Post> posts = postService.getPostsOfAllFollows(currentUser.getUserId());

return new ResponseEntity<>(posts, HttpStatus.OK);
}

@PatchMapping("/{postId}")
@CheckLogin
public ResponseEntity<Void> updatePost(@PathVariable int postId,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/me/liiot/snsserver/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface PostMapper {
@ClientDatabase(value = ClientDatabases.SLAVE)
List<Post> getPostsByUserId(String userId);

@ClientDatabase(value = ClientDatabases.SLAVE)
List<Post> getPostsOfAllFollows(String userId);

@ClientDatabase(value = ClientDatabases.SLAVE)
boolean isAuthorizedOnPost(@Param("userId") String userId, @Param("postId") int postId);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/liiot/snsserver/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface PostService {

public List<Post> getPostsByUser(String userId);

public List<Post> getPostsOfAllFollows(String userId);

public void updatePost(User user, int postId, String content) throws AccessException;

public void deletePost(User user, int postId) throws AccessException;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/liiot/snsserver/service/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public List<Post> getPostsByUser(String userId) {
return posts;
}

@Override
public List<Post> getPostsOfAllFollows(String userId) {

List<Post> posts = postMapper.getPostsOfAllFollows(userId);

return posts;
}

@Override
@Caching(evict = {@CacheEvict(cacheNames = CacheNames.POST, key = "#postId"),
@CacheEvict(cacheNames = CacheNames.FEED, key = "#user.userId")})
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/mapper/PostMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
ORDER BY p.createTime DESC
</select>

<select id="getPostsOfAllFollows" parameterType="String" resultMap="postsWithImagesMap">
SELECT p.id, p.userId, p.content, p.createTime, i.id, i.postId, i.imageName, i.imagePath, i.seq
FROM post as p
LEFT OUTER JOIN image as i ON i.postId = p.id
WHERE p.userId IN (SELECT followUserId FROM follow WHERE userId = #{userId})
ORDER BY p.createTime DESC
</select>

<select id="isAuthorizedOnPost" resultType="boolean">
SELECT EXISTS
(SELECT id FROM post WHERE id = #{postId} AND userId = #{userId})
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/me/liiot/snsserver/service/PostServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ void getPostsByUserTest() {
verify(postMapper).getPostsByUserId("test2");
}

@DisplayName("메인 / 사용자가 팔로우한 모든 계정의 게시물 조회")
@Test
void getPostsOfAllFollowsTest() {
Post testPost1 = new Post(11, "test3", "content", Date.valueOf("2020-10-19"));
Post testPost2 = new Post(12, "test4", "content", Date.valueOf("2020-10-19"));
List<Post> posts = new ArrayList<>();
posts.add(testPost1);
posts.add(testPost2);

when(postMapper.getPostsOfAllFollows("test1")).thenReturn(posts);

List<Post> result = postService.getPostsOfAllFollows(testUser.getUserId());

assertEquals(result, posts);
verify(postMapper).getPostsOfAllFollows("test1");
}

@DisplayName("자신이 작성한 게시물 수정")
@Test
void updatePostTestWithSuccess() {
Expand Down

0 comments on commit db6e42d

Please sign in to comment.