2017-08-25 14:21:52 +08:00
|
|
|
module ClientApi
|
|
|
|
module Users
|
|
|
|
class UsersController < ApplicationController
|
2017-10-05 16:07:50 +08:00
|
|
|
def sign_out_user
|
|
|
|
respond_to do |format|
|
|
|
|
if sign_out current_user
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
else
|
|
|
|
format.json { render json: {}, status: :unauthorized }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-14 17:46:49 +08:00
|
|
|
def preferences_info
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render template: 'client_api/users/preferences',
|
|
|
|
status: :ok,
|
2017-12-07 20:59:33 +08:00
|
|
|
locals: { user: current_user }
|
2017-09-14 17:46:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def profile_info
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render template: '/client_api/users/profile',
|
|
|
|
status: :ok,
|
|
|
|
locals: { user: current_user }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def statistics_info
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render template: '/client_api/users/statistics',
|
|
|
|
status: :ok,
|
|
|
|
locals: { user: current_user }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-25 14:21:52 +08:00
|
|
|
def current_user_info
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render template: '/client_api/users/show',
|
2017-09-14 17:46:49 +08:00
|
|
|
status: :ok,
|
|
|
|
locals: { user: current_user }
|
2017-08-25 14:21:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-25 20:45:02 +08:00
|
|
|
|
2017-09-21 19:42:49 +08:00
|
|
|
def update
|
2017-10-23 23:45:57 +08:00
|
|
|
service = ClientApi::Users::UpdateService.new(
|
2017-09-26 23:29:11 +08:00
|
|
|
current_user: current_user,
|
|
|
|
params: user_params
|
|
|
|
)
|
2017-10-23 23:45:57 +08:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2017-09-26 23:29:11 +08:00
|
|
|
bypass_sign_in(current_user)
|
|
|
|
success_response
|
2017-08-31 17:23:44 +08:00
|
|
|
else
|
2017-10-23 23:45:57 +08:00
|
|
|
error_response(
|
|
|
|
message: result[:message],
|
|
|
|
details: service.user.errors
|
|
|
|
)
|
2017-08-31 17:23:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-04 20:56:03 +08:00
|
|
|
private
|
2017-08-26 20:07:31 +08:00
|
|
|
|
2017-09-04 20:56:03 +08:00
|
|
|
def user_params
|
2017-09-26 23:29:11 +08:00
|
|
|
params.require(:user)
|
2017-09-28 22:53:51 +08:00
|
|
|
.permit(:password, :initials, :email, :full_name,
|
|
|
|
:password_confirmation, :current_password, :avatar,
|
2017-10-05 19:56:56 +08:00
|
|
|
:time_zone, :assignments_notification,
|
|
|
|
:assignments_email_notification, :recent_notification,
|
|
|
|
:recent_email_notification,
|
|
|
|
:system_message_email_notification)
|
2017-08-26 20:07:31 +08:00
|
|
|
end
|
|
|
|
|
2017-10-23 23:45:57 +08:00
|
|
|
def success_response(args = {})
|
|
|
|
template = args.fetch(:template) { nil }
|
|
|
|
locals = args.fetch(:locals) { {} }
|
|
|
|
details = args.fetch(:details) { {} }
|
|
|
|
|
2017-08-25 20:45:02 +08:00
|
|
|
respond_to do |format|
|
2017-09-26 23:29:11 +08:00
|
|
|
format.json do
|
2017-10-23 23:45:57 +08:00
|
|
|
if template
|
|
|
|
render template: template,
|
|
|
|
status: :ok,
|
|
|
|
locals: locals
|
2017-09-26 23:29:11 +08:00
|
|
|
else
|
2017-10-23 23:45:57 +08:00
|
|
|
render json: { details: details }, status: :ok
|
2017-09-26 23:29:11 +08:00
|
|
|
end
|
|
|
|
end
|
2017-08-25 20:45:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-23 23:45:57 +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-25 20:45:02 +08:00
|
|
|
respond_to do |format|
|
2017-09-26 23:29:11 +08:00
|
|
|
format.json do
|
2017-10-23 23:45:57 +08:00
|
|
|
render json: {
|
|
|
|
message: message,
|
|
|
|
details: details
|
|
|
|
},
|
2017-09-27 15:17:09 +08:00
|
|
|
status: status
|
2017-09-04 20:56:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-25 14:21:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|