Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #49 from enpitut2018/feature/preload
Browse files Browse the repository at this point in the history
implement #44 backend API
  • Loading branch information
ishowta authored Oct 26, 2018
2 parents f20f6dc + e04805f commit 3d2cdb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Api::V1::EntriesController < ApplicationController

# GET /entries/1
def show
render json: @entry, include: [ bookmarks: :user ]
render json: @entry, include: ['bookmarks', 'bookmarks.user']
end

# PATCH/PUT /entries/1
Expand Down
29 changes: 17 additions & 12 deletions app/controllers/api/v1/trend_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
class Api::V1::TrendController < ApplicationController
before_action :set_trend, only: [:index]
before_action :set_trend, only: [:index, :preload]

# GET /trend
# GET /trend/:page
def index
render json: @trend
#p @trend
render json: @trend, include: ''
end

# GET /trend/:page/preload
def preload
render json: @trend, include: 'bookmarks.user'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_trend

Entry.find_each do |entry|
entry[:num_of_bookmarked] = entry.count_bookmarks
end
@trend=Entry.all.reorder!(num_of_bookmarked: :desc)
@trend = Entry
.page(trend_params[:page])
.includes(bookmarks: :user)
.each{|e| e[:num_of_bookmarked] = e.bookmarks.size}
.sort_by(&:num_of_bookmarked)
.reverse
end

# Only allow a trusted parameter "white list" through.
#def trend_params
# params.fetch(:entry, {}).permit(:page)
#end
def trend_params
params.permit(:page)
end
end
4 changes: 2 additions & 2 deletions app/serializers/entry_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class EntrySerializer < ActiveModel::Serializer
attributes :id, :url, :title, :thumbnail_url, :num_of_bookmarked, :bookmarks
attributes :id, :url, :title, :thumbnail_url, :num_of_bookmarked
has_many :bookmarks
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace :v1 do
resources :bookmarks
resources :entries, :only => :show
get "/trend", to: "trend#index"
get "/trend/:page", to: "trend#index"
get "/trend/:page/preload", to: "trend#preload"
post "/entries/:entry_id", to: "bookmarks#create_by_entry_id"

mount_devise_token_auth_for 'User', at: 'auth',
Expand Down

0 comments on commit 3d2cdb1

Please sign in to comment.