Skip to content

Commit

Permalink
Merge pull request #95 from 0702Yoon/main
Browse files Browse the repository at this point in the history
feat : 단과대, 학과 정보 데이터 데이터 베이스에 넣기 구현
  • Loading branch information
0702Yoon authored Aug 15, 2024
2 parents 6d8e472 + 1e8b588 commit 49cdc47
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -74,7 +74,7 @@ public static AffiliationCode fromCouncilName(String councilName) {

public static List<AffiliationResponse> 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));

Expand All @@ -85,7 +85,7 @@ public static List<AffiliationResponse> 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());
}

Expand All @@ -108,6 +108,6 @@ private static int getMaxValForCollege(AffiliationCode college) {

@JsonValue
public String getAffiliationCode() {
return this.councilName;
return this.name;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void makeAffiliation() {
public List<AffiliationResponse> 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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()) {
Expand All @@ -38,17 +34,25 @@ public CommandLineRunner loadData(MemberRepository memberRepository, Affiliation
admin.setEmail("[email protected]");
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);
}
};
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 49cdc47

Please sign in to comment.