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

Feat : 직위 추가 ddl 수정, 교직원 스크랩 로직 및 주기 변경 #224

Merged
merged 4 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -3,9 +3,6 @@
import com.kustacks.kuring.staff.domain.Staff;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface StaffRepository extends JpaRepository<Staff, Long>, StaffQueryRepository {

List<Staff> findByDeptContaining(String deptName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -29,20 +28,19 @@ public void compareAndUpdateDb(StaffScrapResults scrapResults) {
}

private StaffCompareResults compare(StaffScrapResults scrapResults) {
Map<String, Staff> originStaffStorage = findByDeptContainingMap(scrapResults.successDepartmentNames());
Map<String, Staff> originStaffStorage = findAllOriginStaffs();
return staffCompareSupport.compareAllDepartmentsAndUpdateExistStaff(scrapResults.kuStaffDTOMap(), originStaffStorage);
}

private void synchronizationWithDb(StaffCompareResults compareResults) {
System.out.println(compareResults.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인용 출력문은 제거 부탁해요~

staffRepository.deleteAll(compareResults.deleteStaffs());
staffRepository.saveAll(compareResults.newStaffs());
}

private Map<String, Staff> findByDeptContainingMap(List<String> scrapSuccessDepartmentNames) {
return scrapSuccessDepartmentNames.stream()
.flatMap(
deptName -> staffRepository.findByDeptContaining(deptName).stream()
).collect(
private Map<String, Staff> findAllOriginStaffs() {
return staffRepository.findAll().stream()
.collect(
Collectors.toMap(
Staff::identifier,
Function.identity(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -26,7 +25,7 @@ public class StaffUpdater {
private final StaffScraper staffScraper;
private final List<DeptInfo> deptInfos;

@Scheduled(fixedRate = 1, timeUnit = TimeUnit.DAYS)
@Scheduled(fixedRate = 30, timeUnit = TimeUnit.DAYS)
public void update() {
log.info("========== 교직원 업데이트 시작 ==========");

Expand All @@ -38,19 +37,17 @@ public void update() {

private StaffScrapResults scrapAllDepartmentsStaffs() {
Map<String, StaffDto> kuStaffDtoMap = new HashMap<>();
List<String> successDepartmentNames = new LinkedList<>();

deptInfos.stream()
.filter(DeptInfo::isSupportStaffScrap)
.forEach(deptInfo -> scrapSingleDepartmentsStaffs(kuStaffDtoMap, successDepartmentNames, deptInfo));
return new StaffScrapResults(kuStaffDtoMap, successDepartmentNames);
.forEach(deptInfo -> scrapSingleDepartmentsStaffs(kuStaffDtoMap, deptInfo));
return new StaffScrapResults(kuStaffDtoMap);
}

private void scrapSingleDepartmentsStaffs(Map<String, StaffDto> kuStaffDtoMap, List<String> successDepartmentNames, DeptInfo deptInfo) {
private void scrapSingleDepartmentsStaffs(Map<String, StaffDto> kuStaffDtoMap, DeptInfo deptInfo) {
try {
Map<String, StaffDto> staffScrapResultMap = scrapStaffByDepartment(deptInfo);
mergeForMultipleDepartmentsStaff(kuStaffDtoMap, staffScrapResultMap);
successDepartmentNames.add(deptInfo.getDeptName());
} catch (InternalLogicException e) {
log.error("[StaffScraperException] {}학과 교직원 스크래핑 문제 발생.", deptInfo.getDeptName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import java.util.Map;

public record StaffScrapResults(
Map<String, StaffDto> kuStaffDTOMap,
List<String> successDepartmentNames
Map<String, StaffDto> kuStaffDTOMap
) {

public List<StaffDto> getStaffDtos() {
return kuStaffDTOMap.values().stream().toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ALTER TABLE staff
ADD column position varchar(64) default false;
ADD column position varchar(64);
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ void compareAndUpdateDb() {
kuStaffDtoMap.put(dto1.identifier(), dto1);
kuStaffDtoMap.put(dto2.identifier(), dto2);

List<String> successDepartmentNames = List.of("컴퓨터공학과", "생명과학부");
StaffScrapResults staffScrapResults = new StaffScrapResults(kuStaffDtoMap, successDepartmentNames);
StaffScrapResults staffScrapResults = new StaffScrapResults(kuStaffDtoMap);

// when
staffDataSynchronizer.compareAndUpdateDb(staffScrapResults);
Expand Down
Loading