2017-08-30 15:29:45 +08:00
|
|
|
module ClientApi
|
|
|
|
module Teams
|
|
|
|
class TeamsController < ApplicationController
|
|
|
|
include ClientApi::Users::UserTeamsHelper
|
|
|
|
|
|
|
|
def index
|
2017-10-26 22:59:00 +08:00
|
|
|
teams = current_user.datatables_teams
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/index',
|
2017-10-26 22:59:00 +08:00
|
|
|
locals: { teams: teams })
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
|
2017-09-18 15:13:37 +08:00
|
|
|
def create
|
2017-09-19 16:37:21 +08:00
|
|
|
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
|
2017-09-18 15:13:37 +08:00
|
|
|
end
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
def details
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-08-30 15:29:45 +08:00
|
|
|
current_user: current_user)
|
2017-09-19 16:37:21 +08:00
|
|
|
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
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-08-30 15:29:45 +08:00
|
|
|
current_user: current_user)
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service.change_current_team!
|
2017-09-19 16:37:21 +08:00
|
|
|
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
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def update
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-09-12 23:30:13 +08:00
|
|
|
current_user: current_user,
|
|
|
|
params: team_params)
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service.update_team!
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/update_details',
|
|
|
|
locals: teams_service.single_team_details_data)
|
2017-09-12 23:30:13 +08:00
|
|
|
rescue ClientApi::CustomTeamError => error
|
2017-09-19 16:37:21 +08:00
|
|
|
error_response(message: error.to_s)
|
2017-09-12 23:30:13 +08:00
|
|
|
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
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def team_params
|
2017-09-18 15:13:37 +08:00
|
|
|
params.require(:team).permit(:name, :description)
|
2017-09-12 23:30:13 +08:00
|
|
|
end
|
|
|
|
|
2017-09-19 16:37:21 +08:00
|
|
|
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
|
2017-09-19 16:37:21 +08:00
|
|
|
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 = {})
|
2017-09-19 16:37:21 +08:00
|
|
|
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
|
2017-09-19 16:37:21 +08:00
|
|
|
render json: {
|
|
|
|
message: message,
|
|
|
|
details: details
|
|
|
|
},
|
|
|
|
status: status
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|