Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] 데이터베이스 스키마 변경에 따른 초기화 로직 추가 #161

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void handleDeletedProgram(EndAttendModeEvent event) {
}

if (event.getProgramIds().isEmpty()) {
log.info("Empty Set");
log.info("종료할 프로그램이 없습니다.");
}
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private List<TeamModel> findTeamByProgram(Long programId) {
}

private List<TeamModel> findTeams() {
return teamRepository.findAllTeamsByStatus().stream()
return teamRepository.findAllActiveTeams().stream()
.map(entityConverter::from)
.collect(Collectors.toList());
}
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ public interface TeamRepository extends JpaRepository<TeamEntity, Long> {
void deleteTeamEntityByName(@Param("teamId") Long teamId);

@Query("SELECT T FROM TeamEntity T WHERE T.status = true order by T.name")
List<TeamEntity> findAllTeamsByStatus();
List<TeamEntity> findAllActiveTeams();


@Query("SELECT T FROM TeamEntity T ORDER BY T.name")
List<TeamEntity> findAllTeams();
}
Loading