Skip to content

Commit 80785ba

Browse files
authored
Merge pull request #43 from new-writon/42-에러-코드-정리
[Refactor] 에러 코드 정리
2 parents ee0adc1 + 29eefec commit 80785ba

15 files changed

Lines changed: 97 additions & 59 deletions

src/main/java/com/writon/admin/domain/controller/ParticipationController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public SuccessDto<List<String>> participate(
6363

6464
return new SuccessDto<>(sendedEmailList);
6565
} else {
66-
throw new CustomException(ErrorCode.EMAIL_DUPLICATION);
66+
throw new CustomException(ErrorCode.EMAIL_DUPLICATE);
6767
}
6868
}
6969

src/main/java/com/writon/admin/domain/repository/activity/UserTemplateRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface UserTemplateRepository extends JpaRepository<UserTemplate, Long> {
99

10-
Optional<List<UserTemplate>> findByUserChallengeId(Long userChallengeId);
10+
List<UserTemplate> findByUserChallengeId(Long userChallengeId);
1111
int countByUserChallengeId(Long userChallengeId);
1212

1313
}

src/main/java/com/writon/admin/domain/repository/challenge/ChallengeDayRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface ChallengeDayRepository extends JpaRepository<ChallengeDay, Long> {
99

10-
Optional<List<ChallengeDay>> findByChallengeId(Long challengeId);
10+
List<ChallengeDay> findByChallengeId(Long challengeId);
1111

1212
}

src/main/java/com/writon/admin/domain/repository/challenge/ChallengeRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface ChallengeRepository extends JpaRepository<Challenge, Long> {
99

10-
Optional<List<Challenge>> findByOrganizationId(Long organizationId);
10+
List<Challenge> findByOrganizationId(Long organizationId);
1111

1212
}

src/main/java/com/writon/admin/domain/repository/challenge/EmailRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface EmailRepository extends JpaRepository<Email, Long> {
99

10-
Optional<List<Email>> findByChallengeId(Long challengeId);
10+
List<Email> findByChallengeId(Long challengeId);
1111

1212
}

src/main/java/com/writon/admin/domain/repository/organization/PositionRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface PositionRepository extends JpaRepository<Position, Long> {
99

10-
Optional<List<Position>> findByOrganizationId(Long organizationId);
10+
List<Position> findByOrganizationId(Long organizationId);
1111

1212
}

