Skip to content

Commit

Permalink
Merge pull request #86 from TaetaetaE01/main
Browse files Browse the repository at this point in the history
affiliation 해당 단과대 학과 조회
  • Loading branch information
TaetaetaE01 authored Aug 14, 2024
2 parents 7c73b96 + 61f1808 commit 9620d7e
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.bigbrotherbe.domain.member.dto.request.MemberInfoChangeRequest;
import com.example.bigbrotherbe.domain.member.dto.request.MemberRequest;
import com.example.bigbrotherbe.domain.member.dto.request.SignUpDto;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationCollegeResponse;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse;
import com.example.bigbrotherbe.domain.member.entity.role.AffiliationListDto;
Expand Down Expand Up @@ -75,7 +75,11 @@ public interface MemberController {

@GetMapping("/colleges")
@Operation(summary = "단과대학 리스트 조회")
ResponseEntity<com.example.bigbrotherbe.global.exception.response.ApiResponse<List<AffiliationCollegeResponse>>> getCollegesList();
ResponseEntity<com.example.bigbrotherbe.global.exception.response.ApiResponse<List<AffiliationResponse>>> getCollegesList();

@GetMapping("/departments")
@Operation(summary = "해당 학과 리스트 조회")
ResponseEntity<com.example.bigbrotherbe.global.exception.response.ApiResponse<List<AffiliationResponse>>> getDepartmentList(@RequestParam(name = "councilName") String councilName);

@GetMapping("/refresh")
@Operation(summary = "refresh 토큰으로 토큰 재 생성 요청")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.bigbrotherbe.domain.member.dto.request.MemberInfoChangeRequest;
import com.example.bigbrotherbe.domain.member.dto.request.MemberRequest;
import com.example.bigbrotherbe.domain.member.dto.request.SignUpDto;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationCollegeResponse;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse;
import com.example.bigbrotherbe.domain.member.entity.role.AffiliationListDto;
Expand Down Expand Up @@ -69,18 +69,22 @@ public ResponseEntity<ApiResponse<EmailVerificationResult>> verificationEmail(St
}



public ResponseEntity<ApiResponse<Void>> changePassword(ChangePasswordRequest changePasswordRequest) {
memberService.changePasswrd(changePasswordRequest.email(), changePasswordRequest.password());
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}

public ResponseEntity<ApiResponse<List<AffiliationCollegeResponse>>> getCollegesList() {
List<AffiliationCollegeResponse> collegesList = memberService.getColleges();
public ResponseEntity<ApiResponse<List<AffiliationResponse>>> getCollegesList() {
List<AffiliationResponse> collegesList = memberService.getColleges();
return ResponseEntity.ok(ApiResponse.success(SUCCESS, collegesList));
}

public ResponseEntity<ApiResponse<TokenDto>> refreshToken(String refreshToken){
public ResponseEntity<ApiResponse<List<AffiliationResponse>>> getDepartmentList(String councilName) {
List<AffiliationResponse> departmentsList = memberService.getDepartments(councilName);
return ResponseEntity.ok(ApiResponse.success(SUCCESS, departmentsList));
}

public ResponseEntity<ApiResponse<TokenDto>> refreshToken(String refreshToken) {
return ResponseEntity.ok(ApiResponse.success(SUCCESS, memberService.refreshToken(refreshToken)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
@Getter
@Builder
@AllArgsConstructor
public class AffiliationCollegeResponse {
public class AffiliationResponse {
private int val;
private String councilName;

public static AffiliationCollegeResponse fromAffiliationCollegeResponse(int val, String councilName) {
return AffiliationCollegeResponse.builder()
public static AffiliationResponse fromAffiliationResponse(int val, String councilName) {
return AffiliationResponse.builder()
.val(val)
.councilName(councilName)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.bigbrotherbe.domain.member.entity.enums;

import com.example.bigbrotherbe.domain.member.dto.response.AffiliationResponse;
import com.example.bigbrotherbe.global.exception.BusinessException;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -8,6 +10,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.example.bigbrotherbe.global.exception.enums.ErrorCode.INVALID_AFFILIATION;

@Getter
@AllArgsConstructor
public enum AffiliationCode {
Expand Down Expand Up @@ -67,9 +71,20 @@ public static AffiliationCode fromCouncilName(String councilName) {
throw new IllegalArgumentException("없는 단체 명입니다." + councilName);
}

public static List<AffiliationCode> getDepartmentsByCollege(AffiliationCode college) {
public static List<AffiliationResponse> getDepartmentsByCollegeName(String collegeName) {
AffiliationCode college = Arrays.stream(AffiliationCode.values())
.filter(ac -> ac.getCouncilName().equals(collegeName))
.findFirst()
.orElseThrow(() -> new BusinessException(INVALID_AFFILIATION));

if (!college.getCouncilType().equals("단과대")) {
throw new BusinessException(INVALID_AFFILIATION);
}

// 단과대에 속한 학과를 필터링하여 반환합니다.
return Arrays.stream(AffiliationCode.values())
.filter(code -> code.getCouncilType().equals("학과") && code.getVal() > college.getVal() && code.getVal() <= getMaxValForCollege(college))
.map(department -> AffiliationResponse.fromAffiliationResponse(department.getVal(), department.getCouncilName()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.bigbrotherbe.domain.member.service;

import com.example.bigbrotherbe.domain.member.dto.request.SignUpDto;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationCollegeResponse;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse;
import com.example.bigbrotherbe.domain.member.entity.role.AffiliationListDto;
Expand Down Expand Up @@ -32,7 +32,9 @@ public interface MemberService {

AffiliationListDto getMemberAffiliationRoleList();

List<AffiliationCollegeResponse> getColleges();
List<AffiliationResponse> getColleges();

List<AffiliationResponse> getDepartments(String councilName);

TokenDto refreshToken(String refreshToken);

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


import com.example.bigbrotherbe.domain.member.dto.request.SignUpDto;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationCollegeResponse;
import com.example.bigbrotherbe.domain.member.dto.response.AffiliationResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberInfoResponse;
import com.example.bigbrotherbe.domain.member.dto.response.MemberResponse;
import com.example.bigbrotherbe.domain.member.entity.enums.AffiliationCode;
Expand All @@ -25,6 +25,7 @@


import com.example.bigbrotherbe.global.jwt.entity.TokenDto;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Duration;
Expand Down Expand Up @@ -75,8 +76,8 @@ public MemberResponse userSignUp(SignUpDto signUpDto) {


// Affiliation 조회
log.info(signUpDto.getCollege() +" " + signUpDto.getAffiliation());
Affiliation college = affiliationRepository.findByName(signUpDto.getCollege())
log.info(signUpDto.getCollege() + " " + signUpDto.getAffiliation());
Affiliation college = affiliationRepository.findByName(signUpDto.getCollege())
.orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_AFFILIATION));

// AffiliationMember 엔티티 생성
Expand All @@ -88,13 +89,13 @@ public MemberResponse userSignUp(SignUpDto signUpDto) {
affiliationMemberRepository.save(memberCollage);

Affiliation affiliation = affiliationRepository.findByName(signUpDto.getAffiliation())
.orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_AFFILIATION));
.orElseThrow(() -> new BusinessException(ErrorCode.NO_EXIST_AFFILIATION));

AffiliationMember memberAffiliation = AffiliationMember.builder()
.member(savedMember)
.affiliation(affiliation)
.role("ROLE_USER")
.build();
.member(savedMember)
.affiliation(affiliation)
.role("ROLE_USER")
.build();

affiliationMemberRepository.save(memberAffiliation);

Expand Down Expand Up @@ -190,7 +191,7 @@ public boolean checkExistAffiliationById(Long affiliationId) {

@Override
@Transactional
public void changePasswrd(String email,String password) {
public void changePasswrd(String email, String password) {
Member member = memberLoader.findByMemberEmail(email);
member.changePassword(passwordEncoder.encode(password));
}
Expand All @@ -201,33 +202,40 @@ public void makeAffiliation() {
affiliationRepository.save(Affiliation.builder().affiliation_id(1L).name("총학").build());
}

public List<AffiliationCollegeResponse> getColleges() {
@Override
public List<AffiliationResponse> getColleges() {
return Arrays.stream(AffiliationCode.values())
.filter(code -> code.getCouncilType().equals("단과대"))
.map(code -> AffiliationCollegeResponse.fromAffiliationCollegeResponse(code.getVal(), code.getCouncilName()))
.map(code -> AffiliationResponse.fromAffiliationResponse(code.getVal(), code.getCouncilName()))
.collect(Collectors.toList());
}

@Override
public List<AffiliationResponse> getDepartments(String councilName) {
return AffiliationCode.getDepartmentsByCollegeName(councilName);
}


@Override
public TokenDto refreshToken(String refreshToken) {
System.out.println(refreshToken);
String resolveToken = resolveToken(refreshToken);

if (jwtTokenProvider.validateToken(resolveToken)) {
// 리프레시 토큰이 유효한 경우 새로운 엑세스 토큰 발급
String newAccessToken = jwtTokenProvider.createTokenByRefreshToken(resolveToken);
// 리프레시 토큰이 유효한 경우 새로운 엑세스 토큰 발급
String newAccessToken = jwtTokenProvider.createTokenByRefreshToken(resolveToken);

// 새 엑세스 토큰으로 인증 설정
Authentication authentication = jwtTokenProvider.getAuthentication(newAccessToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
// 새 엑세스 토큰으로 인증 설정
Authentication authentication = jwtTokenProvider.getAuthentication(newAccessToken);
SecurityContextHolder.getContext().setAuthentication(authentication);

return TokenDto
return TokenDto
.builder()
.accessToken(newAccessToken)
.refreshToken(resolveToken)
.build();
} else
throw new BusinessException(ErrorCode.REFRESH_Token_Expired);
} else
throw new BusinessException(ErrorCode.REFRESH_Token_Expired);
}

@Override
Expand All @@ -242,18 +250,15 @@ public MemberInfoResponse changeMemberInfo(String username) {
Member member = authUtil.getLoginMember();
member.changeName(username);
return MemberInfoResponse
.builder()
.email(member.getEmail())
.memberName(member.getUsername())
.createAt(member.getCreateAt())
.updateAt(member.getUpdateAt())
.affiliationListDto(getMemberAffiliationRoleList())
.build();
.builder()
.email(member.getEmail())
.memberName(member.getUsername())
.createAt(member.getCreateAt())
.updateAt(member.getUpdateAt())
.affiliationListDto(getMemberAffiliationRoleList())
.build();
}

public List<AffiliationCode> getDepartmentsByFaculty(AffiliationCode faculty) {
return AffiliationCode.getDepartmentsByCollege(faculty);
}

@Override
public AffiliationListDto getMemberAffiliationRoleList() {
Expand Down Expand Up @@ -293,7 +298,7 @@ private String resolveToken(String token) {
if (StringUtils.hasText(token) && token.startsWith("Bearer ")) {
return token.substring(7);
}
throw new BusinessException(ErrorCode.ILIEGAL_HEADER_PATTERN);
throw new BusinessException(ErrorCode.ILLEGAL_HEADER_PATTERN);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum ErrorCode {
NOT_COUNCIL_MEMBER(HttpStatus.BAD_REQUEST, "MEMBER-008", "해당 학생회 멤버가 아닙니다."),
NOT_PRESIDENT_MEMBER(HttpStatus.BAD_REQUEST, "MEMBER-009", "해당 학생회 회장이 아닙니다."),


// TOKEN
ACCESS_Token_Expired(HttpStatus.BAD_REQUEST, "TOKEN_001", "만료된 access 토큰입니다."),
REFRESH_Token_Expired(HttpStatus.BAD_REQUEST, "TOKEN_002", "만료된 refresh 토큰입니다."),
Expand All @@ -41,6 +42,7 @@ public enum ErrorCode {

// AFFILIATION
NO_EXIST_AFFILIATION(HttpStatus.NOT_FOUND, "AFFILIATION-001", "존재하지 않는 학생회 입니다."),
INVALID_AFFILIATION(HttpStatus.BAD_REQUEST, "AFFILIATION-002", "해당 학생회는 단과대가 아닙니다."),

// FILE
FAIL_TO_UPLOAD(HttpStatus.INTERNAL_SERVER_ERROR, "FILE-001", "파일 업로드에 실패하였습니다."),
Expand All @@ -61,7 +63,8 @@ public enum ErrorCode {
MISMATCH_VERIFIED_CODE(HttpStatus.NOT_FOUND, "VERIFICATION-001", "이메일 인증 코드가 일치하지 않습니다."),

// PATTERN
ILIEGAL_HEADER_PATTERN(HttpStatus.BAD_REQUEST, "PATTERN-001", "헤더의 형식이 맞지 않습니다.");
ILLEGAL_HEADER_PATTERN(HttpStatus.BAD_REQUEST, "PATTERN-001", "헤더의 형식이 맞지 않습니다.");

private final HttpStatus httpStatus;
private final String errorCode;
private final String message;
Expand Down
21 changes: 11 additions & 10 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
spring:
# datasource:
# url: ${DB_URL}
# driver-class-name: com.mysql.cj.jdbc.Driver
# username: ${DB_USER_NAME}
# password: ${DB_PASSWORD}

# 태태 로컬 데이터베이스 설정
datasource:
url: ${DB_URL}
url: jdbc:mysql://localhost:3306/bigbrother
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${DB_USER_NAME}
password: ${DB_PASSWORD}
# 환경 변수를 사용하는 데이터베이스 설정 (주석 해제 필요시) # datasource:
# 태태 로컬 데이터베이스 설정
# datasource:
# url: jdbc:mysql://localhost:3306/bigbrother
# driver-class-name: com.mysql.cj.jdbc.Driver
# username: root
# password: 1234
username: root
password: 1234

jpa:
hibernate:
ddl-auto: update
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ spring:
hikari:
max-lifetime: 177000 # 177초, hikari는 RDS wait_timeout(180초)보다 2 ~ 3초 정도 짧게 줄 것을 권장
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
hibernate:
ddl-auto: create-drop
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ server:
spring:
profiles:
active: ${ACTIVE_NAME}

main:
debug: true

Expand Down

0 comments on commit 9620d7e

Please sign in to comment.