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

Inherits:
BaseController
  • Object
show all
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

Instance Method Details

#create_profileObject



6
7
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
# File 'app/controllers/usman/api/v1/profile_controller.rb', line 6

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]

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

        if @user.valid?
          @user.save
          @current_registration.user = @user
          
          @current_registration.country = @country if @country
          @current_registration.city = @city if @city

          @current_registration.save

          @current_user = @user
          @success = true
          @alert = {
            heading: I18n.translate("api.profile.profile_created.heading"),
            message: I18n.translate("api.profile.profile_created.message")
          }
          @data = { user: @user }
        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

#update_profileObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'app/controllers/usman/api/v1/profile_controller.rb', line 62

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.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])

        if @user.valid?
          @user.save
          
          @current_registration.country = @country if @country
          @current_registration.city = @city if @city

          @current_registration.save

          @success = true
          @alert = {
            heading: I18n.translate("api.profile.profile_created.heading"),
            message: I18n.translate("api.profile.profile_created.message")
          }
          @data = { user: @user }
        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