Class: Usman::Api::V1::ProfilePictureController

Inherits:
BaseController
  • Object
show all
Includes:
ImageUploaderHelper
Defined in:
app/controllers/usman/api/v1/profile_picture_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

#destroy_profile_pictureObject



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
164
165
166
167
168
169
170
# File 'app/controllers/usman/api/v1/profile_picture_controller.rb', line 126

def destroy_profile_picture
	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_image = @current_user.profile_picture
            unless user_image
             @success = false
             @errors = {
               heading: I18n.translate("api.profile_picture.profile_picture_does_not_exists.heading"),
              	message: I18n.translate("api.profile_picture.profile_picture_does_not_exists.message")
             }
           else
		      begin
		      	@success = true
		      	@alert = {
               heading: I18n.translate("api.profile_picture.image_deleted.heading"),
               message: I18n.translate("api.profile_picture.image_deleted.message")
             }
						user_image.destroy
		    	rescue Exception => e
		    		@success = false
						@errors = {
							heading: I18n.translate("api.profile_picture.image_delete_failed.heading"),
							message: I18n.translate("api.profile_picture.image_delete_failed.message"),
							details: "Unable to parse the base64 image data"
						}
		    	end
		    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_pictureObject



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
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/usman/api/v1/profile_picture_controller.rb', line 67

def profile_picture
	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_image = @current_user.profile_picture || Image::ProfilePicture.new(imageable: @current_user)
	      begin
	      	if params[:image] && !params[:image].blank?
		      	user_image.image = params[:image]
		      	user_image.valid?
			      if user_image.errors.any?
			      	@success = false
							@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
			      	@success = true
			      	@alert = {
                heading: I18n.translate("api.profile_picture.image_saved.heading"),
                message: I18n.translate("api.profile_picture.image_saved.message")
              }
							@data = ActiveModelSerializers::SerializableResource.new(user_image, serializer: ProfilePictureSerializer)
			      	user_image.save
			      end
			    else
			    	@success = false
							@errors = {
								heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
								message: I18n.translate("api.profile_picture.image_save_failed.message"),
								details: "Image can't be blank"
							}
			    end
	    	rescue Exception => e
	    		@success = false
					@errors = {
						heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
						message: I18n.translate("api.profile_picture.image_save_failed.message"),
						details: "Unable to parse the base64 image data"
					}
	    	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_picture_base64Object



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

def profile_picture_base64
	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_image = @current_user.profile_picture || Image::ProfilePicture.new(imageable: @current_user)
      	begin
	      	if params[:image] && !params[:image].blank?
		      	user_image.image = image_params[:image_path]
		      	user_image.valid?
			      if user_image.errors.any?
			      	@success = false
							@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
			      	@success = true
			      	@alert = {
                heading: I18n.translate("api.profile_picture.image_saved.heading"),
                message: I18n.translate("api.profile_picture.image_saved.message")
              }
							@data = ActiveModelSerializers::SerializableResource.new(user_image, serializer: ProfilePictureSerializer)
			      	user_image.save
			      end
			    else
			    	@success = false
							@errors = {
								heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
								message: I18n.translate("api.profile_picture.image_save_failed.message"),
								details: "Image can't be blank"
							}
			    end
	    	rescue Exception => e
	    		@success = false
					@errors = {
						heading: I18n.translate("api.profile_picture.image_save_failed.heading"),
						message: I18n.translate("api.profile_picture.image_save_failed.message"),
						details: "Unable to parse the base64 image data"
					}
	    	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