diff --git a/eeos/src/main/java/com/blackcompany/eeos/program/application/util/ProgramInitializer.java b/eeos/src/main/java/com/blackcompany/eeos/program/application/util/ProgramInitializer.java new file mode 100644 index 00000000..fbdef24b --- /dev/null +++ b/eeos/src/main/java/com/blackcompany/eeos/program/application/util/ProgramInitializer.java @@ -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 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 getPrograms(){ + return programRepository.findAll(); + } +} diff --git a/eeos/src/main/java/com/blackcompany/eeos/target/application/event/EndAttendModeEventListener.java b/eeos/src/main/java/com/blackcompany/eeos/target/application/event/EndAttendModeEventListener.java index f4e81a7b..25e1a340 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/target/application/event/EndAttendModeEventListener.java +++ b/eeos/src/main/java/com/blackcompany/eeos/target/application/event/EndAttendModeEventListener.java @@ -37,7 +37,7 @@ public void handleDeletedProgram(EndAttendModeEvent event) { } if (event.getProgramIds().isEmpty()) { - log.info("Empty Set"); + log.info("종료할 프로그램이 없습니다."); } } } diff --git a/eeos/src/main/java/com/blackcompany/eeos/target/application/util/PresentationInitializer.java b/eeos/src/main/java/com/blackcompany/eeos/target/application/util/PresentationInitializer.java new file mode 100644 index 00000000..c26e34cf --- /dev/null +++ b/eeos/src/main/java/com/blackcompany/eeos/target/application/util/PresentationInitializer.java @@ -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 programs = getPrograms().stream().map(ProgramEntity::getId).collect(Collectors.toSet()); + + if(programs.isEmpty()) return; + + Set presentations = getPresentations().stream().map(PresentationEntity::getProgramId).collect(Collectors.toSet()); + + Set target = programs.stream().filter(programId -> !presentations.contains(programId)).collect(Collectors.toSet()); + + Long defaultTeamId = teamRepository.findById(0L).orElseThrow().getId(); + + Set entities = target.stream().map(targetId -> PresentationEntity.builder().teamId(defaultTeamId).programId(targetId).build()).collect(Collectors.toSet()); + + presentationRepository.saveAll(entities); + System.out.println("----------------------------------------------"); + } + + private List getPrograms(){ + return programRepository.findAll(); + } + + private List getPresentations(){ + return presentationRepository.findAll(); + } +} diff --git a/eeos/src/main/java/com/blackcompany/eeos/team/application/Service/TeamService.java b/eeos/src/main/java/com/blackcompany/eeos/team/application/Service/TeamService.java index d31e16cb..70f65aa3 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/team/application/Service/TeamService.java +++ b/eeos/src/main/java/com/blackcompany/eeos/team/application/Service/TeamService.java @@ -94,7 +94,7 @@ private List findTeamByProgram(Long programId) { } private List findTeams() { - return teamRepository.findAllTeamsByStatus().stream() + return teamRepository.findAllActiveTeams().stream() .map(entityConverter::from) .collect(Collectors.toList()); } diff --git a/eeos/src/main/java/com/blackcompany/eeos/team/application/util/TeamInitializer.java b/eeos/src/main/java/com/blackcompany/eeos/team/application/util/TeamInitializer.java new file mode 100644 index 00000000..b7f88200 --- /dev/null +++ b/eeos/src/main/java/com/blackcompany/eeos/team/application/util/TeamInitializer.java @@ -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(); + } +} diff --git a/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamEntity.java b/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamEntity.java index 33d50ba7..4004494d 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamEntity.java +++ b/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamEntity.java @@ -32,8 +32,7 @@ public class TeamEntity extends BaseEntity { @Column( name = ENTITY_PREFIX + "_status", - nullable = false, - columnDefinition = "boolean default 1") + nullable = false) @Builder.Default private boolean status = true; // 현학기 활동팀 1, 아니면 0 diff --git a/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamRepository.java b/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamRepository.java index 448ff3b7..0d4aba3c 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamRepository.java +++ b/eeos/src/main/java/com/blackcompany/eeos/team/persistence/TeamRepository.java @@ -18,5 +18,9 @@ public interface TeamRepository extends JpaRepository { void deleteTeamEntityByName(@Param("teamId") Long teamId); @Query("SELECT T FROM TeamEntity T WHERE T.status = true order by T.name") - List findAllTeamsByStatus(); + List findAllActiveTeams(); + + + @Query("SELECT T FROM TeamEntity T ORDER BY T.name") + List findAllTeams(); }