Skip to content

Commit 7c93044

Browse files
committed
refactor : 코드리뷰 수정
1 parent b344297 commit 7c93044

22 files changed

+110
-120
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sprint.mission.discodeit.common.error;
22

3-
public enum ErrorMessage {
3+
public enum ErrorCode {
44

55
/**
66
* 1000 ~ 1999 User 관련 에러
@@ -9,7 +9,7 @@ public enum ErrorMessage {
99

1010
USER_NAME_NULL(1_001, "유저의 이름은 반드시 존재해야합니다."),
1111

12-
NAME_LENGTH_ERROR_MESSAGE(1_002, "유저 이름의 길이 제한을 확인해주세요."),
12+
USER_NAME_LENGTH_OUT_OF_RANGE(1_002, "유저 이름의 길이 제한을 확인해주세요."),
1313

1414
USER_NOT_PARTICIPATED_CHANNEL(1_003, "유저가 참여하지 않은 방입니다."),
1515

@@ -21,16 +21,20 @@ public enum ErrorMessage {
2121
;
2222

2323
private final int errorCode;
24-
private final String message;
24+
private final String errorMessage;
2525

2626

27-
ErrorMessage(int errorCode, String message) {
27+
ErrorCode(int errorCode, String message) {
2828
this.errorCode = errorCode;
29-
this.message = message;
29+
this.errorMessage = message;
3030
}
3131

32-
public String getMessage() {
33-
return message;
32+
public int getErrorCode() {
33+
return errorCode;
34+
}
35+
36+
public String getErrorMessage() {
37+
return errorMessage;
3438
}
3539

3640
}

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/common/error/channel/ChannelException.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sprint.mission.discodeit.common.error.channel;
22

3-
import com.sprint.mission.discodeit.common.error.ErrorMessage;
3+
import com.sprint.mission.discodeit.common.error.ErrorCode;
44
import java.util.UUID;
55

66
public class ChannelException extends RuntimeException {
@@ -13,28 +13,28 @@ private ChannelException(String message, Throwable cause) {
1313
super(message, cause);
1414
}
1515

16-
public static ChannelException of(ErrorMessage message) {
17-
return new ChannelException(message.getMessage());
16+
public static ChannelException of(ErrorCode message) {
17+
return new ChannelException(message.getErrorMessage());
1818
}
1919

20-
public static ChannelException ofErrorMessageAndNotExistChannelId(ErrorMessage message, UUID causeInputParameter) {
20+
public static ChannelException ofNotFound(ErrorCode message, UUID causeInputParameter) {
2121
var format =
2222
String.format(
2323
"%s : input Channel Id = %s",
24-
message.getMessage(),
24+
message.getErrorMessage(),
2525
causeInputParameter.toString()
2626
);
2727

2828
return new ChannelException(format);
2929
}
3030

31-
public static ChannelException ofErrorMessageAndCreatorName(
32-
ErrorMessage message,
31+
public static ChannelException ofNotCreatorName(
32+
ErrorCode message,
3333
String creatorName
3434
) {
3535
var format = String.format(
3636
"%s : 채널 생성자 %s 가 아닙니다.",
37-
message.getMessage(), creatorName
37+
message.getErrorMessage(), creatorName
3838
);
3939

4040
return new ChannelException(format);

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/common/error/user/UserException.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sprint.mission.discodeit.common.error.user;
22

3-
import com.sprint.mission.discodeit.common.error.ErrorMessage;
3+
import com.sprint.mission.discodeit.common.error.ErrorCode;
44

55
public class UserException extends RuntimeException {
66

@@ -12,33 +12,21 @@ public UserException(String message, Throwable cause) {
1212
super(message, cause);
1313
}
1414

15-
public static UserException of(ErrorMessage message) {
16-
return new UserException(message.getMessage());
15+
public static UserException of(ErrorCode message) {
16+
return new UserException(message.getErrorMessage());
1717
}
1818

1919
public static UserException of(String message, Throwable cause) {
2020
return new UserException(message, cause);
2121
}
2222

2323

24-
public static UserException ofErrorMessageAndId(
25-
ErrorMessage message, String id
24+
public static UserException ofNotJoinChannel(
25+
ErrorCode message, String id
2626
) {
2727
var format = String.format(
2828
"%s : not participated channel id %s",
29-
message.getMessage(), id
30-
);
31-
32-
return new UserException(format);
33-
}
34-
35-
36-
public static UserException ofErrorMessageAndChannelName(
37-
ErrorMessage message, String channelName
38-
) {
39-
var format = String.format(
40-
"%s : not participated channel name %s",
41-
message.getMessage(), channelName
29+
message.getErrorMessage(), id
4230
);
4331

4432
return new UserException(format);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package com.sprint.mission.discodeit.common.valid;
1+
package com.sprint.mission.discodeit.common.validation;
22

33
import jakarta.validation.ConstraintViolation;
44
import jakarta.validation.Validation;
5+
import jakarta.validation.ValidationException;
56
import jakarta.validation.Validator;
67
import jakarta.validation.ValidatorFactory;
78
import java.util.Set;
9+
import java.util.StringJoiner;
10+
import java.util.stream.Collectors;
811

912
public class ValidationUtils {
1013

@@ -14,12 +17,9 @@ public class ValidationUtils {
1417

1518
public static <T> void validate(T object) {
1619
Set<ConstraintViolation<T>> violations = validator.validate(object);
17-
if (!violations.isEmpty()) {
18-
StringBuilder errorMessage = new StringBuilder();
19-
for (ConstraintViolation<T> violation : violations) {
20-
errorMessage.append(violation.getMessage()).append("\n");
21-
}
22-
throw new IllegalArgumentException(errorMessage.toString());
23-
}
20+
String errorMessage = violations.stream()
21+
.map(ConstraintViolation::getMessage)
22+
.collect(Collectors.joining("\n"));
23+
throw new ValidationException(String.valueOf(errorMessage));
2424
}
2525
}

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/entity/channel/Channel.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.sprint.mission.discodeit.entity.channel;
22

33
import com.google.common.base.Preconditions;
4-
import com.sprint.mission.discodeit.common.error.ErrorMessage;
4+
import com.sprint.mission.discodeit.common.error.ErrorCode;
55
import com.sprint.mission.discodeit.common.error.channel.ChannelException;
66
import com.sprint.mission.discodeit.entity.common.AbstractUUIDEntity;
77
import com.sprint.mission.discodeit.entity.user.entity.User;
@@ -50,43 +50,43 @@ public static Channel createDefaultNameAndUser(User user) {
5050
}
5151

5252
public void changeName(String newName, User user) {
53-
checkCreatorEqualsOrThrow(user);
53+
checkCreatorOrThrow(user);
5454

5555
channelName = newName;
56-
updateStatusAndUpdateAt();
56+
updateModified();
5757
}
5858

5959
public void deleteChannel(User user) {
60-
checkCreatorEqualsOrThrow(user);
60+
checkCreatorOrThrow(user);
6161

6262
updateUnregistered();
6363
}
6464

65-
public boolean isStatusNotUnregisteredAndEqualsTo(String channelName) {
66-
return isNotUnregistered() && this.channelName.equals(channelName);
65+
public boolean isRegisteredAndNameEqual(String channelName) {
66+
return isRegistered() && this.channelName.equals(channelName);
6767
}
6868

6969
public String getChannelName() {
7070
return channelName;
7171
}
7272

73-
private void checkCreatorEqualsOrThrow(User user) {
74-
var isNotCreator = isNotCreator(user);
73+
private void checkCreatorOrThrow(User user) {
74+
var isCreator = isCreator(user);
7575

76-
if (isNotCreator) {
77-
throw ChannelException.ofErrorMessageAndCreatorName(
78-
ErrorMessage.CHANNEL_NOT_EQUAL_CREATOR,
76+
if (!isCreator) {
77+
throw ChannelException.ofNotCreatorName(
78+
ErrorCode.CHANNEL_NOT_EQUAL_CREATOR,
7979
user.getName()
8080
);
8181
}
8282
}
8383

84-
private boolean isNotCreator(User user) {
84+
private boolean isCreator(User user) {
8585
Preconditions.checkNotNull(user);
86-
return !creator.equals(user);
86+
return creator.equals(user);
8787
}
8888

89-
public String getCreator() {
89+
public String getCreatorName() {
9090
return creator.getName();
9191
}
9292

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/entity/common/AbstractUUIDEntity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ private void updateStatus(Status status) {
5252
this.updateAt = createUnixTimestamp();
5353
}
5454

55-
public void updateStatusAndUpdateAt() {
55+
public void updateModified() {
5656
updateStatus(MODIFIED);
5757
}
5858

5959
public void updateUnregistered() {
6060
updateStatus(UNREGISTERED);
6161
}
6262

63-
public boolean isNotUnregistered() {
63+
public boolean isRegistered() {
6464
return status != UNREGISTERED;
6565
}
6666

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/entity/common/Status.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public enum Status {
77
UNREGISTERED("해지")
88
;
99

10-
private final String status;
10+
private final String description;
1111

1212
Status(String status) {
13-
this.status = status;
13+
this.description = status;
1414
}
1515

16-
public String getStatus() {
17-
return status;
16+
public String getDescription() {
17+
return description;
1818
}
1919
}

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/entity/user/entity/ParticipatedChannel.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sprint.mission.discodeit.entity.user.entity;
22

3-
import static com.sprint.mission.discodeit.common.error.ErrorMessage.USER_NOT_PARTICIPATED_CHANNEL;
3+
import static com.sprint.mission.discodeit.common.error.ErrorCode.USER_NOT_PARTICIPATED_CHANNEL;
44

55
import com.sprint.mission.discodeit.common.error.channel.ChannelException;
66
import com.sprint.mission.discodeit.common.error.user.UserException;
@@ -41,7 +41,7 @@ public List<Channel> findAllChannels() {
4141
var participatedChannels =
4242
this.participatedChannels.values()
4343
.stream()
44-
.filter(Channel::isNotUnregistered)
44+
.filter(Channel::isRegistered)
4545
.toList();
4646

4747
return Collections.unmodifiableList(participatedChannels);
@@ -52,14 +52,15 @@ private Optional<Channel> findByChannelId(UUID channelId) {
5252
return Optional.ofNullable(foundParticipatedChannel);
5353
}
5454

55-
public Optional<Channel> findByChannelIdNotUnregisteredOrThrow(UUID channelId) {
55+
public Optional<Channel> getUnregisteredChannelById(UUID channelId) {
5656
var foundChannel =
5757
findByChannelId(channelId)
5858
.orElseThrow(
59-
() -> ChannelException.ofErrorMessageAndNotExistChannelId(USER_NOT_PARTICIPATED_CHANNEL, channelId));
59+
() -> ChannelException.ofNotFound(USER_NOT_PARTICIPATED_CHANNEL, channelId));
6060

6161
var unregisteredReturnNullOrFoundChannel =
62-
foundChannel.getStatus() == Status.UNREGISTERED ? null : foundChannel;
62+
foundChannel.isRegistered() ? foundChannel : null;
63+
6364

6465
return Optional.ofNullable(unregisteredReturnNullOrFoundChannel);
6566
}
@@ -68,17 +69,17 @@ public Optional<Channel> findByChannelIdNotUnregisteredOrThrow(UUID channelId) {
6869
public Optional<Channel> findByName(String name) {
6970
var foundChannelByName = participatedChannels.values()
7071
.stream()
71-
.filter(channel -> channel.isStatusNotUnregisteredAndEqualsTo(name))
72+
.filter(channel -> channel.isRegisteredAndNameEqual(name))
7273
.findFirst();
7374

7475
return foundChannelByName;
7576
}
7677

7778
public Channel changeChannelNameOrThrow(UUID channelId, String newName, User user) {
7879
var foundChannel =
79-
findByChannelIdNotUnregisteredOrThrow(channelId)
80+
getUnregisteredChannelById(channelId)
8081
.orElseThrow(
81-
() -> UserException.ofErrorMessageAndId(USER_NOT_PARTICIPATED_CHANNEL,
82+
() -> UserException.ofNotJoinChannel(USER_NOT_PARTICIPATED_CHANNEL,
8283
channelId.toString()));
8384

8485
foundChannel.changeName(newName, user);

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/entity/user/entity/User.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void changeUserName(String newName) {
2929
Preconditions.checkNotNull(newName);
3030
var changedName = this.name.changeName(newName);
3131
this.name = changedName;
32-
updateStatusAndUpdateAt();
32+
updateModified();
3333
}
3434

3535
/**

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/repository/common/InMemoryCrudRepository.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sprint.mission.discodeit.repository.common;
22

33
import com.sprint.mission.discodeit.entity.common.AbstractUUIDEntity;
4+
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.HashMap;
67
import java.util.List;
@@ -32,12 +33,8 @@ public List<T> findAll() {
3233
if (store.isEmpty()) {
3334
return Collections.emptyList();
3435
}
35-
36-
var existEntities = store.values()
37-
.stream()
38-
.toList();
3936

40-
return Collections.unmodifiableList(existEntities);
37+
return List.copyOf(store.values());
4138
}
4239

4340
@Override

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/service/basic/BasicChannelMessageService.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sprint.mission.discodeit.service.basic;
22

3-
import com.sprint.mission.discodeit.common.error.ErrorMessage;
3+
import com.sprint.mission.discodeit.common.error.ErrorCode;
44
import com.sprint.mission.discodeit.common.error.channel.ChannelException;
55
import com.sprint.mission.discodeit.common.error.user.UserException;
66
import com.sprint.mission.discodeit.entity.channel.Channel;
@@ -73,9 +73,9 @@ public ChannelMessageInfoResponse sendMessage(SendChannelMessageRequest sendChan
7373
*/
7474
private User findUserByIdOrThrow(UUID userId) {
7575
var foundUser = userRepository.findById(userId)
76-
.filter(User::isNotUnregistered)
77-
.orElseThrow(() -> UserException.ofErrorMessageAndId(
78-
ErrorMessage.USER_NOT_FOUND, userId.toString()
76+
.filter(User::isRegistered)
77+
.orElseThrow(() -> UserException.ofNotJoinChannel(
78+
ErrorCode.USER_NOT_FOUND, userId.toString()
7979
));
8080

8181
return foundUser;
@@ -84,8 +84,8 @@ private User findUserByIdOrThrow(UUID userId) {
8484

8585
private Channel findChannelByIdOrThrow(UUID channelId) {
8686
var foundChannel = channelRepository.findById(channelId)
87-
.orElseThrow(() -> ChannelException.ofErrorMessageAndNotExistChannelId(
88-
ErrorMessage.CHANNEL_NOT_FOUND,
87+
.orElseThrow(() -> ChannelException.ofNotFound(
88+
ErrorCode.CHANNEL_NOT_FOUND,
8989
channelId
9090
));
9191
return foundChannel;

0 commit comments

Comments
 (0)