Skip to content

Commit

Permalink
Merge pull request #66 from snuhcs-course/backend/refactor-place-reco…
Browse files Browse the repository at this point in the history
…mmend

장소 추천에 유저의 마지막 위치를 사용한다
  • Loading branch information
leeeryboy authored Dec 6, 2023
2 parents aa07ba6 + 1e91389 commit cd235c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
11 changes: 6 additions & 5 deletions backend/app/controllers/places_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

class PlacesController < ApplicationController
skip_before_action :require_login

def recommend
# TODO: User 입력으로 변경하기
latitude = params[:latitude].to_f
longitude = params[:longitude].to_f
user_ids = params[:user_ids].map(&:to_i) + [current_user.id]

checkins = CheckIn.where(user_id: user_ids).last_check_in_by_user.select(:latitude, :longitude).to_ary

latitude = checkins.sum { |x| x.latitude } / checkins.size
longitude = checkins.sum { |x| x.longitude } / checkins.size

render json: {
places: RecommendPlaces.new(latitude: latitude, longitude: longitude).run
Expand Down
4 changes: 2 additions & 2 deletions backend/app/services/recommend_places.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class RecommendPlaces
HOST = 'localhost'
PORT = 50051
HOST = ENV.fetch("PLACE_SERVICE_HOST") { "localhost" }
PORT = ENV.fetch("PLACE_SERVICE_PORT") { 50051 }

def initialize(latitude:, longitude:)
@latitude = latitude
Expand Down
32 changes: 32 additions & 0 deletions backend/spec/controllers/places_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe PlacesController, type: :request do
let!(:user) { User.create!(email: "[email protected]", name: "test", password: "1234") }
let!(:user2) { User.create!(email: "[email protected]", name: "test2", password: "1234") }
let!(:user3) { User.create!(email: "[email protected]", name: "test3", password: "1234") }

before do
allow_any_instance_of(Swpp::V1::PlaceService::Stub).to receive(:list_places).and_return(
Swpp::V1::ListPlacesResponse.new(place_ids: [1, 2, 3])
)
end

describe "GET places/recommend" do
context "유저 id 목록을 넘겨주면" do
it "그들의 중점으로 요청한다" do

user.check_in!(latitude: 126.1, longitude: 37.1)
user2.check_in!(latitude: 127.1, longitude: 38.1)
user3.check_in!(latitude: 128.1, longitude: 39.1)

get "/places/recommend",
headers: auth_header(user),
params: {user_ids: [user2.id, user3.id]}

expect(response).to have_http_status(:ok)
end
end
end
end

0 comments on commit cd235c9

Please sign in to comment.