Skip to content

Commit

Permalink
feat : (Admin)런타임 수집 노선 서비스 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyaak committed Aug 28, 2024
1 parent 0f792a2 commit 23b6b56
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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("삭제에 성공했습니다.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public record CollectBusRouteCreateDto(
public CollectBusRouteEntity toEntity(BusRouteEntity route) {
return CollectBusRouteEntity.builder()
.route(route)
.apiRouteId(route.getApiRouteId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@
@Service
@RequiredArgsConstructor
public class CollectBusRouteService {
private final CollectedRouteProvider collectedRouteProvider;
private final CollectBusRouteRepository collectBusRouteRepository;
private final BusRouteRepository busRouteRepository;

Expand All @@ -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)));
}

Expand All @@ -37,9 +40,12 @@ public List<CollectBusRouteRespDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.talkka.server.admin.util;

import java.util.List;

public interface CollectedRouteProvider {
List<String> getTargetIdList();
}
Original file line number Diff line number Diff line change
@@ -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<String> targetIdList = new ArrayList<>();
private final CollectBusRouteRepository collectBusRouteRepository;

@PostConstruct
public void init() {
targetIdList.addAll(
collectBusRouteRepository.findAll().stream()
.map(CollectBusRouteEntity::getApiRouteId)
.toList()
);
}

@Override
public List<String> getTargetIdList() {
return targetIdList;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.talkka.server.bus.util;
package com.talkka.server.admin.util;

import java.util.List;

Expand All @@ -12,6 +12,6 @@
@Setter
@Getter
@ConfigurationProperties(prefix = "bus.location.collect")
public class BusLocationCollectProperty implements BusLocationCollectProvider {
public class PropertyCollectedRouteProvider implements CollectedRouteProvider {
private List<String> targetIdList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
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")
BusApiService busApiService,
@Qualifier("busLocationService")
BusLocationService busLocationService
) {
this.busLocationCollectProvider = busLocationCollectProvider;
this.collectedRouteProvider = collectedRouteProvider;
this.apiCallNumberProvider = apiCallNumberProvider;
this.busApiService = busApiService;
this.busLocationService = busLocationService;
Expand All @@ -42,7 +42,7 @@ public BlockedApiLocationCollectService(
@Override
@Transactional
public void collectLocations() {
List<String> apiRouteIdList = busLocationCollectProvider.getTargetIdList();
List<String> apiRouteIdList = collectedRouteProvider.getTargetIdList();
if (apiRouteIdList.isEmpty()) {
log.info("No target routeId to collect bus locations");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -41,7 +41,7 @@ public LocationCollectingScheduler(
@Scheduled(fixedRate = 1000 * 60) // per minute
public void runParallelLocationScheduler() {
if (isEnabled()) {
List<String> targetList = busLocationCollectProvider.getTargetIdList();
List<String> targetList = collectedRouteProvider.getTargetIdList();
ExecutorService executor = Executors.newFixedThreadPool(20);

// CompletableFuture 리스트 생성
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
mode: never # schema.sql 실행시 always 키고 실행하시면 됩니다.
jpa:
hibernate:
ddl-auto: validate
ddl-auto: update
properties:
hibernate:
format_sql: true
Expand Down

0 comments on commit 23b6b56

Please sign in to comment.