Class: Api::V1::UsersController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 20

def create
  respond_to do |format|
    user = User.new user_params
    if user.save
      user_token = user.user_tokens.where(installation_identifier: user_token_params[:installation_identifier]).first_or_create
      if user_token.persisted?
        format.json do
          render json: {user_token: user_token_hash(user_token, user: true)}, status: :created
        end
      else
        format.json do
          render json: {errors: user_token.errors}, status: :unprocessable_entity
        end
      end
    else
      format.json do
        render json: {errors: user.errors}, status: :unprocessable_entity
      end
    end
  end
end

#forgot_passwordObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 62

def forgot_password
  respond_to do |format|
    user = FormUser.where(email: user_params[:email]).first
    format.json do
      if user.present?
        user.send_reset_password_instructions
        user.save
        render json: {user: user_hash(user).slice(:email)}, status: :ok
      else
        render json: {errors: {email: 'not found'}}, status: :not_found
      end
    end
  end
end

#reset_passwordObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 77

def reset_password
  respond_to do |format|
    user = User.reset_password_by_token user_params.slice(:reset_password_token, :password, :password_confirmation)
    format.json do
      if user.present?
        if user.errors.empty?
          render json: {user: user_hash(user)}, status: :ok
        else
          p user.errors
          render json: {errors: user.errors}, status: :unprocessable_entity
        end
      else
        render json: {errors: {email: 'not found'}}, status: :not_found
      end
    end
  end
end

#showObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 6

def show
  respond_to do |format|
    if @user.present?
      format.json do
        render json: {user: user_hash(@user)}, status: :ok
      end
    else
      format.json do
        render json: {errors: {id: 'not found'}}, status: :not_found
      end
    end
  end
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 42

def update
  respond_to do |format|
    if @user.present?
      if @user.update(user_params)
        format.json do
          render json: {user: user_hash(@user)}, status: :ok
        end
      else
        format.json do
          render json: {errors: @user.errors}, status: :unprocessable_entity
        end
      end
    else
      format.json do
        render json: {errors: {id: 'not found'}}, status: :not_found
      end
    end
  end
end