-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR] 데이터베이스 스키마 변경에 따른 초기화 로직 추가 (#161)
- Loading branch information
Showing
7 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
eeos/src/main/java/com/blackcompany/eeos/program/application/util/ProgramInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.blackcompany.eeos.program.application.util; | ||
|
||
import com.blackcompany.eeos.program.application.model.ProgramAttendMode; | ||
import com.blackcompany.eeos.program.persistence.ProgramEntity; | ||
import com.blackcompany.eeos.program.persistence.ProgramRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ProgramInitializer implements ApplicationRunner { | ||
|
||
private final ProgramRepository programRepository; | ||
private final String defaultUrl = "https://github.com/JNU-econovation/weekly_presentation/tree/2024-1/2024-1/A_team/1st"; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
System.out.println("------------Program Initializer----------"); | ||
List<ProgramEntity> programs = getPrograms(); | ||
|
||
if(programs.isEmpty()) return; | ||
|
||
programs = programs.stream() | ||
.map(program -> { | ||
if(program.getGithubUrl()==null){ | ||
return program.toBuilder().githubUrl(defaultUrl).build(); | ||
} | ||
return program; | ||
}) | ||
.map(program -> { | ||
if(program.getAttendMode()==null){ | ||
return program.toBuilder().attendMode(ProgramAttendMode.END).build(); | ||
} | ||
|
||
return program; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
programRepository.saveAll(programs); | ||
System.out.println("----------------------------------------------"); | ||
} | ||
|
||
private List<ProgramEntity> getPrograms(){ | ||
return programRepository.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../src/main/java/com/blackcompany/eeos/target/application/util/PresentationInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.blackcompany.eeos.target.application.util; | ||
|
||
import com.blackcompany.eeos.program.persistence.ProgramEntity; | ||
import com.blackcompany.eeos.program.persistence.ProgramRepository; | ||
import com.blackcompany.eeos.target.persistence.PresentationEntity; | ||
import com.blackcompany.eeos.target.persistence.PresentationRepository; | ||
import com.blackcompany.eeos.team.persistence.TeamEntity; | ||
import com.blackcompany.eeos.team.persistence.TeamRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
// 기존에 저장된 Program과 Team을 매핑하는 구조 | ||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PresentationInitializer implements ApplicationRunner { | ||
|
||
private final PresentationRepository presentationRepository; | ||
private final ProgramRepository programRepository; | ||
private final TeamRepository teamRepository; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
System.out.println("------------Presentation Initializer----------"); | ||
Set<Long> programs = getPrograms().stream().map(ProgramEntity::getId).collect(Collectors.toSet()); | ||
|
||
if(programs.isEmpty()) return; | ||
|
||
Set<Long> presentations = getPresentations().stream().map(PresentationEntity::getProgramId).collect(Collectors.toSet()); | ||
|
||
Set<Long> target = programs.stream().filter(programId -> !presentations.contains(programId)).collect(Collectors.toSet()); | ||
|
||
Long defaultTeamId = teamRepository.findById(0L).orElseThrow().getId(); | ||
|
||
Set<PresentationEntity> entities = target.stream().map(targetId -> PresentationEntity.builder().teamId(defaultTeamId).programId(targetId).build()).collect(Collectors.toSet()); | ||
|
||
presentationRepository.saveAll(entities); | ||
System.out.println("----------------------------------------------"); | ||
} | ||
|
||
private List<ProgramEntity> getPrograms(){ | ||
return programRepository.findAll(); | ||
} | ||
|
||
private List<PresentationEntity> getPresentations(){ | ||
return presentationRepository.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
eeos/src/main/java/com/blackcompany/eeos/team/application/util/TeamInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.blackcompany.eeos.team.application.util; | ||
|
||
import com.blackcompany.eeos.team.persistence.TeamEntity; | ||
import com.blackcompany.eeos.team.persistence.TeamRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
// 기존 데이터베이스와의 데이터 정합성을 위한 클래스 | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Component | ||
public class TeamInitializer implements ApplicationRunner { | ||
|
||
private final TeamRepository teamRepository; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
System.out.println("-----------Team Initializer-----------"); | ||
if(isEmptyTable()){ | ||
TeamEntity defaultTeam = TeamEntity.builder().id(0L).name("임시 활동 팀").status(false).build(); | ||
teamRepository.save(defaultTeam); | ||
log.info("임시 팀이 생성되었습니다."); | ||
} | ||
else log.info("임시 팀이 생성되지 않았습니다."); | ||
System.out.println("---------------------------------------"); | ||
|
||
} | ||
|
||
private boolean isEmptyTable(){ | ||
return teamRepository.findAllTeams().isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters