Skip to content

Commit 8fd1599

Browse files
committed
refactor: 분기 처리 삼항연산자, if 문 질문
1 parent ad5b767 commit 8fd1599

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/application/service/channel/JCFChannelService.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.sprint.mission.discodeit.domain.channel.enums.ChannelVisibility;
1414
import com.sprint.mission.discodeit.domain.channel.exception.AlreadyJoinUserException;
1515
import com.sprint.mission.discodeit.domain.channel.exception.ChannelNotFoundException;
16+
import com.sprint.mission.discodeit.domain.channel.exception.NotChannelManagerException;
1617
import com.sprint.mission.discodeit.domain.readStatus.ReadStatus;
1718
import com.sprint.mission.discodeit.domain.user.User;
1819
import com.sprint.mission.discodeit.global.error.ErrorCode;
@@ -115,7 +116,7 @@ public void deleteChannel(UUID userId, DeleteChannelRequestDto requestDto) {
115116

116117
private void throwIsNotManager(User foundUser, Channel foundChannel) {
117118
if (!foundChannel.isManager(foundUser)) {
118-
throw new IllegalArgumentException();
119+
throw new NotChannelManagerException(ErrorCode.CHANNEL_ADMIN_REQUIRED, foundUser.getUsernameValue());
119120
}
120121
}
121122

@@ -141,9 +142,17 @@ private void createAndSaveReadStatus(User foundUser, Channel savedChannel) {
141142

142143
private Channel createChannelWithVisibility(UUID userId, CreateChannelRequestDto requestDto) {
143144
User foundUser = userService.findOneByIdOrThrow(userId);
144-
Channel createChannel = requestDto.visibility() == ChannelVisibility.PUBLIC ?
145-
Channel.ofPublicChannel(requestDto.name(), requestDto.channelType(), foundUser) :
146-
Channel.ofPrivateChannel(foundUser, requestDto.channelType());
145+
Channel createChannel = null;
146+
if (requestDto.visibility() == ChannelVisibility.PUBLIC) {
147+
createChannel = Channel.ofPublicChannel(requestDto.name(), requestDto.channelType(), foundUser);
148+
} else if (requestDto.visibility() == ChannelVisibility.PRIVATE) {
149+
createChannel = Channel.ofPrivateChannel(foundUser, requestDto.channelType());
150+
}
151+
// ====> 삼항 연산자와 if 문 분기처리 중 어떤 것을 더 선호해야할까요? 제가 생각하기에는 삼항 연산자가 보기 더 편한것 같습니다.
152+
// 다음 미션에서는 둘 중 하나 지워두겠습니다.
153+
// createChannel = requestDto.visibility() == ChannelVisibility.PUBLIC ?
154+
// Channel.ofPublicChannel(requestDto.name(), requestDto.channelType(), foundUser) :
155+
// Channel.ofPrivateChannel(foundUser, requestDto.channelType());
147156
Channel savedChannel = channelRepository.save(createChannel);
148157
createAndSaveReadStatus(foundUser, savedChannel);
149158
return savedChannel;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.sprint.mission.discodeit.domain.channel.exception;
2+
3+
import com.sprint.mission.discodeit.global.error.ErrorCode;
4+
import com.sprint.mission.discodeit.global.error.exception.InvalidException;
5+
6+
public class NotChannelManagerException extends InvalidException {
7+
8+
public NotChannelManagerException(ErrorCode errorCode, String message) {
9+
super(errorCode, message);
10+
}
11+
}

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/global/error/ErrorCode.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public enum ErrorCode {
3030
// Channel
3131
INVALID_CHANNEL_NAME_NOT_NULL(400, "채널 이름은 필수 입력값 입니다."),
3232
ALREADY_CHANNEL_JOIN_USER(400, "이미 참여한 채널입니다."),
33+
CHANNEL_ADMIN_REQUIRED(400, "채널 삭제가 허용되지 않은 사용자입니다."),
34+
3335
// Message
3436
INVALID_MESSAGE_CONTENT_NOT_NULL(400, "메시지 내용은 필수 입력값 입니다."),
3537
;

0 commit comments

Comments
 (0)