diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/entity/enums/AffiliationCode.java b/src/main/java/com/example/bigbrotherbe/domain/member/entity/enums/AffiliationCode.java index b3f7f5a..f7ea7ce 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/entity/enums/AffiliationCode.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/entity/enums/AffiliationCode.java @@ -60,12 +60,12 @@ public enum AffiliationCode { private final int val; private final String councilType; - private final String councilName; + private final String name; public static AffiliationCode fromCouncilName(String councilName) { for (AffiliationCode affiliationCodeIns : AffiliationCode.values()) { - if (affiliationCodeIns.councilName.equals(councilName)) { + if (affiliationCodeIns.name.equals(councilName)) { return affiliationCodeIns; } } @@ -74,7 +74,7 @@ public static AffiliationCode fromCouncilName(String councilName) { public static List getDepartmentsByCollegeName(String collegeName) { AffiliationCode college = Arrays.stream(AffiliationCode.values()) - .filter(ac -> ac.getCouncilName().equals(collegeName)) + .filter(ac -> ac.getName().equals(collegeName)) .findFirst() .orElseThrow(() -> new BusinessException(INVALID_AFFILIATION)); @@ -85,7 +85,7 @@ public static List getDepartmentsByCollegeName(String colle // 단과대에 속한 학과를 필터링하여 반환합니다. 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())) + .map(department -> AffiliationResponse.fromAffiliationResponse(department.getVal(), department.getName())) .collect(Collectors.toList()); } @@ -108,6 +108,6 @@ private static int getMaxValForCollege(AffiliationCode college) { @JsonValue public String getAffiliationCode() { - return this.councilName; + return this.name; } } diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/entity/role/Affiliation.java b/src/main/java/com/example/bigbrotherbe/domain/member/entity/role/Affiliation.java index 589928d..191997f 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/entity/role/Affiliation.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/entity/role/Affiliation.java @@ -1,5 +1,6 @@ package com.example.bigbrotherbe.domain.member.entity.role; +import com.example.bigbrotherbe.domain.member.entity.enums.AffiliationCode; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -28,4 +29,8 @@ public class Affiliation { @Column private String presidentName; + + public static Affiliation toEntity(AffiliationCode affiliationCode) { + return Affiliation.builder().affiliation_id((long)affiliationCode.getVal()).councilType(affiliationCode.getCouncilType()).name(affiliationCode.getName()).build(); + } } diff --git a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java index 01c6ac9..bd6cd6f 100644 --- a/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java +++ b/src/main/java/com/example/bigbrotherbe/domain/member/service/MemberServiceImpl.java @@ -207,7 +207,7 @@ public void makeAffiliation() { public List getColleges() { return Arrays.stream(AffiliationCode.values()) .filter(code -> code.getCouncilType().equals("단과대")) - .map(code -> AffiliationResponse.fromAffiliationResponse(code.getVal(), code.getCouncilName())) + .map(code -> AffiliationResponse.fromAffiliationResponse(code.getVal(), code.getName())) .collect(Collectors.toList()); } diff --git a/src/main/java/com/example/bigbrotherbe/global/database/InitialDataLoader.java b/src/main/java/com/example/bigbrotherbe/global/database/InitialDataLoader.java index 905f41f..3ba4d3a 100644 --- a/src/main/java/com/example/bigbrotherbe/global/database/InitialDataLoader.java +++ b/src/main/java/com/example/bigbrotherbe/global/database/InitialDataLoader.java @@ -1,6 +1,7 @@ package com.example.bigbrotherbe.global.database; import com.example.bigbrotherbe.domain.member.entity.Member; +import com.example.bigbrotherbe.domain.member.entity.enums.AffiliationCode; import com.example.bigbrotherbe.domain.member.repository.AffiliationMemberRepository; import com.example.bigbrotherbe.domain.member.repository.AffiliationRepository; import com.example.bigbrotherbe.domain.member.repository.MemberRepository; @@ -20,15 +21,10 @@ public class InitialDataLoader { public CommandLineRunner loadData(MemberRepository memberRepository, AffiliationRepository affiliationRepository, AffiliationMemberRepository affiliationMemberRepository, PasswordEncoder passwordEncoder) { return args -> { - if (affiliationRepository.findByName("응용소프트웨어전공").isEmpty()) { - affiliationRepository.save(Affiliation.builder().affiliation_id(1L).name("응용소프트웨어전공").councilType("학과").build()); - } - - if (affiliationRepository.findByName("디지털콘텐츠디자인학과").isEmpty()) { - affiliationRepository.save(Affiliation.builder().affiliation_id(2L).name("디지털콘텐츠디자인학과").councilType("학과").build()); - } - if (affiliationRepository.findByName("ICT융합대학").isEmpty()) { - affiliationRepository.save(Affiliation.builder().affiliation_id(3L).name("ICT융합대학").councilType("단과대").build()); + for(AffiliationCode affiliationCode : AffiliationCode.values()){ + if(affiliationRepository.findByName(affiliationCode.getName()).isEmpty()){ + affiliationRepository.save(Affiliation.toEntity(affiliationCode)); + } } // Check if an admin user already exists if (memberRepository.findByUsername("admin").isEmpty()) { @@ -38,17 +34,25 @@ public CommandLineRunner loadData(MemberRepository memberRepository, Affiliation admin.setEmail("developthreefeet@gmail.com"); admin.setUsername("admin"); memberRepository.save(admin); - Affiliation affiliation = affiliationRepository.findByName("응용소프트웨어전공") - .orElseThrow(() -> new IllegalArgumentException("잘못된 소속 이름입니다.")); + + Affiliation college = Affiliation.toEntity(AffiliationCode.ICT); // AffiliationMember 엔티티 생성 - AffiliationMember affiliationMember = AffiliationMember.builder() - .member(admin) - .affiliation(affiliation) - .role("ROLE_ADMIN") - .build(); + affiliationMemberRepository.save(AffiliationMember + .builder() + .member(admin) + .affiliation(college) + .role("ROLE_ADMIN") + .build()); + + Affiliation affiliation = Affiliation.toEntity(AffiliationCode.SOFTWARE_APPLICATIONS); + affiliationMemberRepository.save(AffiliationMember + .builder() + .member(admin) + .affiliation(affiliation) + .role("ROLE_ADMIN") + .build()); - affiliationMemberRepository.save(affiliationMember); } }; } diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index ed0c9b9..ac31f4a 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,20 +1,20 @@ spring: -# datasource: -# url: ${DB_URL} -# driver-class-name: com.mysql.cj.jdbc.Driver -# username: ${DB_USER_NAME} -# password: ${DB_PASSWORD} - - # 태태 로컬 데이터베이스 설정 datasource: - url: jdbc:mysql://localhost:3306/bigbrother + url: ${DB_URL} driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: 1234 + username: ${DB_USER_NAME} + password: ${DB_PASSWORD} + + # 태태 로컬 데이터베이스 설정 +# datasource: +# url: jdbc:mysql://localhost:3306/bigbrother +# driver-class-name: com.mysql.cj.jdbc.Driver +# username: root +# password: 1234 jpa: hibernate: - ddl-auto: update + ddl-auto: create-drop properties: hibernate: format_sql: true