Skip to content

Commit

Permalink
[UNI-333] 특정 버전 미만으로 롤백 방지 & 6km 이상 길 추가 방지 로직 작성 (#190)
Browse files Browse the repository at this point in the history
* [UNI-333] feat : 길 추가 로직에서 2000개 (약 6km) 이상의 노드를 추가하지 못하도록 Limit 제한

* [UNI-333] feat : 특정 버전 미만으로 롤백하지 못하도록 코드 작성

* [UNI-333] limitVersion을 DB에 저장하도록 수정

* [UNI-333] 컬럼명 명시

* [UNI-333] 테스트 컨테이너에 임시 학교 할당

* [UNI-333] 테스트 코드 변경

* [UNI-333] refactor : limit version을 primitive type으로 변경
  • Loading branch information
thdgustjd1 authored Feb 20, 2025
1 parent c25a612 commit 08f8dd0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.softeer5.uniro_backend.building.repository.BuildingRepository;
import com.softeer5.uniro_backend.common.exception.custom.AdminException;
import com.softeer5.uniro_backend.common.exception.custom.RouteException;
import com.softeer5.uniro_backend.common.exception.custom.UnivException;
import com.softeer5.uniro_backend.map.dto.response.AllRoutesInfo;
import com.softeer5.uniro_backend.map.dto.response.GetChangedRoutesByRevisionResDTO;
import com.softeer5.uniro_backend.map.dto.response.GetRiskRoutesResDTO;
Expand All @@ -20,14 +21,13 @@
import com.softeer5.uniro_backend.map.repository.NodeRepository;
import com.softeer5.uniro_backend.map.repository.RouteRepository;
import com.softeer5.uniro_backend.map.service.RouteCalculator;
import com.softeer5.uniro_backend.univ.entity.Univ;
import com.softeer5.uniro_backend.univ.repository.UnivRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand All @@ -38,6 +38,7 @@ public class AdminService {
private final RouteRepository routeRepository;
private final NodeRepository nodeRepository;
private final BuildingRepository buildingRepository;
private final UnivRepository univRepository;

private final RouteAuditRepository routeAuditRepository;
private final NodeAuditRepository nodeAuditRepository;
Expand All @@ -54,6 +55,14 @@ public List<RevInfoDTO> getAllRevInfo(Long univId){
@Transactional
@DisableAudit
public void rollbackRev(Long univId, Long versionId){

Univ univ = univRepository.findById(univId)
.orElseThrow(()-> new UnivException("Univ not found", UNIV_NOT_FOUND));
long limitVersion = univ.getLimitVersion();
if(limitVersion > versionId){
throw new AdminException("version is too low", CANT_ROLLBACK_BELOW_MINIMUM_VERSION);
}

RevInfo revInfo = revInfoRepository.findFirstByUnivIdAndRevAfter(univId, versionId)
.orElseThrow(() -> new AdminException("Already the latest version id", ALREADY_LATEST_VERSION_ID));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public final class UniroConst {
public static final int IS_SINGLE_ROUTE = 2;
public static final double HEURISTIC_WEIGHT_NORMALIZATION_FACTOR = 10.0;
public static final int STREAM_FETCH_SIZE = 2500;

//컴파일 상수를 보장하기 위한 코드
public static final String STREAM_FETCH_SIZE_AS_STRING = "" + STREAM_FETCH_SIZE;

public static final int CREATE_ROUTE_LIMIT_COUNT = 2000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorCode {
ROUTE_NOT_FOUND(404, "루트를 찾을 수 없습니다."),
CAUTION_DANGER_CANT_EXIST_SIMULTANEOUSLY(400, "위험요소와 주의요소는 동시에 존재할 수 없습니다."),
INVALID_MAP(500,"현재 지도 데이터가 제약조건에 어긋난 상태입니다."),
CREATE_ROUTE_LIMIT_EXCEEDED(400, "길 추가 최대 길이를 초과하였습니다."),

//길 생성
ELEVATION_API_ERROR(500, "구글 해발고도 API에서 오류가 발생했습니다."),
Expand All @@ -34,6 +35,9 @@ public enum ErrorCode {
// 경로 계산
INTERSECTION_ONLY_ALLOWED_POINT(400, "기존 경로와 겹칠 수 없습니다."),

// 대학
UNIV_NOT_FOUND(404, "대학을 찾을 수 없습니다."),

// 어드민
ALREADY_LATEST_VERSION_ID(400, "현재 가장 최신 버전 id 입니다."),
INVALID_VERSION_ID(400, "유효하지 않은 버전 id 입니다."),
Expand All @@ -42,6 +46,7 @@ public enum ErrorCode {
UNAUTHORIZED_UNIV(401, "해당 대학교의 권한이 없습니다."),
INVALID_UNIV_ID(400, "유효하지 않은 대학교 id 입니다."),
RECENT_REVISION_NOT_FOUND(404, "최신 버전을 찾을 수 없습니다."),
CANT_ROLLBACK_BELOW_MINIMUM_VERSION(400, "롤백하려는 버전이 허용된 최소 버전보다 낮습니다.")
;

private final int httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public void createBuildingRoute(Long univId, CreateBuildingRouteReqDTO createBui
@RevisionOperation(RevisionOperationType.CREATE_ROUTE)
synchronized public AllRoutesInfo createRoute(Long univId, CreateRoutesReqDTO requests){

if(requests.getCoordinates().size() >= CREATE_ROUTE_LIMIT_COUNT){
throw new RouteException("creat route limit exceeded", CREATE_ROUTE_LIMIT_EXCEEDED);
}

List<Route> savedRoutes = routeRepository.findAllRouteByUnivIdWithNodes(univId);

List<Node> nodesForSave = routeCalculator.createValidRouteNodes(univId, requests.getStartNodeId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class Univ {
@NotNull
private Polygon areaPolygon;

@Column(name = "limit_version")
@NotNull
private long limitVersion;

public Map<String, Double> getCenterXY(){
return Map.of("lat", centerPoint.getY(), "lng", centerPoint.getX());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@SqlGroup({
@Sql(value = "/sql/delete-all-data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD),
@Sql(value = "/sql/insert-univ-data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD),
@Sql(value = "/sql/delete-all-data.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
})
class AdminServiceTest {
Expand Down
14 changes: 14 additions & 0 deletions uniro_backend/src/test/resources/sql/insert-univ-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO univ
(id, name, trimmed_name, image_url, center_point, left_top_point, right_bottom_point, area_polygon, limit_version)
VALUES
(
1001,
'HYUNIV',
'HYUNIV',
'http://example.com/hanyang.jpg',
ST_GeomFromText('POINT(126.9780 37.5665)', 4326),
ST_GeomFromText('POINT(126.9720 37.5700)', 4326),
ST_GeomFromText('POINT(126.9850 37.5620)', 4326),
ST_GeomFromText('POLYGON((126.9720 37.5700, 126.9850 37.5700, 126.9850 37.5620, 126.9720 37.5620, 126.9720 37.5700))', 4326),
0
);

0 comments on commit 08f8dd0

Please sign in to comment.