Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] #20 인플루언서 좋아요와 반영하여 반환하도록 했어요 #72

Merged
merged 14 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[feat] 로그인 시 좋아요한 인플루언서 반영하여 반환 기능
wndlthsk committed Oct 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1252b7069e90630815a416e49b842bfafc233a71
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package team7.inplace.influencer.application;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -28,9 +30,31 @@ public class InfluencerService {

@Transactional(readOnly = true)
public List<InfluencerInfo> getAllInfluencers() {
return influencerRepository.findAll().stream()
.map(InfluencerInfo::from)
List<Influencer> influencers = influencerRepository.findAll();
Long userId = AuthorizationUtil.getUserId();

// 로그인 안된 경우, likes를 모두 false로 설정
if (userId == null) {
return influencers.stream()
.map(influencer -> InfluencerInfo.from(influencer, false))
.toList();
}

// 로그인 된 경우
Set<Long> likedInfluencerIds = favoriteRepository.findByUserId(userId).stream()
.map(FavoriteInfluencer::getInfluencer)
.map(Influencer::getId)
.collect(Collectors.toSet());

List<InfluencerInfo> influencerInfos = influencers.stream()
.map(influencer -> {
boolean isLiked = likedInfluencerIds.contains(influencer.getId());
return InfluencerInfo.from(influencer, isLiked);
})
.sorted((a, b) -> Boolean.compare(b.likes(), a.likes()))
.toList();

return influencerInfos;
}

@Transactional
@@ -43,7 +67,7 @@ public Long createInfluencer(InfluencerCommand command) {
public Long updateInfluencer(Long id, InfluencerCommand command) {
Influencer influencer = influencerRepository.findById(id).orElseThrow();
influencer.update(command.influencerName(), command.influencerImgUrl(),
command.influencerJob());
command.influencerJob());

return influencer.getId();
}
@@ -58,7 +82,7 @@ public void deleteInfluencer(Long id) {
@Transactional
public void likeToInfluencer(InfluencerRequestParam param) {
String username = AuthorizationUtil.getUsername();
if (StringUtils.hasText(username)) {
if (!StringUtils.hasText(username)) {
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY);
}

Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@ public record InfluencerInfo(
boolean likes
) {

public static InfluencerInfo from(Influencer influencer) {
public static InfluencerInfo from(Influencer influencer, boolean isLiked) {
return new InfluencerInfo(
influencer.getId(),
influencer.getName(),
influencer.getImgUrl(),
influencer.getJob(),
false // 좋아요 기능 추가할 때 로직 추가 예정
isLiked
);
}
}
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@

public interface InfluencerControllerApiSpec {

@Operation(summary = "인플루언서들 리스트 반환", description = "토큰 유무에 따라 좋아요한 인플루언서 반영 여부가 다릅니다.")
@Operation(summary = "인플루언서들 리스트 반환", description = "토큰이 있는 경우 좋아요된 인플루언서가 먼저 반환됩니다.")
ResponseEntity<InfluencerListResponse> getAllInfluencers();

@Operation(summary = "인플루언서 등록", description = "새 인플루언서를 등록합니다.")
Original file line number Diff line number Diff line change
@@ -4,38 +4,54 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import team7.inplace.favoriteInfluencer.domain.FavoriteInfluencer;
import team7.inplace.favoriteInfluencer.persistent.FavoriteInfluencerRepository;
import team7.inplace.influencer.application.InfluencerService;
import team7.inplace.influencer.application.dto.InfluencerCommand;
import team7.inplace.influencer.application.dto.InfluencerInfo;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.influencer.persistence.InfluencerRepository;
import team7.inplace.security.util.AuthorizationUtil;
import team7.inplace.user.domain.Role;
import team7.inplace.user.domain.User;
import team7.inplace.user.domain.UserType;

@ExtendWith(MockitoExtension.class)
public class InfluencerServiceTest {

@Mock
private InfluencerRepository influencerRepository;

@Mock
private FavoriteInfluencerRepository favoriteInfluencerRepository;

@InjectMocks
private InfluencerService influencerService;

@Test
public void getAllInfluencersTest() {
public void getAllInfluencersTest_NotLoggedIn() {
MockedStatic<AuthorizationUtil> authorizationUtil = mockStatic(AuthorizationUtil.class);

Influencer influencer1 = new Influencer("influencer1", "imgUrl1", "job1");
Influencer influencer2 = new Influencer("influencer2", "imgUrl2", "job2");

given(influencerRepository.findAll()).willReturn(Arrays.asList(influencer1, influencer2));
given(AuthorizationUtil.getUserId()).willReturn(null);

List<InfluencerInfo> influencerInfoList = influencerService.getAllInfluencers();

@@ -44,8 +60,45 @@ public void getAllInfluencersTest() {
assertThat(influencerInfoList.get(0).likes()).isFalse();
assertThat(influencerInfoList.get(1).influencerName()).isEqualTo("influencer2");
assertThat(influencerInfoList.get(1).likes()).isFalse();

authorizationUtil.close();
}

@Test
public void getAllInfluencersTest_LoggedIn() {
MockedStatic<AuthorizationUtil> authorizationUtil = mockStatic(AuthorizationUtil.class);

Influencer influencer1 = new Influencer(1L, "influencer1", "imgUrl1", "job1");
Influencer influencer2 = new Influencer(2L, "influencer2", "imgUrl2", "job2");
Influencer influencer3 = new Influencer(3L, "influencer3", "imgUrl3", "job3");

Long userId = 1L;
User user = new User("name", "password", "nickname", UserType.KAKAO, Role.USER);

given(influencerRepository.findAll()).willReturn(
Arrays.asList(influencer1, influencer2, influencer3));
given(AuthorizationUtil.getUserId()).willReturn(userId);

// 2, 3번째 인플루언서 좋아요로 설정
given(favoriteInfluencerRepository.findByUserId(userId)).willReturn(Arrays.asList(
new FavoriteInfluencer(user, influencer2),
new FavoriteInfluencer(user, influencer3)
));

List<InfluencerInfo> influencerInfoList = influencerService.getAllInfluencers();

assertThat(influencerInfoList).hasSize(3);
assertThat(influencerInfoList.get(0).influencerName()).isEqualTo("influencer2");
assertThat(influencerInfoList.get(0).likes()).isTrue();
assertThat(influencerInfoList.get(1).influencerName()).isEqualTo("influencer3");
assertThat(influencerInfoList.get(1).likes()).isTrue();
assertThat(influencerInfoList.get(2).influencerName()).isEqualTo("influencer1");
assertThat(influencerInfoList.get(2).likes()).isFalse();

authorizationUtil.close();
}


@Test
public void createInfluencerTest() {
InfluencerCommand command = new InfluencerCommand("name", "imgUrl", "job");
@@ -81,4 +134,25 @@ public void deleteInfluencerTest() {

verify(influencerRepository, times(1)).delete(influencer);
}

@Test
public void test() {
List<Boolean> booleanList = new ArrayList<>();
booleanList.add(false);
booleanList.add(true);
booleanList.add(false);
booleanList.add(true);
booleanList.add(false);

// 정렬 전 리스트 출력
System.out.println("정렬 전: " + booleanList);

// 정렬: true 값이 앞에 오도록
List<Boolean> sortedList = booleanList.stream()
.sorted(Comparator.reverseOrder()) // reverseOrder()를 사용하여 true가 먼저 오게 함
.toList();

// 정렬 후 리스트 출력
System.out.println("정렬 후: " + sortedList);
}
}