src/main/java/com/writon/admin/domain/repository/question/QuestionRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public interface QuestionRepository extends JpaRepository<Question, Long> {
1010

11-
Optional<List<Question>> findByChallengeId(Long challengeId);
11+
List<Question> findByChallengeId(Long challengeId);
1212

1313
int countByKeyword(Keyword keyword);
1414

src/main/java/com/writon/admin/domain/repository/user/UserChallengeRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface UserChallengeRepository extends JpaRepository<UserChallenge, Long> {
99

10-
Optional<List<UserChallenge>> findByChallengeId(Long challengeId);
11-
Optional<List<UserChallenge>> findByAffiliationId(Long affiliationId);
10+
List<UserChallenge> findByChallengeId(Long challengeId);
11+
List<UserChallenge> findByAffiliationId(Long affiliationId);
1212

1313
}

src/main/java/com/writon/admin/domain/service/AuthService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class AuthService {
4848
// ========== SignUp API ==========
4949
public SignUpResponseDto signup(SignUpRequestDto signUpRequestDto) {
5050
if (adminUserRepository.existsByIdentifier(signUpRequestDto.getIdentifier())) {
51-
throw new CustomException(ErrorCode.ETC_ERROR);
51+
throw new CustomException(ErrorCode.USER_IDENTIFIER_DUPLICATE);
5252
}
5353

5454
AdminUser encodedAdminUser = signUpRequestDto.toAdminUser(passwordEncoder);
@@ -89,14 +89,13 @@ public LoginResponseWrapper login(LoginRequestDto loginRequestDto) {
8989

9090
// 5. 해당 Organization 정보 가져오기
9191
AdminUser adminUser = adminUserRepository.findByIdentifier(identifier)
92-
.orElseThrow(() -> new UsernameNotFoundException(" -> 데이터베이스에서 찾을 수 없습니다."));
92+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
9393
Optional<Organization> organization = organizationRepository.findByAdminUserId(adminUser.getId());
9494

9595
// 7. 챌린지 정보 가져오기
9696
List<Challenge> challenges = Collections.emptyList();
9797
if (organization.isPresent()) {
98-
challenges = challengeRepository.findByOrganizationId(organization.get().getId())
99-
.orElse(Collections.emptyList()); // 데이터가 없을 때 빈 리스트 반환
98+
challenges = challengeRepository.findByOrganizationId(organization.get().getId()); // 데이터가 없을 때 빈 리스트 반환
10099
}
101100
List<ChallengeResponse> challengeList = challenges.stream()
102101
.map(entity -> new ChallengeResponse(entity.getId(), entity.getName()))

src/main/java/com/writon/admin/domain/service/ChallengeService.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ChallengeService {
5656
private final UserTemplateRepository userTemplateRepository;
5757
private final EmailService emailService;
5858

59-
// ========== Create API ==========
59+
// ========== CreateChallenge API ==========
6060
public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requestDto) {
6161
// 1. 조직정보 추출
6262
Organization organization = tokenUtil.getOrganization();
@@ -98,8 +98,11 @@ public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requ
9898
}
9999

100100
// 6. Response 생성
101-
List<Challenge> challenges = challengeRepository.findByOrganizationId(organization.getId())
102-
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));
101+
List<Challenge> challenges = challengeRepository.findByOrganizationId(organization.getId());
102+
if (challenges.isEmpty()) {
103+
throw new CustomException(ErrorCode.CHALLENGE_NOT_FOUND);
104+
}
105+
103106
List<ChallengeResponse> challengeList = challenges.stream()
104107
.map(entity -> new ChallengeResponse(entity.getId(), entity.getName()))
105108
.collect(Collectors.toList());
@@ -110,25 +113,29 @@ public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requ
110113
// ========== Get Dashboard API ==========
111114
public List<UserStatus> getDashboard(Long challengeId) {
112115
// 1. 챌린지 날짜 리스트 추출
113-
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId)
114-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
116+
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId);
117+
118+
if (challengeDayList.isEmpty()) {
119+
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
120+
}
115121

116122
challengeDayList = challengeDayList.stream()
117123
.sorted(Comparator.comparing(ChallengeDay::getDay))
118124
.toList();
119125

120126
// 2. 챌린지 참여 유저 리스트 추출
121-
List<UserChallenge> userChallengeList = userChallengeRepository.findByChallengeId(challengeId)
122-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
127+
List<UserChallenge> userChallengeList = userChallengeRepository.findByChallengeId(challengeId);
123128

124129
// 3. 유저별 참여여부 확인
125130
List<UserStatus> statusTable = new ArrayList<>();
126131

127132
for (UserChallenge userChallenge : userChallengeList) {
128133
List<Status> statusList = new ArrayList<>();
129134
List<UserTemplate> userTemplateList = userTemplateRepository.findByUserChallengeId(
130-
userChallenge.getId())
131-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
135+
userChallenge.getId());
136+
if (userTemplateList.isEmpty()) {
137+
throw new CustomException(ErrorCode.USER_TEMPLATE_NOT_FOUND);
138+
}
132139

133140
for (ChallengeDay challengeDay : challengeDayList) {
134141
// 참여여부 확인과정
@@ -149,8 +156,10 @@ public List<UserStatus> getDashboard(Long challengeId) {
149156

150157
// ========== Get Questions API ==========
151158
public QuestionsResponseDto getQuestions(Long challengeId) {
152-
List<Question> questionList = questionRepository.findByChallengeId(challengeId)
153-
.orElseThrow(() -> new CustomException((ErrorCode.ETC_ERROR)));
159+
List<Question> questionList = questionRepository.findByChallengeId(challengeId);
160+
if (questionList.isEmpty()) {
161+
throw new CustomException(ErrorCode.QUESTION_NOT_FOUND);
162+
}
154163

155164
List<String> basicQuestions = questionList.stream()
156165
.filter(question -> question.getKeyword() == null)
@@ -179,29 +188,34 @@ public QuestionsResponseDto getQuestions(Long challengeId) {
179188
public ChallengeInfoResponseDto getInfo(Long challengeId) {
180189
// 1. 챌린지 기본 정보 가져오기
181190
Challenge challenge = challengeRepository.findById(challengeId)
182-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
191+
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));
183192

184193
// 2. 챌린지 날짜 정보 가져오기
185-
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId)
186-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
194+
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId);
195+
196+
if (challengeDayList.isEmpty()) {
197+
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
198+
}
187199

188200
return new ChallengeInfoResponseDto(
189201
challenge.getName(),
190202
challenge.getStartAt(),
191203
challenge.getFinishAt(),
192-
challengeDays.stream().map(ChallengeDay::getDay).collect(Collectors.toList())
204+
challengeDayList.stream().map(ChallengeDay::getDay).collect(Collectors.toList())
193205
);
194206
}
195207

196208
// ========== Put Questions API ==========
197209
public QuestionsResponseDto putQuestions(Long challengeId, QuestionsRequestDto requestDto) {
198210
// 1. 챌린지 조회
199211
Challenge challenge = challengeRepository.findById(challengeId)
200-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
212+
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));
201213

