From 372f36abc73149a0f4d1de6acabbcdbf5f1deb42 Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Sun, 14 Apr 2019 11:41:18 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E9=81=8E=E5=8E=BB=E3=83=81=E3=83=A3?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=82=92rest=20api=E3=81=8B=E3=82=89?= =?UTF-8?q?=E8=A6=8B=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/chats_controller.rb | 10 ++++++++++ app/views/api/v1/chats/index.json.jbuilder | 3 +++ config/routes.rb | 1 + spec/requests/api/v1/chats_spec.rb | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 app/controllers/api/v1/chats_controller.rb create mode 100644 app/views/api/v1/chats/index.json.jbuilder create mode 100644 spec/requests/api/v1/chats_spec.rb diff --git a/app/controllers/api/v1/chats_controller.rb b/app/controllers/api/v1/chats_controller.rb new file mode 100644 index 0000000..df193d3 --- /dev/null +++ b/app/controllers/api/v1/chats_controller.rb @@ -0,0 +1,10 @@ +class Api::V1::ChatsController < ApplicationController + def index + room = Room.find_by(key: params[:room_key]) + if room.blank? + render json: { error: t("404 error") }, status: 404 + return + end + @chats = room.past_chats(10) + end +end diff --git a/app/views/api/v1/chats/index.json.jbuilder b/app/views/api/v1/chats/index.json.jbuilder new file mode 100644 index 0000000..6d86a54 --- /dev/null +++ b/app/views/api/v1/chats/index.json.jbuilder @@ -0,0 +1,3 @@ +json.chats @chats do |chat| + json.partial! "chats/chat", chat: chat +end diff --git a/config/routes.rb b/config/routes.rb index 8613735..c214e0c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,7 @@ get :recommend end end + resources :chats, only: [:index] get "joined_rooms", to: "users#joined_rooms" get "youtube/search", to: "youtube#search" get "youtube/video", to: "youtube#video" diff --git a/spec/requests/api/v1/chats_spec.rb b/spec/requests/api/v1/chats_spec.rb new file mode 100644 index 0000000..77e383e --- /dev/null +++ b/spec/requests/api/v1/chats_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" + +describe "chats" do + let(:room) { create(:room) } + + describe "GET /api/v1/chats" do + let(:params) { { room_key: room.key } } + let!(:chat) { create(:user_chat, room: room) } + + context "with valid params" do + it "returns a chat", :autodoc do + is_expected.to eq 200 + body = response.body + expect(body).to be_json_eql(chat.id).at_path("chats/0/id") + end + end + end +end From 36df01cc97a402188f7f12b1c5154fc7cbbff577 Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Mon, 29 Apr 2019 10:40:31 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/chats_controller.rb | 3 ++- app/models/chat.rb | 6 +++++- app/models/room.rb | 8 ++++++-- spec/models/room_spec.rb | 11 +++++++++++ spec/requests/api/v1/chats_spec.rb | 17 +++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/chats_controller.rb b/app/controllers/api/v1/chats_controller.rb index df193d3..b17a51a 100644 --- a/app/controllers/api/v1/chats_controller.rb +++ b/app/controllers/api/v1/chats_controller.rb @@ -5,6 +5,7 @@ def index render json: { error: t("404 error") }, status: 404 return end - @chats = room.past_chats(10) + cursor = Chat.find_by(id: params[:cursor]) + @chats = room.past_chats(10, cursor) end end diff --git a/app/models/chat.rb b/app/models/chat.rb index 6492af2..81cfc2a 100644 --- a/app/models/chat.rb +++ b/app/models/chat.rb @@ -7,6 +7,10 @@ class Chat < ApplicationRecord end scope :latest_by, ->(num) do - order(created_at: :desc).limit(num) + order(id: :desc).limit(num) + end + + scope :before, ->(cursor) do + where("id < ?", cursor) end end diff --git a/app/models/room.rb b/app/models/room.rb index c239cdb..3003926 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -56,8 +56,12 @@ def last_played_video videos.ended.order_by_end.last end - def past_chats(num) - chats.latest_by(num).reverse + def past_chats(num, cursor = nil) + if cursor.blank? + chats.latest_by(num).reverse + else + chats.before(cursor).latest_by(num).reverse + end end def online_users diff --git a/spec/models/room_spec.rb b/spec/models/room_spec.rb index 12b1cec..01ed0ef 100644 --- a/spec/models/room_spec.rb +++ b/spec/models/room_spec.rb @@ -141,6 +141,17 @@ } it { is_expected.to eq [chat2, chat3] } end + + context "with cursor" do + subject { room.past_chats 2, chat3.id } + + before { + chat1.save! + chat2.save! + chat3.save! + } + it { is_expected.to eq [chat1, chat2] } + end end describe "#online_users" do diff --git a/spec/requests/api/v1/chats_spec.rb b/spec/requests/api/v1/chats_spec.rb index 77e383e..32263aa 100644 --- a/spec/requests/api/v1/chats_spec.rb +++ b/spec/requests/api/v1/chats_spec.rb @@ -14,5 +14,22 @@ expect(body).to be_json_eql(chat.id).at_path("chats/0/id") end end + + context "with cursor" do + let!(:before_chat) { create(:user_chat, room: room) } + let(:params) { { room_key: room.key, cursor: before_chat.id } } + + it "returns a chat", :autodoc do + is_expected.to eq 200 + body = response.body + expect(body).to be_json_eql(chat.id).at_path("chats/0/id") + end + + it "dose not contain before_chat" do + is_expected.to eq 200 + body = response.body + expect(body).not_to have_json_path("chats/1/id") + end + end end end From 43b005bffe6811e7058c193b6d26194323ed39c4 Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Mon, 29 Apr 2019 10:44:52 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/channels/room_channel_spec.rb | 4 ++-- spec/requests/api/v1/chats_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/channels/room_channel_spec.rb b/spec/channels/room_channel_spec.rb index d8a957c..4aeaacd 100644 --- a/spec/channels/room_channel_spec.rb +++ b/spec/channels/room_channel_spec.rb @@ -195,8 +195,8 @@ before { chat.save! } it "expect to have broadcast with chat" do expect { subject }.to have_broadcasted_to(target).with { |data| - expect(data).to be_json_eql(%("#{chat.message}")).at_path("data/past_chats/0/message") - expect(data).to be_json_eql(user.id).at_path("data/past_chats/0/user/id") + expect(data).to be_json_eql(%("#{chat.message}")).at_path("data/past_chats/1/message") + expect(data).to be_json_eql(user.id).at_path("data/past_chats/1/user/id") } end end diff --git a/spec/requests/api/v1/chats_spec.rb b/spec/requests/api/v1/chats_spec.rb index 32263aa..1005eac 100644 --- a/spec/requests/api/v1/chats_spec.rb +++ b/spec/requests/api/v1/chats_spec.rb @@ -15,6 +15,18 @@ end end + context "without room_key" do + let(:params) { {} } + + it { is_expected.to eq 404 } + end + + context "with invalid room_key" do + let(:params) { { room_key: "invalid_key" } } + + it { is_expected.to eq 404 } + end + context "with cursor" do let!(:before_chat) { create(:user_chat, room: room) } let(:params) { { room_key: room.key, cursor: before_chat.id } } From e515c4afdb4085285b38bcf108144ca0e7ef1292 Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Mon, 29 Apr 2019 10:59:12 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Cursor=E3=81=8C=E7=A9=BA=E3=81=AE=E3=81=A8?= =?UTF-8?q?=E3=81=8D=E3=81=AE=E6=8C=99=E5=8B=95=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/chats_controller.rb | 13 +++++++++++-- spec/requests/api/v1/chats_spec.rb | 10 ++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/chats_controller.rb b/app/controllers/api/v1/chats_controller.rb index b17a51a..af1e06c 100644 --- a/app/controllers/api/v1/chats_controller.rb +++ b/app/controllers/api/v1/chats_controller.rb @@ -5,7 +5,16 @@ def index render json: { error: t("404 error") }, status: 404 return end - cursor = Chat.find_by(id: params[:cursor]) - @chats = room.past_chats(10, cursor) + + if params[:cursor].present? + cursor = Chat.find_by(id: params[:cursor]) + if cursor.blank? + render json: { error: t("404 error") }, status: 404 + return + end + @chats = room.past_chats(10, cursor) + else + @chats = room.past_chats(10) + end end end diff --git a/spec/requests/api/v1/chats_spec.rb b/spec/requests/api/v1/chats_spec.rb index 1005eac..e031905 100644 --- a/spec/requests/api/v1/chats_spec.rb +++ b/spec/requests/api/v1/chats_spec.rb @@ -27,8 +27,8 @@ it { is_expected.to eq 404 } end - context "with cursor" do - let!(:before_chat) { create(:user_chat, room: room) } + context "with valid cursor" do + let(:before_chat) { create(:user_chat, room: room) } let(:params) { { room_key: room.key, cursor: before_chat.id } } it "returns a chat", :autodoc do @@ -43,5 +43,11 @@ expect(body).not_to have_json_path("chats/1/id") end end + + context "with invalid cursor" do + let(:params) { { room_key: room.key, cursor: "invalid" } } + + it { is_expected.to eq 404 } + end end end