factor stars into their own controller

This commit is contained in:
Devin Howard 2016-09-26 00:50:52 +08:00
parent 3ee8d41298
commit 5065655436
3 changed files with 50 additions and 33 deletions

View file

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class MapsController < ApplicationController class MapsController < ApplicationController
before_action :require_user, only: [:create, :update, :destroy, :access, :events, before_action :require_user, only: [:create, :update, :destroy, :access, :events,
:screenshot, :star, :unstar] :screenshot]
before_action :set_map, only: [:show, :update, :destroy, :access, :contains, before_action :set_map, only: [:show, :update, :destroy, :access, :contains,
:events, :export, :screenshot, :star, :unstar] :events, :export, :screenshot]
after_action :verify_authorized after_action :verify_authorized
autocomplete :map, :name, full: true, extra_data: [:user_id] autocomplete :map, :name, full: true, extra_data: [:user_id]
@ -150,29 +150,6 @@ class MapsController < ApplicationController
end end
end end
# POST maps/:id/star
def star
Star.find_or_create_by(map_id: @map.id, user_id: current_user.id)
respond_to do |format|
format.json do
render json: { message: 'Successfully starred map' }
end
end
end
# POST maps/:id/unstar
def unstar
star = Star.find_by(map_id: @map.id, user_id: current_user.id)
star&.delete
respond_to do |format|
format.json do
render json: { message: 'Successfully unstarred map' }
end
end
end
private private
def set_map def set_map

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
class StarsController < ApplicationController
before_action :require_user
before_action :set_map
after_action :verify_authorized
# POST maps/:id/star
def create
authorize @map, :star?
Star.find_or_create_by(map_id: @map.id, user_id: current_user.id)
respond_to do |format|
format.json do
render json: { message: 'Successfully starred map' }
end
end
end
# POST maps/:id/unstar
def destroy
authorize @map, :unstar?
star = Star.find_by(map_id: @map.id, user_id: current_user.id)
star&.delete
respond_to do |format|
format.json do
render json: { message: 'Successfully unstarred map' }
end
end
end
private
def set_map
@map = Map.find(params[:id])
end
end

View file

@ -48,14 +48,17 @@ Metamaps::Application.routes.draw do
get 'topics/:id/relative_numbers', to: 'topics#relative_numbers', as: :relative_numbers get 'topics/:id/relative_numbers', to: 'topics#relative_numbers', as: :relative_numbers
get 'topics/:id/relatives', to: 'topics#relatives', as: :relatives get 'topics/:id/relatives', to: 'topics#relatives', as: :relatives
resources :maps, except: [:index, :edit] resources :maps, except: [:index, :edit] do
get 'maps/:id/export', to: 'maps#export' member do
post 'maps/:id/events/:event', to: 'maps#events' get :export
get 'maps/:id/contains', to: 'maps#contains', as: :contains post 'events/:event', action: :events
post 'maps/:id/upload_screenshot', to: 'maps#screenshot', as: :screenshot get :contains
post 'maps/:id/access', to: 'maps#access', as: :access, defaults: { format: :json } post :upload_screenshot, action: :screenshot
post 'maps/:id/star', to: 'maps#star', defaults: { format: :json } post :access, default: { format: :json }
post 'maps/:id/unstar', to: 'maps#unstar', defaults: { format: :json } post :star, to: 'stars#create', defaults: { format: :json }
post :unstar, to: 'stars#destroy', defaults: { format: :json }
end
end
namespace :explore do namespace :explore do
get 'active' get 'active'