diff --git a/server/src/main/java/com/talkka/server/admin/controller/AdminController.java b/server/src/main/java/com/talkka/server/admin/controller/AdminController.java index 4ae4aafa..72d546e2 100644 --- a/server/src/main/java/com/talkka/server/admin/controller/AdminController.java +++ b/server/src/main/java/com/talkka/server/admin/controller/AdminController.java @@ -73,12 +73,13 @@ public String createCollectRoute(CollectBusRouteCreateDto dto, Model model) { } @DeleteMapping("/collect-route/{collectRouteId}") - public ResponseEntity deleteCollectRoute(@PathVariable Long collectRouteId, Model model) { + public ResponseEntity deleteCollectRoute(@PathVariable Long collectRouteId, Model model) { try { collectBusRouteService.deleteCollectBusRoute(collectRouteId); } catch (CollectBusRouteNotFoundException exception) { return ResponseEntity.badRequest().body(exception.getMessage()); } - return ResponseEntity.ok().build(); + return ResponseEntity.ok().body("삭제에 성공했습니다."); } + } diff --git a/server/src/main/java/com/talkka/server/admin/dao/CollectBusRouteEntity.java b/server/src/main/java/com/talkka/server/admin/dao/CollectBusRouteEntity.java index 83402d20..1203ac27 100644 --- a/server/src/main/java/com/talkka/server/admin/dao/CollectBusRouteEntity.java +++ b/server/src/main/java/com/talkka/server/admin/dao/CollectBusRouteEntity.java @@ -36,6 +36,9 @@ public class CollectBusRouteEntity { @JoinColumn(name = "route_id", nullable = false) private BusRouteEntity route; + @Column(name = "api_route_id", nullable = false) + private String apiRouteId; + @Column(name = "created_at", nullable = false) @CreatedDate private LocalDateTime createdAt; diff --git a/server/src/main/java/com/talkka/server/admin/dto/CollectBusRouteCreateDto.java b/server/src/main/java/com/talkka/server/admin/dto/CollectBusRouteCreateDto.java index 904658e9..41e3c521 100644 --- a/server/src/main/java/com/talkka/server/admin/dto/CollectBusRouteCreateDto.java +++ b/server/src/main/java/com/talkka/server/admin/dto/CollectBusRouteCreateDto.java @@ -10,6 +10,7 @@ public record CollectBusRouteCreateDto( public CollectBusRouteEntity toEntity(BusRouteEntity route) { return CollectBusRouteEntity.builder() .route(route) + .apiRouteId(route.getApiRouteId()) .build(); } } diff --git a/server/src/main/java/com/talkka/server/admin/service/CollectBusRouteService.java b/server/src/main/java/com/talkka/server/admin/service/CollectBusRouteService.java index 59c61382..2ee1f20b 100644 --- a/server/src/main/java/com/talkka/server/admin/service/CollectBusRouteService.java +++ b/server/src/main/java/com/talkka/server/admin/service/CollectBusRouteService.java @@ -9,6 +9,7 @@ import com.talkka.server.admin.dto.CollectBusRouteRespDto; import com.talkka.server.admin.exception.CollectBusRouteAlreadyExistsException; import com.talkka.server.admin.exception.CollectBusRouteNotFoundException; +import com.talkka.server.admin.util.CollectedRouteProvider; import com.talkka.server.bus.dao.BusRouteEntity; import com.talkka.server.bus.dao.BusRouteRepository; import com.talkka.server.bus.exception.BusRouteNotFoundException; @@ -18,6 +19,7 @@ @Service @RequiredArgsConstructor public class CollectBusRouteService { + private final CollectedRouteProvider collectedRouteProvider; private final CollectBusRouteRepository collectBusRouteRepository; private final BusRouteRepository busRouteRepository; @@ -29,6 +31,7 @@ public CollectBusRouteRespDto createCollectBusRoute(CollectBusRouteCreateDto dto } BusRouteEntity route = busRouteRepository.findById(dto.routeId()) .orElseThrow(() -> new BusRouteNotFoundException(dto.routeId())); + collectedRouteProvider.getTargetIdList().add(route.getApiRouteId()); return CollectBusRouteRespDto.of(collectBusRouteRepository.save(dto.toEntity(route))); } @@ -37,9 +40,12 @@ public List findAllCollectBusRoutes() { } public void deleteCollectBusRoute(Long collectRouteId) throws CollectBusRouteNotFoundException { - if (collectBusRouteRepository.findById(collectRouteId).isEmpty()) { + var entity = collectBusRouteRepository.findById(collectRouteId); + if (entity.isPresent()) { + collectBusRouteRepository.delete(entity.get()); + collectedRouteProvider.getTargetIdList().remove(entity.get().getApiRouteId()); + } else { throw new CollectBusRouteNotFoundException(); } - collectBusRouteRepository.deleteById(collectRouteId); } } diff --git a/server/src/main/java/com/talkka/server/admin/util/CollectedRouteProvider.java b/server/src/main/java/com/talkka/server/admin/util/CollectedRouteProvider.java new file mode 100644 index 00000000..919a64d4 --- /dev/null +++ b/server/src/main/java/com/talkka/server/admin/util/CollectedRouteProvider.java @@ -0,0 +1,7 @@ +package com.talkka.server.admin.util; + +import java.util.List; + +public interface CollectedRouteProvider { + List getTargetIdList(); +} diff --git a/server/src/main/java/com/talkka/server/admin/util/PersistenceCollectedRouteProvider.java b/server/src/main/java/com/talkka/server/admin/util/PersistenceCollectedRouteProvider.java new file mode 100644 index 00000000..4b9f7e79 --- /dev/null +++ b/server/src/main/java/com/talkka/server/admin/util/PersistenceCollectedRouteProvider.java @@ -0,0 +1,35 @@ +package com.talkka.server.admin.util; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import com.talkka.server.admin.dao.CollectBusRouteEntity; +import com.talkka.server.admin.dao.CollectBusRouteRepository; + +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +@Primary +public class PersistenceCollectedRouteProvider implements CollectedRouteProvider { + private final List targetIdList = new ArrayList<>(); + private final CollectBusRouteRepository collectBusRouteRepository; + + @PostConstruct + public void init() { + targetIdList.addAll( + collectBusRouteRepository.findAll().stream() + .map(CollectBusRouteEntity::getApiRouteId) + .toList() + ); + } + + @Override + public List getTargetIdList() { + return targetIdList; + } +} diff --git a/server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProperty.java b/server/src/main/java/com/talkka/server/admin/util/PropertyCollectedRouteProvider.java similarity index 72% rename from server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProperty.java rename to server/src/main/java/com/talkka/server/admin/util/PropertyCollectedRouteProvider.java index d6f81371..ecd1e5db 100644 --- a/server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProperty.java +++ b/server/src/main/java/com/talkka/server/admin/util/PropertyCollectedRouteProvider.java @@ -1,4 +1,4 @@ -package com.talkka.server.bus.util; +package com.talkka.server.admin.util; import java.util.List; @@ -12,6 +12,6 @@ @Setter @Getter @ConfigurationProperties(prefix = "bus.location.collect") -public class BusLocationCollectProperty implements BusLocationCollectProvider { +public class PropertyCollectedRouteProvider implements CollectedRouteProvider { private List targetIdList; } diff --git a/server/src/main/java/com/talkka/server/bus/service/BlockedApiLocationCollectService.java b/server/src/main/java/com/talkka/server/bus/service/BlockedApiLocationCollectService.java index a9ed170c..aa885b42 100644 --- a/server/src/main/java/com/talkka/server/bus/service/BlockedApiLocationCollectService.java +++ b/server/src/main/java/com/talkka/server/bus/service/BlockedApiLocationCollectService.java @@ -7,25 +7,25 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.talkka.server.admin.util.CollectedRouteProvider; import com.talkka.server.api.core.exception.ApiClientException; import com.talkka.server.api.datagg.dto.BusLocationBodyDto; import com.talkka.server.api.datagg.service.BusApiService; import com.talkka.server.bus.util.ApiCallNumberProvider; -import com.talkka.server.bus.util.BusLocationCollectProvider; import lombok.extern.slf4j.Slf4j; @Slf4j @Service public class BlockedApiLocationCollectService implements BusLocationCollectService { - private final BusLocationCollectProvider busLocationCollectProvider; + private final CollectedRouteProvider collectedRouteProvider; private final ApiCallNumberProvider apiCallNumberProvider; private final BusApiService busApiService; private final BusLocationService busLocationService; public BlockedApiLocationCollectService( - @Qualifier("busLocationCollectProperty") - BusLocationCollectProvider busLocationCollectProvider, + @Qualifier("persistenceCollectedRouteProvider") + CollectedRouteProvider collectedRouteProvider, @Qualifier("minuteApiCallNumberProvider") ApiCallNumberProvider apiCallNumberProvider, @Qualifier("simpleBusApiService") @@ -33,7 +33,7 @@ public BlockedApiLocationCollectService( @Qualifier("busLocationService") BusLocationService busLocationService ) { - this.busLocationCollectProvider = busLocationCollectProvider; + this.collectedRouteProvider = collectedRouteProvider; this.apiCallNumberProvider = apiCallNumberProvider; this.busApiService = busApiService; this.busLocationService = busLocationService; @@ -42,7 +42,7 @@ public BlockedApiLocationCollectService( @Override @Transactional public void collectLocations() { - List apiRouteIdList = busLocationCollectProvider.getTargetIdList(); + List apiRouteIdList = collectedRouteProvider.getTargetIdList(); if (apiRouteIdList.isEmpty()) { log.info("No target routeId to collect bus locations"); return; diff --git a/server/src/main/java/com/talkka/server/bus/service/LocationCollectingScheduler.java b/server/src/main/java/com/talkka/server/bus/service/LocationCollectingScheduler.java index c1f0391b..adae14f0 100644 --- a/server/src/main/java/com/talkka/server/bus/service/LocationCollectingScheduler.java +++ b/server/src/main/java/com/talkka/server/bus/service/LocationCollectingScheduler.java @@ -13,7 +13,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import com.talkka.server.bus.util.BusLocationCollectProvider; +import com.talkka.server.admin.util.CollectedRouteProvider; import com.talkka.server.bus.util.LocationCollectingSchedulerConfigProperty; import lombok.extern.slf4j.Slf4j; @@ -25,7 +25,7 @@ public class LocationCollectingScheduler { private final LocationCollectingSchedulerConfigProperty locationCollectingSchedulerConfigProperty; private final BusLocationCollectService busLocationCollectService; @Autowired - private BusLocationCollectProvider busLocationCollectProvider; + private CollectedRouteProvider collectedRouteProvider; public LocationCollectingScheduler( @Qualifier("locationCollectingSchedulerConfigProperty") @@ -41,7 +41,7 @@ public LocationCollectingScheduler( @Scheduled(fixedRate = 1000 * 60) // per minute public void runParallelLocationScheduler() { if (isEnabled()) { - List targetList = busLocationCollectProvider.getTargetIdList(); + List targetList = collectedRouteProvider.getTargetIdList(); ExecutorService executor = Executors.newFixedThreadPool(20); // CompletableFuture 리스트 생성 diff --git a/server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProvider.java b/server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProvider.java deleted file mode 100644 index 569367e9..00000000 --- a/server/src/main/java/com/talkka/server/bus/util/BusLocationCollectProvider.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.talkka.server.bus.util; - -import java.util.List; - -public interface BusLocationCollectProvider { - List getTargetIdList(); -} diff --git a/server/src/main/resources/application.yaml b/server/src/main/resources/application.yaml index c67bcace..83c88a83 100644 --- a/server/src/main/resources/application.yaml +++ b/server/src/main/resources/application.yaml @@ -11,7 +11,7 @@ spring: mode: never # schema.sql 실행시 always 키고 실행하시면 됩니다. jpa: hibernate: - ddl-auto: validate + ddl-auto: update properties: hibernate: format_sql: true