202214
// 2. 질문 리스트 조회
203-
List<Question> questionList = questionRepository.findByChallengeId(challengeId)
204-
.orElseThrow(() -> new CustomException((ErrorCode.ETC_ERROR)));
215+
List<Question> questionList = questionRepository.findByChallengeId(challengeId);
216+
if (questionList.isEmpty()) {
217+
throw new CustomException(ErrorCode.QUESTION_NOT_FOUND);
218+
}
205219

206220
// 3. 베이직 질문 처리
207221
List<String> requestBasicQuestions = requestDto.getBasicQuestions();
@@ -310,7 +324,7 @@ public QuestionsResponseDto putQuestions(Long challengeId, QuestionsRequestDto r
310324
public ChallengeInfoResponseDto putInfo(Long challengeId, ChallengeInfoRequestDto requestDto) {
311325
// 1. 챌린지 기본 정보 조회
312326
Challenge challenge = challengeRepository.findById(challengeId)
313-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
327+
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));
314328

315329
// 2. 챌린지 기본 정보 수정 및 저장
316330
challenge.setName(requestDto.getName());
@@ -319,8 +333,11 @@ public ChallengeInfoResponseDto putInfo(Long challengeId, ChallengeInfoRequestDt
319333
Challenge editedChallenge = challengeRepository.save(challenge);
320334

321335
// 3. 챌린지 날짜 정보 조회
322-
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId)
323-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
336+
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId);
337+
338+
if (challengeDays.isEmpty()) {
339+
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
340+
}
324341

325342
// 4. 챌린지 날짜 정보 수정 및 저장
326343
// 1) 새로운 날짜가 기존 리스트에 없으면 추가
@@ -345,8 +362,11 @@ public ChallengeInfoResponseDto putInfo(Long challengeId, ChallengeInfoRequestDt
345362
}
346363

347364
// 5. 변경된 날짜 정보 조회
348-
List<ChallengeDay> editedChallengeDays = challengeDayRepository.findByChallengeId(challengeId)
349-
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
365+
List<ChallengeDay> editedChallengeDays = challengeDayRepository.findByChallengeId(challengeId);
366+
367+
if (editedChallengeDays.isEmpty()) {
368+
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
369+
}
350370

351371
return new ChallengeInfoResponseDto(
352372
editedChallenge.getName(),

0 commit comments

Comments
 (0)