Skip to content

Commit

Permalink
Merge pull request #73 from team-crews/fix/#54-fix-minor-bugs
Browse files Browse the repository at this point in the history
토큰 발급하는 api 응답값 통일, dto 질문 응답 정렬
  • Loading branch information
jongmee authored Sep 7, 2024
2 parents 2f1b9ed + da4a8c1 commit 7fc6959
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.server.crews.applicant.dto.request.EvaluationRequest;
import com.server.crews.applicant.dto.response.ApplicationDetailsResponse;
import com.server.crews.applicant.dto.response.ApplicationsResponse;
import com.server.crews.applicant.mapper.ApplicationMapper;
import com.server.crews.applicant.util.ApplicationMapper;
import com.server.crews.applicant.repository.ApplicationRepository;
import com.server.crews.applicant.repository.NarrativeAnswerRepository;
import com.server.crews.applicant.repository.SelectiveAnswerRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.crews.applicant.mapper;
package com.server.crews.applicant.util;

import com.server.crews.applicant.domain.NarrativeAnswer;
import com.server.crews.applicant.domain.SelectiveAnswer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.crews.applicant.mapper;
package com.server.crews.applicant.util;

import com.server.crews.applicant.domain.Application;
import com.server.crews.applicant.domain.NarrativeAnswer;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/server/crews/auth/application/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import com.server.crews.auth.dto.LoginUser;
import com.server.crews.auth.dto.request.AdminLoginRequest;
import com.server.crews.auth.dto.request.ApplicantLoginRequest;
import com.server.crews.auth.dto.response.AdminLoginResponse;
import com.server.crews.auth.dto.response.ApplicantLoginResponse;
import com.server.crews.auth.dto.response.TokenResponse;
import com.server.crews.auth.repository.AdministratorRepository;
import com.server.crews.auth.repository.ApplicantRepository;
import com.server.crews.global.exception.CrewsException;
Expand All @@ -26,7 +25,7 @@ public class AuthService {
private final PasswordEncoder passwordEncoder;

@Transactional
public AdminLoginResponse loginForAdmin(AdminLoginRequest request) {
public TokenResponse loginForAdmin(AdminLoginRequest request) {
String clubName = request.clubName();
String password = request.password();

Expand All @@ -38,7 +37,7 @@ public AdminLoginResponse loginForAdmin(AdminLoginRequest request) {
.orElseGet(() -> createAdmin(clubName, password));

String accessToken = jwtTokenProvider.createAccessToken(Role.ADMIN, clubName);
return new AdminLoginResponse(administrator.getClubName(), accessToken);
return new TokenResponse(administrator.getClubName(), accessToken);
}

private Administrator createAdmin(String clubName, String password) {
Expand All @@ -48,7 +47,7 @@ private Administrator createAdmin(String clubName, String password) {
}

@Transactional
public ApplicantLoginResponse loginForApplicant(ApplicantLoginRequest request) {
public TokenResponse loginForApplicant(ApplicantLoginRequest request) {
String email = request.email();
String password = request.password();

Expand All @@ -60,7 +59,7 @@ public ApplicantLoginResponse loginForApplicant(ApplicantLoginRequest request) {
.orElseGet(() -> createApplicant(email, password));

String accessToken = jwtTokenProvider.createAccessToken(Role.APPLICANT, email);
return new ApplicantLoginResponse(applicant.getEmail(), accessToken);
return new TokenResponse(applicant.getEmail(), accessToken);
}

private Applicant createApplicant(String email, String password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.server.crews.auth.domain.Applicant;
import com.server.crews.auth.domain.RefreshToken;
import com.server.crews.auth.domain.Role;
import com.server.crews.auth.dto.response.TokenRefreshResponse;
import com.server.crews.auth.dto.response.TokenResponse;
import com.server.crews.auth.repository.AdministratorRepository;
import com.server.crews.auth.repository.ApplicantRepository;
import com.server.crews.auth.repository.RefreshTokenRepository;
Expand All @@ -30,7 +30,7 @@ public RefreshToken createRefreshToken(Role role, String username) {
return refreshTokenRepository.save(new RefreshToken(username, refreshToken));
}

public TokenRefreshResponse renew(String refreshToken) {
public TokenResponse renew(String refreshToken) {
jwtTokenProvider.validateRefreshToken(refreshToken);
String username = jwtTokenProvider.getPayload(refreshToken);
RefreshToken savedRefreshToken = refreshTokenRepository.findByUsername(username)
Expand All @@ -41,7 +41,7 @@ public TokenRefreshResponse renew(String refreshToken) {

Role role = jwtTokenProvider.getRole(refreshToken);
String accessToken = jwtTokenProvider.createAccessToken(role, username);
return new TokenRefreshResponse(accessToken);
return new TokenResponse(username, accessToken);
}

public void delete(Long userId, Role role) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.server.crews.auth.dto.response;

public record TokenResponse(String username, String accessToken) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import com.server.crews.auth.dto.LoginUser;
import com.server.crews.auth.dto.request.AdminLoginRequest;
import com.server.crews.auth.dto.request.ApplicantLoginRequest;
import com.server.crews.auth.dto.response.AdminLoginResponse;
import com.server.crews.auth.dto.response.ApplicantLoginResponse;
import com.server.crews.auth.dto.response.TokenRefreshResponse;
import com.server.crews.auth.dto.response.TokenResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand All @@ -34,33 +32,33 @@ public class AuthController {
* [동아리 관리자] 로그인 해 토큰을 발급 받는다. 모집 공고가 존재하지 않는다면 모집 공고를 새로 생성한다.
*/
@PostMapping("/admin/login")
public ResponseEntity<AdminLoginResponse> loginForAdmin(@RequestBody AdminLoginRequest request) {
AdminLoginResponse loginResponse = authService.loginForAdmin(request);
RefreshToken refreshToken = refreshTokenService.createRefreshToken(Role.ADMIN, loginResponse.username());
public ResponseEntity<TokenResponse> loginForAdmin(@RequestBody AdminLoginRequest request) {
TokenResponse tokenResponse = authService.loginForAdmin(request);
RefreshToken refreshToken = refreshTokenService.createRefreshToken(Role.ADMIN, tokenResponse.username());
ResponseCookie cookie = refreshTokenCookieGenerator.generateWithDefaultValidity(refreshToken.getToken());
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.body(loginResponse);
.body(tokenResponse);
}

/**
* [지원자] 로그인 해 토큰을 발급 받는다.
*/
@PostMapping("/applicant/login")
public ResponseEntity<ApplicantLoginResponse> loginForApplicant(@RequestBody ApplicantLoginRequest request) {
ApplicantLoginResponse loginResponse = authService.loginForApplicant(request);
RefreshToken refreshToken = refreshTokenService.createRefreshToken(Role.APPLICANT, loginResponse.username());
public ResponseEntity<TokenResponse> loginForApplicant(@RequestBody ApplicantLoginRequest request) {
TokenResponse tokenResponse = authService.loginForApplicant(request);
RefreshToken refreshToken = refreshTokenService.createRefreshToken(Role.APPLICANT, tokenResponse.username());
ResponseCookie cookie = refreshTokenCookieGenerator.generateWithDefaultValidity(refreshToken.getToken());
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.body(loginResponse);
.body(tokenResponse);
}

/**
* access token을 재발급 받는다.
*/
@PostMapping("/refresh")
public ResponseEntity<TokenRefreshResponse> renew(@CookieValue("refreshToken") String refreshToken) {
public ResponseEntity<TokenResponse> renew(@CookieValue("refreshToken") String refreshToken) {
return ResponseEntity.ok(refreshTokenService.renew(refreshToken));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import com.server.crews.recruitment.dto.response.RecruitmentDetailsResponse;
import com.server.crews.recruitment.dto.response.RecruitmentProgressResponse;
import com.server.crews.recruitment.dto.response.RecruitmentStateInProgressResponse;
import com.server.crews.recruitment.mapper.RecruitmentMapper;
import com.server.crews.recruitment.repository.NarrativeQuestionRepository;
import com.server.crews.recruitment.repository.RecruitmentRepository;
import com.server.crews.recruitment.repository.SelectiveQuestionRepository;
import com.server.crews.recruitment.util.QuestionSorter;
import com.server.crews.recruitment.util.RecruitmentMapper;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -62,8 +63,10 @@ public RecruitmentDetailsResponse saveRecruitment(Long publisherId, RecruitmentS
Recruitment recruitment = RecruitmentMapper.recruitmentSaveRequestToRecruitment(request, code, publisher);
validateDeadline(recruitment.getDeadline());
Recruitment savedRecruitment = recruitmentRepository.save(recruitment);
savedRecruitment.sortQuestions();
return RecruitmentMapper.recruitmentToRecruitmentDetailsResponse(savedRecruitment);
RecruitmentDetailsResponse recruitmentDetailsResponse = RecruitmentMapper.recruitmentToRecruitmentDetailsResponse(
savedRecruitment);
QuestionSorter.sort(recruitmentDetailsResponse);
return recruitmentDetailsResponse;
}

private void validateDeadline(LocalDateTime deadline) {
Expand Down Expand Up @@ -121,8 +124,10 @@ public RecruitmentDetailsResponse toRecruitmentDetailsWithQuestions(Recruitment
List<SelectiveQuestion> selectives = selectiveQuestionsBySection.getOrDefault(section, List.of());
section.replaceQuestions(narratives, selectives);
});
recruitment.sortQuestions();
return RecruitmentMapper.recruitmentToRecruitmentDetailsResponse(recruitment);
RecruitmentDetailsResponse recruitmentDetailsResponse = RecruitmentMapper.recruitmentToRecruitmentDetailsResponse(
recruitment);
QuestionSorter.sort(recruitmentDetailsResponse);
return recruitmentDetailsResponse;
}

public RecruitmentProgressResponse findRecruitmentProgress(Long publisherId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ public boolean hasOnOrAfterDeadline(LocalDateTime other) {
return other.isAfter(deadline) || other.equals(deadline);
}

public void sortQuestions() {
this.sections.forEach(Section::sortQuestions);
}

public boolean isPublishedBy(Long publisherId) {
return this.publisher.getId().equals(publisherId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import jakarta.persistence.Table;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -67,9 +66,4 @@ public void replaceQuestions(List<NarrativeQuestion> narrativeQuestions,
public void updateRecruitment(Recruitment recruitment) {
this.recruitment = recruitment;
}

public void sortQuestions() {
this.narrativeQuestions.sort(Comparator.comparingInt(NarrativeQuestion::getOrder));
this.selectiveQuestions.sort(Comparator.comparingInt(SelectiveQuestion::getOrder));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.crews.recruitment.mapper;
package com.server.crews.recruitment.util;

import com.server.crews.recruitment.domain.Choice;
import com.server.crews.recruitment.domain.NarrativeQuestion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.server.crews.recruitment.util;

import com.server.crews.recruitment.dto.response.QuestionResponse;
import com.server.crews.recruitment.dto.response.RecruitmentDetailsResponse;
import com.server.crews.recruitment.dto.response.SectionResponse;
import java.util.Comparator;

public class QuestionSorter {

public static void sort(RecruitmentDetailsResponse recruitmentDetailsResponse) {
recruitmentDetailsResponse.sections().forEach(QuestionSorter::sortQuestions);
}

private static void sortQuestions(SectionResponse sectionResponse) {
sectionResponse.questions().sort(Comparator.comparing(QuestionResponse::order));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.crews.recruitment.mapper;
package com.server.crews.recruitment.util;

import com.server.crews.auth.domain.Administrator;
import com.server.crews.recruitment.domain.Recruitment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.crews.recruitment.mapper;
package com.server.crews.recruitment.util;

import com.server.crews.recruitment.domain.NarrativeQuestion;
import com.server.crews.recruitment.domain.Section;
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/com/server/crews/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import com.server.crews.applicant.dto.response.ApplicationDetailsResponse;
import com.server.crews.auth.dto.request.AdminLoginRequest;
import com.server.crews.auth.dto.request.ApplicantLoginRequest;
import com.server.crews.auth.dto.response.AdminLoginResponse;
import com.server.crews.auth.dto.response.ApplicantLoginResponse;
import com.server.crews.auth.dto.response.TokenResponse;
import com.server.crews.auth.presentation.AuthorizationExtractor;
import com.server.crews.environ.DatabaseCleaner;
import com.server.crews.external.application.EmailService;
Expand Down Expand Up @@ -75,15 +74,15 @@ protected ApplicationSaveRequest applicationSaveRequest(String recruitmentCode)
answerSaveRequests, recruitmentCode);
}

protected AdminLoginResponse signUpAdmin(String clubName, String password) {
protected TokenResponse signUpAdmin(String clubName, String password) {
ExtractableResponse<Response> response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(new AdminLoginRequest(clubName, password))
.when().post("/auth/admin/login")
.then()
.statusCode(HttpStatus.OK.value())
.extract();
return response.as(AdminLoginResponse.class);
return response.as(TokenResponse.class);
}

protected RecruitmentDetailsResponse createRecruitment(String accessToken) {
Expand All @@ -102,15 +101,15 @@ protected RecruitmentDetailsResponse createRecruitment(String accessToken, Recru
return response.as(RecruitmentDetailsResponse.class);
}

protected ApplicantLoginResponse signUpApplicant(String email, String password) {
protected TokenResponse signUpApplicant(String email, String password) {
ExtractableResponse<Response> response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(new ApplicantLoginRequest(email, password))
.when().post("/auth/applicant/login")
.then()
.statusCode(HttpStatus.OK.value())
.extract();
return response.as(ApplicantLoginResponse.class);
return response.as(TokenResponse.class);
}

protected ApplicationDetailsResponse createTestApplication(String accessToken,
Expand Down
Loading

0 comments on commit 7fc6959

Please sign in to comment.