Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
152 changes: 0 additions & 152 deletions app/controllers/api/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,6 @@ def destroy
render json: { error: "Course not found" }, status: :not_found
end

# GET /api/v1/courses/:id/directions?mode=transit|driving
# 코스 내 장소들 간의 경로 검색
# A → B → C → D 코스라면, A→B, B→C, C→D 경로를 모두 반환
def directions
course = current_user.courses.includes(course_places: :place).find(params[:id])
places = course.course_places.order(:position).map(&:place)

if places.length < 2
render json: { error: "Course must have at least 2 places" }, status: :unprocessable_entity
return
end

mode = params[:mode]
unless %w[transit driving].include?(mode)
render json: { error: "Invalid mode. Use 'transit' or 'driving'" }, status: :bad_request
return
end

# 각 구간별 경로 검색
segments = []
places.each_cons(2).with_index do |(from_place, to_place), index|
route = fetch_route(from_place, to_place, mode)
segments << {
segment: index + 1,
from: format_place_simple(from_place),
to: format_place_simple(to_place),
route: route
}
end

render json: {
course_id: course.id,
course_name: course.name,
mode: mode,
total_segments: segments.length,
segments: segments,
summary: calculate_summary(segments, mode)
}, status: :ok
rescue ActiveRecord::RecordNotFound
render json: { error: "Course not found" }, status: :not_found
end

private

def places_params
Expand Down Expand Up @@ -131,116 +89,6 @@ def format_place(course_place)
naverMapUrl: place.naver_map_url
}
end

def format_place_simple(place)
{
name: place.name,
lat: place.latitude.to_f,
lng: place.longitude.to_f
}
end

def fetch_route(from_place, to_place, mode)
case mode
when "transit"
OdsayTransitService.search_route(
start_lat: from_place.latitude.to_f,
start_lng: from_place.longitude.to_f,
end_lat: to_place.latitude.to_f,
end_lng: to_place.longitude.to_f
)
when "driving"
NaverDirectionsService.search_route(
start_lat: from_place.latitude.to_f,
start_lng: from_place.longitude.to_f,
end_lat: to_place.latitude.to_f,
end_lng: to_place.longitude.to_f
)
end
end

def calculate_summary(segments, mode)
case mode
when "transit"
calculate_transit_summary(segments)
when "driving"
calculate_driving_summary(segments)
end
end

def calculate_transit_summary(segments)
total_time = 0
total_distance = 0
total_payment = 0

segments.each do |segment|
route = segment[:route]
next if route[:error] || route[:paths].blank?

# 첫 번째 경로(추천 경로) 기준
best_path = route[:paths].first
total_time += best_path[:total_time].to_i
total_distance += best_path[:total_distance].to_i
total_payment += best_path[:payment].to_i
end

{
total_time: total_time,
total_time_text: "#{total_time}분",
total_distance: total_distance,
total_distance_text: format_distance(total_distance),
total_payment: total_payment,
total_payment_text: "#{total_payment.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse}원"
}
end

def calculate_driving_summary(segments)
total_duration = 0
total_distance = 0
total_toll = 0
total_fuel = 0

segments.each do |segment|
route = segment[:route]
next if route[:error] || route[:summary].blank?

summary = route[:summary]
total_duration += summary[:duration].to_i
total_distance += summary[:distance].to_i
total_toll += summary[:toll_fare].to_i
total_fuel += summary[:fuel_price].to_i
end

total_minutes = (total_duration / 60_000.0).round

{
total_duration: total_duration,
total_duration_minutes: total_minutes,
total_duration_text: format_duration(total_minutes),
total_distance: total_distance,
total_distance_text: format_distance(total_distance),
total_toll_fare: total_toll,
total_fuel_price: total_fuel
}
end

def format_distance(meters)
if meters >= 1000
"#{(meters / 1000.0).round(1)}km"
else
"#{meters}m"
end
end

def format_duration(minutes)
hours = (minutes / 60).to_i
mins = (minutes % 60).to_i
if hours > 0
"#{hours}시간 #{mins}분"
else
"#{mins}분"
end
end
end
end
end
161 changes: 0 additions & 161 deletions app/controllers/api/v1/directions_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/api/v1/external_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module V1
# 외부 장소 검색 API 컨트롤러
# 네이버 로컬 검색 API를 프록시하여 클라이언트에 제공
class ExternalController < ApplicationController
before_action :require_login
# 로그인 불필요 - 공개 검색 API

# GET /api/v1/external/search?query=스타벅스 강남역
# 외부 API로 장소를 검색하여 결과를 반환
Expand Down
Loading