diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 351b1c0d..b24f85fc 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true class MapsController < ApplicationController before_action :require_user, only: [:create, :update, :destroy, :access, :events, - :screenshot, :star, :unstar] + :screenshot] before_action :set_map, only: [:show, :update, :destroy, :access, :contains, - :events, :export, :screenshot, :star, :unstar] + :events, :export, :screenshot] after_action :verify_authorized autocomplete :map, :name, full: true, extra_data: [:user_id] @@ -150,29 +150,6 @@ class MapsController < ApplicationController 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 def set_map diff --git a/app/controllers/stars_controller.rb b/app/controllers/stars_controller.rb new file mode 100644 index 00000000..ec4d1347 --- /dev/null +++ b/app/controllers/stars_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 3ad59cb2..b114d3d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,14 +48,17 @@ Metamaps::Application.routes.draw do get 'topics/:id/relative_numbers', to: 'topics#relative_numbers', as: :relative_numbers get 'topics/:id/relatives', to: 'topics#relatives', as: :relatives - resources :maps, except: [:index, :edit] - get 'maps/:id/export', to: 'maps#export' - post 'maps/:id/events/:event', to: 'maps#events' - get 'maps/:id/contains', to: 'maps#contains', as: :contains - post 'maps/:id/upload_screenshot', to: 'maps#screenshot', as: :screenshot - post 'maps/:id/access', to: 'maps#access', as: :access, defaults: { format: :json } - post 'maps/:id/star', to: 'maps#star', defaults: { format: :json } - post 'maps/:id/unstar', to: 'maps#unstar', defaults: { format: :json } + resources :maps, except: [:index, :edit] do + member do + get :export + post 'events/:event', action: :events + get :contains + post :upload_screenshot, action: :screenshot + post :access, default: { format: :json } + post :star, to: 'stars#create', defaults: { format: :json } + post :unstar, to: 'stars#destroy', defaults: { format: :json } + end + end namespace :explore do get 'active'