스크린샷 2023-11-07 오후 8.29.13.png

📷 CCTV

Model

@Builder@Getter@NoArgsConstructor@AllArgsConstructor
@Entity@Table(name = "CCTV")@Where(clause = "active_status = 'Y'")
public class CCTV {
    @Id
    @Column(name = "id")
    @JsonProperty("id")
    private int cctvId;

    @Column(name = "지역")
    @JsonProperty("Area")
    private String area;

    @Column(name = "도로명주소")
    @JsonProperty("new_address")
    private String newAddress;

    @Column(name = "지번주소")
    @JsonProperty("old_address")
    private String oldAddress;

    @Column(name = "설치목적")
    @JsonProperty("purpose")
    private String purpose;

    @Column(name = "갯수")
    @JsonProperty("number")
    private Integer number;

    @Column(name = "화소수")
    @JsonProperty("pixel")
    private Integer pixel;

    @Column(name = "촬영방면")
    @JsonProperty("recode_side")
    private String recodeSide;

    @Column(name = "보관일수")
    @JsonProperty("store_days")
    private Integer storeDays;

    @Column(name = "설치날짜")
    @JsonProperty("install_date")
    private String installDate;

    @Column(name = "전화번호")
    @JsonProperty("call")
    private String call;

    @Column(name = "위도")
    @JsonProperty("latitude")
    private Double latitude;

    @Column(name = "경도")
    @JsonProperty("equator")
    private Double equator;

    @Column(name = "업데이트날짜")
    @JsonProperty("recent_update")
    private String recentUpdate;
}

Controller

@Controller @RequiredArgsConstructor
public class CCTVcontroller {
    private final CCTVservice cctvService;

    @PostMapping("/Safety_route/walking")
    public ResponseEntity<?> walkingRoute(@RequestBody startToEnd ste) {
        System.out.println("Received walkingRoute request with data: " + ste);
        String result = cctvService.walkingUrl(ste);
        return new ResponseEntity<>(new respDto<>(1,result), HttpStatus.OK);
    }

    @PostMapping("/Safety_route/CCTV/searching")
    public ResponseEntity<?> cctvCount(@RequestBody currentLocation cl) {
        List<CCTV> result = cctvService.searchCctv(cl);
        return new ResponseEntity<>(new respDto<>(1,result), HttpStatus.OK);
    }
}

Repository

public interface CCTVrepository extends JpaRepository<CCTV,Integer> {
    @Query(value = "select one.* from CCTV one , ( "
            +" SELECT ( 6371 * acos( cos( radians( :x ) ) * cos( radians( two.위도 ) ) * cos( radians( two.경도 ) - radians(:y) ) "
            +" + sin( radians( :x ) ) * sin( radians(two.위도) ) ) ) AS distance ,two.id "
            +" FROM CCTV two )Data "
            +" where Data.distance < 2 && one.id = Data.id ", nativeQuery = true)
    List<CCTV> searchCctv(@Param("y") double y,@Param("x") double x);
//    @Query(value = "select * from CCTV where CCTV.id<100", nativeQuery = true)
//    List<CCTV> searchCctv(@Param("x") double x,@Param("y") double y);
}

Service

@Service @RequiredArgsConstructor @Log4j2
public class CCTVserviceImpl implements CCTVservice {
    private final CCTVrepository cctvrepository;
    @Override
    public String walkingUrl(startToEnd ste){
        String uri = "<http://localhost:5000/route/v1/foot/>";
        String uri_End = "?steps=true";
        uri+=(Double.toString(ste.getStart_x())+",");
        uri+=(Double.toString(ste.getStart_y())+";");
        uri+=(Double.toString(ste.getEnd_x())+",");
        uri+=(Double.toString(ste.getEnd_y()));
        uri+=uri_End;
        log.error(uri);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(uri, String.class);

        return result;
    }

    @Override
    public List<CCTV> searchCctv(currentLocation cl){
        double x = cl.getX();
        double y = cl.getY();
        return cctvrepository.searchCctv(x,y);
    }
}

public interface CCTVservice {
    String walkingUrl(startToEnd ste);
    List<CCTV> searchCctv(currentLocation cl);
}

🍱 DTO

currentLocation

@Getter
public class currentLocation {
    double x;
    double y;
}

respDto

@NoArgsConstructor@AllArgsConstructor@Data
public class respDto<T> {
    private int code;
    private T data;
}

startToEnd

@Getter @Setter
public class startToEnd {
    double start_x;
    double start_y;
    double end_x;
    double end_y;
}

🌐 Cors

@Configuration
public class Cors implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOrigins("<http://localhost:3000>")
                .allowedMethods("*");
    }
}