Skip to content

Commit

Permalink
feat: unfollow user API impl
Browse files Browse the repository at this point in the history
  • Loading branch information
zoooo-hs committed May 10, 2022
1 parent c939c72 commit 08b6287
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public ProfileDto getProfile(@PathVariable("username") String name, @Authenticat
public ProfileDto followUser(@PathVariable("username") String name, @AuthenticationPrincipal UserDto.Auth authUser) {
return profileService.followUser(name, authUser);
}

@DeleteMapping("/{username}/follow")
public ProfileDto unfollowUser(@PathVariable("username") String name, @AuthenticationPrincipal UserDto.Auth authUser) {
return profileService.unfollowUser(name, authUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface ProfileService {
ProfileDto getProfile(final String username, final UserDto.Auth authUser);

ProfileDto followUser(final String name, final UserDto.Auth authUser);

ProfileDto unfollowUser(final String name, final UserDto.Auth authUser);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ public ProfileDto getProfile(String name, UserDto.Auth authUser) {
UserEntity user = userRepository.findByName(name).orElseThrow(() -> new AppException(Error.USER_NOT_FOUND));
Boolean following = followRepository.findByFolloweeIdAndFollowerId(user.getId(), authUser.getId()).isPresent();

return ProfileDto.builder()
.name(user.getName())
.bio(user.getBio())
.image(user.getImage())
.following(following)
.build();
return convertToProfile(user, following);
}

@Transactional
Expand All @@ -43,11 +38,28 @@ public ProfileDto followUser(String name, UserDto.Auth authUser) {
FollowEntity follow = FollowEntity.builder().followee(followee).follower(follower).build();
followRepository.save(follow);

return convertToProfile(followee, true);
}

@Transactional
@Override
public ProfileDto unfollowUser(String name, UserDto.Auth authUser) {
UserEntity followee = userRepository.findByName(name).orElseThrow(() -> new AppException(Error.USER_NOT_FOUND));
UserEntity follower = UserEntity.builder().id(authUser.getId()).build(); // myself

FollowEntity follow = followRepository.findByFolloweeIdAndFollowerId(followee.getId(), follower.getId())
.orElseThrow(() -> new AppException(Error.FOLLOW_NOT_FOUND));
followRepository.delete(follow);

return convertToProfile(followee, false);
}

private ProfileDto convertToProfile(UserEntity user, Boolean following) {
return ProfileDto.builder()
.name(followee.getName())
.bio(followee.getBio())
.image(followee.getImage())
.following(true)
.name(user.getName())
.bio(user.getBio())
.image(user.getImage())
.following(following)
.build();
}
}
1 change: 1 addition & 0 deletions src/main/java/io/zoooohs/realworld/exception/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum Error {
ALREADY_FOLLOWED_USER("already followed user", HttpStatus.UNPROCESSABLE_ENTITY),

USER_NOT_FOUND("user not found", HttpStatus.NOT_FOUND),
FOLLOW_NOT_FOUND("such follow not found", HttpStatus.NOT_FOUND),
;

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -72,4 +71,22 @@ void whenFollowValidUsername_thenReturnProfile() throws Exception {
.andExpect(jsonPath("$.profile", Matchers.notNullValue(ProfileDto.class)))
.andExpect(jsonPath("$.profile.following", Matchers.is(true)));
}

@Test
@WithAuthUser
void whenUnFollowFollowedUsername_thenReturnProfile() throws Exception {
ProfileDto profileDto = ProfileDto.builder()
.name("testUser")
.bio("some bio")
.image("profilephoto")
.following(true)
.build();

when(profileService.unfollowUser(eq("testUser"), any(UserDto.Auth.class))).thenReturn(profileDto);

mockMvc.perform(delete("/profiles/testUser/follow"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.profile", Matchers.notNullValue(ProfileDto.class)))
.andExpect(jsonPath("$.profile.following", Matchers.is(true)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.swing.text.html.Option;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -86,4 +85,13 @@ void whenFollowFollowedUsername_thenThrow422() {
}

}

@Test
void whenUnFollowFollowedUsername_thenReturnProfile() {
when(followRepository.findByFolloweeIdAndFollowerId(expectedUser.getId(), authUser.getId())).thenReturn(Optional.of(FollowEntity.builder().build()));

ProfileDto actual = profileService.unfollowUser(expectedUser.getName(), authUser);

assertFalse(actual.getFollowing());
}
}

0 comments on commit 08b6287

Please sign in to comment.