Class: Usman::Api::V1::ProfileController

Inherits:
BaseController
  • Object
show all
Includes:
ImageUploaderHelper
Defined in:
app/controllers/usman/api/v1/profile_controller.rb

Instance Method Summary collapse

Methods included from Usman::ApiHelper

#current_device, #current_user, #require_admin_auth_token, #require_api_token, #require_auth_token, #require_super_admin_auth_token, #store_last_accessed_api

Instance Method Details

#create_profileObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/usman/api/v1/profile_controller.rb', line 8

def create_profile
  proc_code = Proc.new do
    if @current_registration
      if @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_already_exists.heading"),
          message: I18n.translate("api.profile.user_already_exists.message")
        }
      else
        @user = User.new
        @user.name = permitted_params[:name]
        @user.generate_username_and_password
        @user.gender = permitted_params[:gender]
        @user.date_of_birth = permitted_params[:date_of_birth]
        @user.email = permitted_params[:email]
        
        @user.country = Country.find_by_id(permitted_params[:country_id])
        @user.city = City.find_by_id(permitted_params[:city_id])

        if @user.valid?
          @user.save
          @user.approve!
          @current_registration.user = @user
          @current_registration.save

          # Saving the profile image if passed
          begin
            if params[:image] && !params[:image].blank?
              user_image = @user.profile_picture || Image::ProfilePicture.new(imageable: @user)
              user_image.image = params[:image]
              user_image.valid?
              if user_image.errors.any?
                @errors = {
                  heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
                  message: I18n.translate("api.profile_picture.image_save_failed.message"),
                  details: user_image.errors.full_messages
                }
              else
                user_image.save
                @user.reload
              end
            end
          rescue
            @errors = {
              heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
              message: I18n.translate("api.profile_picture.image_save_failed.message"),
              details: user_image.errors.full_messages
            }
          end

          @current_user = @user
          @success = true
          @alert = {
            heading: I18n.translate("api.profile.profile_created.heading"),
            message: I18n.translate("api.profile.profile_created.message")
          }
          @data = ActiveModelSerializers::SerializableResource.new(@user, serializer: ProfileSerializer)
        else
          @success = false
          @errors = {
            heading: I18n.translate("api.profile.invalid_inputs.heading"),
            message: I18n.translate("api.profile.invalid_inputs.message"),
            details: @user.errors
          }
        end
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end

#profile_infoObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/usman/api/v1/profile_controller.rb', line 165

def profile_info
  proc_code = Proc.new do
    if @current_registration
      unless @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_does_not_exists.heading"),
          message: I18n.translate("api.profile.user_does_not_exists.message")
        }
      else
        @current_user
        @success = true
        @data = ActiveModelSerializers::SerializableResource.new(@current_user, serializer: ProfileSerializer)
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end

#update_profileObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/usman/api/v1/profile_controller.rb', line 86

def update_profile
  proc_code = Proc.new do
    if @current_registration
      unless @current_user
        @success = false
        @errors = {
          heading: I18n.translate("api.profile.user_does_not_exists.heading"),
          message: I18n.translate("api.profile.user_does_not_exists.message")
        }
      else
        @user = @current_user
        @user.name = permitted_params[:name]
        @user.gender = permitted_params[:gender]
        @user.dummy = false
        @user.date_of_birth = permitted_params[:date_of_birth]
        @user.email = permitted_params[:email]

        @country = Country.find_by_id(permitted_params[:country_id])
        @city = City.find_by_id(permitted_params[:city_id])

        @user.country = @country if @country
        @user.city = @city if @city

        if @user.valid?
          @user.dummy = false
          @user.save
          @current_registration.save

          # Saving the profile image if passed
          begin
            if params[:image] && !params[:image].blank?
              user_image = @user.profile_picture || Image::ProfilePicture.new(imageable: @user)
              user_image.image = params[:image]
              user_image.valid?
              if user_image.errors.any?
                @errors = {
                  heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
                  message: I18n.translate("api.profile_picture.image_save_failed.message"),
                  details: user_image.errors.full_messages
                }
              else
                user_image.save
                @user.reload
              end
            end
          rescue
            @errors = {
              heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
              message: I18n.translate("api.profile_picture.image_save_failed.message"),
              details: user_image.errors.full_messages
            }
          end

          @success = true
          @alert = {
            heading: I18n.translate("api.profile.profile_created.heading"),
            message: I18n.translate("api.profile.profile_created.message")
          }
          @data = ActiveModelSerializers::SerializableResource.new(@user, serializer: ProfileSerializer)
        else
          @success = false
          @errors = {
            heading: I18n.translate("api.profile.invalid_inputs.heading"),
            message: I18n.translate("api.profile.invalid_inputs.message"),
            details: @user.errors
          }
        end
      end
    else
      @success = false
      @errors = {
        heading: I18n.translate("api.profile.registration_details_missing.heading"),
        message: I18n.translate("api.profile.registration_details_missing.message")
      }
    end
  end
  render_json_response(proc_code)
end