From e6dffc95c6867e542edacbdb7f00948e776ecc81 Mon Sep 17 00:00:00 2001 From: nayonsoso Date: Sun, 3 Aug 2025 05:36:36 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B1=84=EB=84=90=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20=EC=8B=9C=20=EC=9C=A0=EB=8B=88=ED=81=AC=20=EC=A0=9C=EC=95=BD?= =?UTF-8?q?=20=EC=9C=84=EB=B0=98=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8F=84?= =?UTF-8?q?=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mentor/domain/Channel.java | 6 ++++ .../solidconnection/mentor/domain/Mentor.java | 21 ++++++++---- .../service/MentorMyPageServiceTest.java | 34 +++++++++++++++---- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/solidconnection/mentor/domain/Channel.java b/src/main/java/com/example/solidconnection/mentor/domain/Channel.java index b3c12bff7..10ed3e940 100644 --- a/src/main/java/com/example/solidconnection/mentor/domain/Channel.java +++ b/src/main/java/com/example/solidconnection/mentor/domain/Channel.java @@ -54,4 +54,10 @@ public Channel(int sequence, ChannelType type, String url) { public void updateMentor(Mentor mentor) { this.mentor = mentor; } + + public void update(Channel channel) { + this.sequence = channel.sequence; + this.type = channel.type; + this.url = channel.url; + } } diff --git a/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java b/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java index d97f6a895..008917e89 100644 --- a/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java +++ b/src/main/java/com/example/solidconnection/mentor/domain/Mentor.java @@ -65,13 +65,20 @@ public void updatePassTip(String passTip) { } public void updateChannels(List channels) { - this.channels.clear(); - if (channels == null || channels.isEmpty()) { - return; - } - for (Channel channel : channels) { - channel.updateMentor(this); - this.channels.add(channel); + int newChannelSize = Math.max(channels.size(), this.channels.size()); + int originalChannelSize = this.channels.size(); + for (int i = 0; i < newChannelSize; i++) { + if (i < channels.size() && i < this.channels.size()) { // 기존 채널 수정 + Channel existing = this.channels.get(i); + Channel newChannel = channels.get(i); + existing.update(newChannel); + } else if (i < channels.size()) { // 채널 갯수 늘어남 - 새로운 채널 추가 + Channel newChannel = channels.get(i); + newChannel.updateMentor(this); + this.channels.add(newChannel); + } else if (i < originalChannelSize) { // 채널 갯수 줄어듦 - 기존 채널 삭제 + this.channels.remove(this.channels.size() - 1); + } } } } diff --git a/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java b/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java index 5d675e14b..f4bc6a0ea 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java @@ -3,6 +3,7 @@ import static com.example.solidconnection.mentor.domain.ChannelType.BLOG; import static com.example.solidconnection.mentor.domain.ChannelType.INSTAGRAM; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.junit.jupiter.api.Assertions.assertAll; import com.example.solidconnection.mentor.domain.Channel; @@ -109,8 +110,28 @@ class 멘토의_마이_페이지를_수정한다 { } @Test - void 채널_정보를_수정한다() { + void 기존보다_적게_채널_정보를_수정한다() { // given + channelFixture.채널(1, mentor); + channelFixture.채널(2, mentor); + channelFixture.채널(3, mentor); + channelFixture.채널(4, mentor); + List newChannels = List.of(new ChannelRequest(BLOG, "https://blog.com")); + MentorMyPageUpdateRequest request = new MentorMyPageUpdateRequest("introduction", "passTip", newChannels); + + // when + mentorMyPageService.updateMentorMyPage(mentorUser.getId(), request); + + // then + List updatedChannels = channelRepositoryForTest.findAllByMentorId(mentor.getId()); + assertThat(updatedChannels).extracting(Channel::getSequence, Channel::getType, Channel::getUrl) + .containsExactlyInAnyOrder(tuple(1, BLOG, "https://blog.com")); + } + + @Test + void 기존보다_많게_채널_정보를_수정한다() { + // given + channelFixture.채널(1, mentor); List newChannels = List.of( new ChannelRequest(BLOG, "https://blog.com"), new ChannelRequest(INSTAGRAM, "https://instagram.com") @@ -122,12 +143,11 @@ class 멘토의_마이_페이지를_수정한다 { // then List updatedChannels = channelRepositoryForTest.findAllByMentorId(mentor.getId()); - assertAll( - () -> assertThat(updatedChannels).extracting(Channel::getType) - .containsExactly(BLOG, INSTAGRAM), - () -> assertThat(updatedChannels).extracting(Channel::getUrl) - .containsExactly("https://blog.com", "https://instagram.com") - ); + assertThat(updatedChannels).extracting(Channel::getSequence, Channel::getType, Channel::getUrl) + .containsExactlyInAnyOrder( + tuple(1, BLOG, "https://blog.com"), + tuple(2, INSTAGRAM, "https://instagram.com") + ); } } }