Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public SuccessDto<List<String>> participate(

return new SuccessDto<>(sendedEmailList);
} else {
throw new CustomException(ErrorCode.EMAIL_DUPLICATION);
throw new CustomException(ErrorCode.EMAIL_DUPLICATE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface UserTemplateRepository extends JpaRepository<UserTemplate, Long> {

Optional<List<UserTemplate>> findByUserChallengeId(Long userChallengeId);
List<UserTemplate> findByUserChallengeId(Long userChallengeId);
int countByUserChallengeId(Long userChallengeId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface ChallengeDayRepository extends JpaRepository<ChallengeDay, Long> {

Optional<List<ChallengeDay>> findByChallengeId(Long challengeId);
List<ChallengeDay> findByChallengeId(Long challengeId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface ChallengeRepository extends JpaRepository<Challenge, Long> {

Optional<List<Challenge>> findByOrganizationId(Long organizationId);
List<Challenge> findByOrganizationId(Long organizationId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface EmailRepository extends JpaRepository<Email, Long> {

Optional<List<Email>> findByChallengeId(Long challengeId);
List<Email> findByChallengeId(Long challengeId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface PositionRepository extends JpaRepository<Position, Long> {

Optional<List<Position>> findByOrganizationId(Long organizationId);
List<Position> findByOrganizationId(Long organizationId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface QuestionRepository extends JpaRepository<Question, Long> {

Optional<List<Question>> findByChallengeId(Long challengeId);
List<Question> findByChallengeId(Long challengeId);

int countByKeyword(Keyword keyword);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface UserChallengeRepository extends JpaRepository<UserChallenge, Long> {

Optional<List<UserChallenge>> findByChallengeId(Long challengeId);
Optional<List<UserChallenge>> findByAffiliationId(Long affiliationId);
List<UserChallenge> findByChallengeId(Long challengeId);
List<UserChallenge> findByAffiliationId(Long affiliationId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class AuthService {
// ========== SignUp API ==========
public SignUpResponseDto signup(SignUpRequestDto signUpRequestDto) {
if (adminUserRepository.existsByIdentifier(signUpRequestDto.getIdentifier())) {
throw new CustomException(ErrorCode.ETC_ERROR);
throw new CustomException(ErrorCode.USER_IDENTIFIER_DUPLICATE);
}

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

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

// 7. 챌린지 정보 가져오기
List<Challenge> challenges = Collections.emptyList();
if (organization.isPresent()) {
challenges = challengeRepository.findByOrganizationId(organization.get().getId())
.orElse(Collections.emptyList()); // 데이터가 없을 때 빈 리스트 반환
challenges = challengeRepository.findByOrganizationId(organization.get().getId()); // 데이터가 없을 때 빈 리스트 반환
}
List<ChallengeResponse> challengeList = challenges.stream()
.map(entity -> new ChallengeResponse(entity.getId(), entity.getName()))
Expand Down
66 changes: 43 additions & 23 deletions src/main/java/com/writon/admin/domain/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ChallengeService {
private final UserTemplateRepository userTemplateRepository;
private final EmailService emailService;

// ========== Create API ==========
// ========== CreateChallenge API ==========
public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requestDto) {
// 1. 조직정보 추출
Organization organization = tokenUtil.getOrganization();
Expand Down Expand Up @@ -98,8 +98,11 @@ public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requ
}

// 6. Response 생성
List<Challenge> challenges = challengeRepository.findByOrganizationId(organization.getId())
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));
List<Challenge> challenges = challengeRepository.findByOrganizationId(organization.getId());
if (challenges.isEmpty()) {
throw new CustomException(ErrorCode.CHALLENGE_NOT_FOUND);
}

List<ChallengeResponse> challengeList = challenges.stream()
.map(entity -> new ChallengeResponse(entity.getId(), entity.getName()))
.collect(Collectors.toList());
Expand All @@ -110,25 +113,29 @@ public CreateChallengeResponseDto createChallenge(CreateChallengeRequestDto requ
// ========== Get Dashboard API ==========
public List<UserStatus> getDashboard(Long challengeId) {
// 1. 챌린지 날짜 리스트 추출
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId);

if (challengeDayList.isEmpty()) {
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
}

challengeDayList = challengeDayList.stream()
.sorted(Comparator.comparing(ChallengeDay::getDay))
.toList();

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

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

for (UserChallenge userChallenge : userChallengeList) {
List<Status> statusList = new ArrayList<>();
List<UserTemplate> userTemplateList = userTemplateRepository.findByUserChallengeId(
userChallenge.getId())
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
userChallenge.getId());
if (userTemplateList.isEmpty()) {
throw new CustomException(ErrorCode.USER_TEMPLATE_NOT_FOUND);
}

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

// ========== Get Questions API ==========
public QuestionsResponseDto getQuestions(Long challengeId) {
List<Question> questionList = questionRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException((ErrorCode.ETC_ERROR)));
List<Question> questionList = questionRepository.findByChallengeId(challengeId);
if (questionList.isEmpty()) {
throw new CustomException(ErrorCode.QUESTION_NOT_FOUND);
}

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

// 2. 챌린지 날짜 정보 가져오기
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<ChallengeDay> challengeDayList = challengeDayRepository.findByChallengeId(challengeId);

if (challengeDayList.isEmpty()) {
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
}

return new ChallengeInfoResponseDto(
challenge.getName(),
challenge.getStartAt(),
challenge.getFinishAt(),
challengeDays.stream().map(ChallengeDay::getDay).collect(Collectors.toList())
challengeDayList.stream().map(ChallengeDay::getDay).collect(Collectors.toList())
);
}

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

// 2. 질문 리스트 조회
List<Question> questionList = questionRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException((ErrorCode.ETC_ERROR)));
List<Question> questionList = questionRepository.findByChallengeId(challengeId);
if (questionList.isEmpty()) {
throw new CustomException(ErrorCode.QUESTION_NOT_FOUND);
}

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

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

// 3. 챌린지 날짜 정보 조회
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<ChallengeDay> challengeDays = challengeDayRepository.findByChallengeId(challengeId);

if (challengeDays.isEmpty()) {
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
}

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

// 5. 변경된 날짜 정보 조회
List<ChallengeDay> editedChallengeDays = challengeDayRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<ChallengeDay> editedChallengeDays = challengeDayRepository.findByChallengeId(challengeId);

if (editedChallengeDays.isEmpty()) {
throw new CustomException(ErrorCode.CHALLENGE_DAY_NOT_FOUND);
}

return new ChallengeInfoResponseDto(
editedChallenge.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void sendEmail(Challenge challenge, String email) {
log.info("Succeeded to send Email");
} catch (Exception e) {
log.info("Failed to send Email");
throw new CustomException(ErrorCode.ETC_ERROR);
throw new CustomException(ErrorCode.EMAIL_SEND_FAILED);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
public class OrganizationService {

private final OrganizationRepository organizationRepository;
private final AdminUserRepository adminUserRepository;
private final TokenUtil tokenUtil;
private final PositionRepository positionRepository;

Expand All @@ -34,6 +33,12 @@ public CreateOrganizationResponseDto createOrganization(
// 1. 사용자 정보 불러오기
AdminUser adminUser = tokenUtil.getAdminUser();

// 1. 조직 생성 여부 확인하기
organizationRepository.findByAdminUserId(adminUser.getId())
.ifPresent(org -> {
throw new CustomException(ErrorCode.ORGANIZATION_DUPLICATE);
});

// 2. Organization 객체 생성
Organization organization = new Organization(
createOrganizationRequestDto.getName(),
Expand Down Expand Up @@ -63,10 +68,12 @@ public CreateOrganizationResponseDto createOrganization(
public List<String> getPositions() {
Organization organization = tokenUtil.getOrganization();

List<Position> responseDto = positionRepository.findByOrganizationId(organization.getId())
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<Position> positionList = positionRepository.findByOrganizationId(organization.getId());
if (positionList.isEmpty()) {
throw new CustomException(ErrorCode.POSITION_NOT_FOUND);
}

return responseDto.stream()
return positionList.stream()
.map(Position::getName)
.toList();
}
Expand Down Expand Up @@ -97,8 +104,10 @@ public List<String> editPositions(List<String> positionList) {
Organization organization = tokenUtil.getOrganization();

// 1. 현재 조직의 모든 Position을 조회
List<Position> existingPositions = positionRepository.findByOrganizationId(organization.getId())
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<Position> existingPositions = positionRepository.findByOrganizationId(organization.getId());
if (positionList.isEmpty()) {
throw new CustomException(ErrorCode.POSITION_NOT_FOUND);
}

// 2. 기존의 position names 리스트를 생성
List<String> existingPositionNames = existingPositions.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public class ParticipationService {
// ========== Get Email API ==========
public List<String> getEmailList(Long challengeId) {

List<Email> emailList = emailRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<Email> emailList = emailRepository.findByChallengeId(challengeId);

return emailList.stream().map(Email::getEmail).toList();
}
Expand All @@ -49,11 +48,10 @@ public List<String> getEmailList(Long challengeId) {
public List<ParticipationInfo> getParticipationInfo(Long challengeId) {
// 1. 챌린지 조회
Challenge challenge = challengeRepository.findById(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));

// 2. 유저 챌린지 조회
List<UserChallenge> userChallengeList = userChallengeRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<UserChallenge> userChallengeList = userChallengeRepository.findByChallengeId(challengeId);

// 3. 유저 정보 조회
List<ParticipationInfo> participationInfoList = new ArrayList<>();
Expand All @@ -62,8 +60,8 @@ public List<ParticipationInfo> getParticipationInfo(Long challengeId) {
Affiliation affiliation = userChallenge.getAffiliation();
User user = affiliation.getUser();

List<UserChallenge> userChallenges = userChallengeRepository.findByAffiliationId(affiliation.getId())
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<UserChallenge> userChallenges = userChallengeRepository.findByAffiliationId(affiliation.getId());

String challenges = userChallenges.stream()
.map(entity -> entity.getChallenge().getName())
.collect(Collectors.joining(", "));
Expand Down Expand Up @@ -104,7 +102,7 @@ public List<ParticipationInfo> withdrawal(Long challengeId, List<Long> userChall

for (Long userChallengeId : userChallengeIdList) {
UserChallenge userChallenge = userChallengeRepository.findById(userChallengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_USER_NOT_FOUND));

userChallenge.setWithdrawn(true);
userChallengeRepository.save(userChallenge);
Expand All @@ -116,15 +114,17 @@ public List<ParticipationInfo> withdrawal(Long challengeId, List<Long> userChall
// ========== Post Participate API ==========
public List<String> participate(Long challengeId, List<String> emailList) {
Challenge challenge = challengeRepository.findById(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));

for (String email : emailList) {
emailService.sendEmail(challenge, email);
emailRepository.save(new Email(email, challenge));
}

List<Email> sendedEmailList = emailRepository.findByChallengeId(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.ETC_ERROR));
List<Email> sendedEmailList = emailRepository.findByChallengeId(challengeId);
if (sendedEmailList.isEmpty()) {
throw new CustomException(ErrorCode.EMAIL_NOT_FOUND);
}

return sendedEmailList.stream().map(Email::getEmail).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void commence(
if (exception.equals(ErrorCode.UNAUTHORIZED_TOKEN.getCode())) {
errorCode = ErrorCode.UNAUTHORIZED_TOKEN;
}

}

exceptionResponseHandler.setResponse(response, errorCode);
Expand Down
Loading