cirandas.net

ref: master

app/api/v1/profiles.rb


 1
 2
 3
 4
 5
 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
61
62
63
64
module Api
  module V1
    class Profiles < Grape::API

      resource :profiles do

        get do
          profiles = select_filtered_collection_of(environment, 'profiles', params)
          profiles = profiles.visible
          profiles = profiles.by_location(params) # Must be the last. May return Exception obj.
          present profiles, :with => Entities::Profile, :current_person => current_person
        end

        get ':id', requirements: { id: /#{Noosfero.identifier_format}/ } do
          profiles = environment.profiles
          profiles = profiles.visible
          key = params[:key].to_s == "identifier" ? :identifier : :id

          profile = profiles.find_by key => params[:id]

          if profile
            type_map = {
              Person => Entities::Person,
              Community => Entities::Community,
              Enterprise => Entities::Enterprise
            }[profile.class] || Entities::Profile

            present profile, :with => type_map, :current_person => current_person
          else
            not_found!
          end
        end

        desc "Update profile information"
        post ':id' do
          authenticate!
          profile = environment.profiles.find_by(id: params[:id])
          return forbidden! unless profile.allow_edit?(current_person)
          begin
            profile_params = asset_with_image(params[:profile])
            profile.update_attributes!(asset_with_custom_image(:top_image, profile_params))
            present profile, :with => Entities::Profile, :current_person => current_person
          rescue ActiveRecord::RecordInvalid
            render_model_errors!(profile.errors)
          end
        end

        delete ':id' do
          authenticate!
          profiles = environment.profiles
          profile = profiles.find_by id: params[:id]

          not_found! if profile.blank?

          if profile.allow_destroy?(current_person)
            present({ success: profile.destroy })
          else
            forbidden!
          end
        end
      end
    end
  end
end