scinote-web/app/controllers/client_api/teams/teams_controller.rb

122 lines
3.8 KiB
Ruby
Raw Normal View History

2017-08-30 15:29:45 +08:00
module ClientApi
module Teams
class TeamsController < ApplicationController
include ClientApi::Users::UserTeamsHelper
before_action :check_update_team_permission, only: :update
2018-01-09 22:28:27 +08:00
before_action :check_create_team_permission, only: :create
2017-08-30 15:29:45 +08:00
def index
teams = current_user.datatables_teams
success_response(template: '/client_api/teams/index',
locals: { teams: teams })
2017-08-30 15:29:45 +08:00
end
def create
service = ClientApi::Teams::CreateService.new(
current_user: current_user,
params: team_params
)
result = service.execute
if result[:status] == :success
success_response(details: { id: service.team.id })
else
error_response(
message: result[:message],
details: service.team.errors
)
end
end
2017-08-30 15:29:45 +08:00
def details
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
2017-08-30 15:29:45 +08:00
current_user: current_user)
success_response(template: '/client_api/teams/details',
locals: teams_service.team_page_details_data)
2017-08-30 22:18:21 +08:00
rescue ClientApi::CustomTeamError
2017-08-30 15:29:45 +08:00
error_response
end
def change_team
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
2017-08-30 15:29:45 +08:00
current_user: current_user)
teams_service.change_current_team!
success_response(template: '/client_api/teams/index',
locals: teams_service.teams_data)
2017-08-30 22:18:21 +08:00
rescue ClientApi::CustomTeamError
2017-08-30 15:29:45 +08:00
error_response
end
def update
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user,
params: team_params)
teams_service.update_team!
success_response(template: '/client_api/teams/update_details',
locals: teams_service.single_team_details_data)
rescue ClientApi::CustomTeamError => error
error_response(message: error.to_s)
end
2017-11-06 22:14:17 +08:00
def current_team
success_response(template: '/client_api/teams/current_team',
locals: { team: current_user.current_team })
end
2017-08-30 15:29:45 +08:00
private
def team_params
params.require(:team).permit(:name, :description)
end
2018-01-09 22:28:27 +08:00
def check_create_team_permission
unless can_create_teams?
respond_422(t('client_api.teams.create_permission_error'))
end
end
def check_update_team_permission
@team = Team.find_by_id(params[:team_id])
unless can_update_team?(@team)
2018-01-09 22:28:27 +08:00
respond_422(t('client_api.teams.update_permission_error'))
end
end
def success_response(args = {})
template = args.fetch(:template) { nil }
locals = args.fetch(:locals) { {} }
details = args.fetch(:details) { {} }
2017-08-30 15:29:45 +08:00
respond_to do |format|
format.json do
if template
render template: template,
status: :ok,
locals: locals
else
render json: { details: details }, status: :ok
end
2017-08-30 15:29:45 +08:00
end
end
end
2017-09-20 16:53:38 +08:00
def error_response(args = {})
message = args.fetch(:message) { t('client_api.generic_error_message') }
details = args.fetch(:details) { {} }
status = args.fetch(:status) { :unprocessable_entity }
2017-08-30 15:29:45 +08:00
respond_to do |format|
format.json do
render json: {
message: message,
details: details
},
status: status
2017-08-30 15:29:45 +08:00
end
end
end
end
end
end