Skip to content

Commit 9c7249c

Browse files
authored
fix: 채널 수정 시 유니크 제약 위반하지 않도록 (#426)
1 parent 51ae689 commit 9c7249c

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/main/java/com/example/solidconnection/mentor/domain/Channel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ public Channel(int sequence, ChannelType type, String url) {
5454
public void updateMentor(Mentor mentor) {
5555
this.mentor = mentor;
5656
}
57+
58+
public void update(Channel channel) {
59+
this.sequence = channel.sequence;
60+
this.type = channel.type;
61+
this.url = channel.url;
62+
}
5763
}

src/main/java/com/example/solidconnection/mentor/domain/Mentor.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ public void updatePassTip(String passTip) {
6565
}
6666

6767
public void updateChannels(List<Channel> channels) {
68-
this.channels.clear();
69-
if (channels == null || channels.isEmpty()) {
70-
return;
71-
}
72-
for (Channel channel : channels) {
73-
channel.updateMentor(this);
74-
this.channels.add(channel);
68+
int newChannelSize = Math.max(channels.size(), this.channels.size());
69+
int originalChannelSize = this.channels.size();
70+
for (int i = 0; i < newChannelSize; i++) {
71+
if (i < channels.size() && i < this.channels.size()) { // 기존 채널 수정
72+
Channel existing = this.channels.get(i);
73+
Channel newChannel = channels.get(i);
74+
existing.update(newChannel);
75+
} else if (i < channels.size()) { // 채널 갯수 늘어남 - 새로운 채널 추가
76+
Channel newChannel = channels.get(i);
77+
newChannel.updateMentor(this);
78+
this.channels.add(newChannel);
79+
} else if (i < originalChannelSize) { // 채널 갯수 줄어듦 - 기존 채널 삭제
80+
this.channels.remove(this.channels.size() - 1);
81+
}
7582
}
7683
}
7784
}

src/test/java/com/example/solidconnection/mentor/service/MentorMyPageServiceTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.example.solidconnection.mentor.domain.ChannelType.BLOG;
44
import static com.example.solidconnection.mentor.domain.ChannelType.INSTAGRAM;
55
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.tuple;
67
import static org.junit.jupiter.api.Assertions.assertAll;
78

89
import com.example.solidconnection.mentor.domain.Channel;
@@ -109,8 +110,28 @@ class 멘토의_마이_페이지를_수정한다 {
109110
}
110111

111112
@Test
112-
void 채널_정보를_수정한다() {
113+
void 기존보다_적게_채널_정보를_수정한다() {
113114
// given
115+
channelFixture.채널(1, mentor);
116+
channelFixture.채널(2, mentor);
117+
channelFixture.채널(3, mentor);
118+
channelFixture.채널(4, mentor);
119+
List<ChannelRequest> newChannels = List.of(new ChannelRequest(BLOG, "https://blog.com"));
120+
MentorMyPageUpdateRequest request = new MentorMyPageUpdateRequest("introduction", "passTip", newChannels);
121+
122+
// when
123+
mentorMyPageService.updateMentorMyPage(mentorUser.getId(), request);
124+
125+
// then
126+
List<Channel> updatedChannels = channelRepositoryForTest.findAllByMentorId(mentor.getId());
127+
assertThat(updatedChannels).extracting(Channel::getSequence, Channel::getType, Channel::getUrl)
128+
.containsExactlyInAnyOrder(tuple(1, BLOG, "https://blog.com"));
129+
}
130+
131+
@Test
132+
void 기존보다_많게_채널_정보를_수정한다() {
133+
// given
134+
channelFixture.채널(1, mentor);
114135
List<ChannelRequest> newChannels = List.of(
115136
new ChannelRequest(BLOG, "https://blog.com"),
116137
new ChannelRequest(INSTAGRAM, "https://instagram.com")
@@ -122,12 +143,11 @@ class 멘토의_마이_페이지를_수정한다 {
122143

123144
// then
124145
List<Channel> updatedChannels = channelRepositoryForTest.findAllByMentorId(mentor.getId());
125-
assertAll(
126-
() -> assertThat(updatedChannels).extracting(Channel::getType)
127-
.containsExactly(BLOG, INSTAGRAM),
128-
() -> assertThat(updatedChannels).extracting(Channel::getUrl)
129-
.containsExactly("https://blog.com", "https://instagram.com")
130-
);
146+
assertThat(updatedChannels).extracting(Channel::getSequence, Channel::getType, Channel::getUrl)
147+
.containsExactlyInAnyOrder(
148+
tuple(1, BLOG, "https://blog.com"),
149+
tuple(2, INSTAGRAM, "https://instagram.com")
150+
);
131151
}
132152
}
133153
}

0 commit comments

Comments
 (0)