scinote-web/app/controllers/client_api/users/users_controller.rb

99 lines
2.7 KiB
Ruby
Raw Normal View History

module ClientApi
module Users
class UsersController < ApplicationController
2017-09-14 17:46:49 +08:00
def preferences_info
settings = current_user.settings
2017-09-14 17:46:49 +08:00
respond_to do |format|
format.json do
render template: 'client_api/users/preferences',
status: :ok,
locals: {
timeZone: settings['time_zone'],
notifications: settings['notifications']
}
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
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 }
end
end
end
2017-09-21 19:42:49 +08:00
def update
2017-09-26 23:29:11 +08:00
user_service = ClientApi::UserService.new(
current_user: current_user,
params: user_params
)
if user_service.update_user!
bypass_sign_in(current_user)
success_response
else
2017-09-27 15:17:09 +08:00
unsuccess_response(current_user.errors.full_messages, :unauthorized)
2017-09-22 22:59:38 +08:00
end
2017-09-26 23:29:11 +08:00
rescue CustomUserError => error
unsuccess_response(error.to_s)
2017-09-21 19:42:49 +08:00
end
2017-09-04 20:56:03 +08:00
private
def user_params
2017-09-26 23:29:11 +08:00
params.require(:user)
.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-09-04 20:56:03 +08:00
end
2017-09-26 23:29:11 +08:00
def success_response(template = nil, locals = nil)
respond_to do |format|
format.json do
if template && locals
render template: template, status: :ok, locals: locals
else
render json: {}, status: :ok
end
end
end
end
2017-09-27 15:17:09 +08:00
def unsuccess_response(message, status = :unprocessable_entity)
2017-09-26 23:29:11 +08:00
respond_to do |format|
format.json do
render json: { message: message },
2017-09-27 15:17:09 +08:00
status: status
2017-09-26 23:29:11 +08:00
end
end
end
end
end
end