Commit 68e9824794fc1e3dfb42a9f32184eeffa3c3dcdc
1 parent
3d62563c
Exists in
master
and in
1 other branch
Add endpoints for points by type, profile and others
Showing
2 changed files
with
49 additions
and
0 deletions
Show diff stats
lib/ext/person.rb
| @@ -12,4 +12,19 @@ class Person | @@ -12,4 +12,19 @@ class Person | ||
| 12 | # FIXME: FIND OUT A WAY TO CHECK EVERY REGISTRY FIELD | 12 | # FIXME: FIND OUT A WAY TO CHECK EVERY REGISTRY FIELD |
| 13 | false | 13 | false |
| 14 | end | 14 | end |
| 15 | + | ||
| 16 | + def points_by_type type | ||
| 17 | + categorizations = GamificationPlugin::PointsCategorization.by_type(type) | ||
| 18 | + categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } | ||
| 19 | + end | ||
| 20 | + | ||
| 21 | + def points_by_profile profile | ||
| 22 | + categorizations = GamificationPlugin::PointsCategorization.by_profile(profile) | ||
| 23 | + categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + def points_out_of_profiles | ||
| 27 | + categorizations = GamificationPlugin::PointsCategorization.where(profile_id: nil) | ||
| 28 | + categorizations.inject(0) { |sum, c| sum += self.points(category: c.id.to_s) } | ||
| 29 | + end | ||
| 15 | end | 30 | end |
lib/gamification_plugin/api.rb
| @@ -11,6 +11,22 @@ class GamificationPlugin::API < Grape::API | @@ -11,6 +11,22 @@ class GamificationPlugin::API < Grape::API | ||
| 11 | authenticate! | 11 | authenticate! |
| 12 | present current_person.badges, :with => Noosfero::API::Entities::Badge | 12 | present current_person.badges, :with => Noosfero::API::Entities::Badge |
| 13 | end | 13 | end |
| 14 | + get 'points' do | ||
| 15 | + authenticate! | ||
| 16 | + {points: current_person.points} | ||
| 17 | + end | ||
| 18 | + get 'points_by_type' do | ||
| 19 | + authenticate! | ||
| 20 | + {points: current_person.points_by_type(params[:type]) } | ||
| 21 | + end | ||
| 22 | + get 'points_by_profile' do | ||
| 23 | + authenticate! | ||
| 24 | + {points: current_person.points_by_profile(params[:profile]) } | ||
| 25 | + end | ||
| 26 | + get 'points_out_of_profiles' do | ||
| 27 | + authenticate! | ||
| 28 | + {points: current_person.points_out_of_profiles } | ||
| 29 | + end | ||
| 14 | 30 | ||
| 15 | get 'level' do | 31 | get 'level' do |
| 16 | authenticate! | 32 | authenticate! |
| @@ -36,6 +52,24 @@ class GamificationPlugin::API < Grape::API | @@ -36,6 +52,24 @@ class GamificationPlugin::API < Grape::API | ||
| 36 | {:points => person.points} | 52 | {:points => person.points} |
| 37 | end | 53 | end |
| 38 | 54 | ||
| 55 | + get ':id/points_by_type' do | ||
| 56 | + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | ||
| 57 | + return not_found! if person.blank? | ||
| 58 | + {points: person.points_by_type(params[:type]) } | ||
| 59 | + end | ||
| 60 | + | ||
| 61 | + get ':id/points_by_profile' do | ||
| 62 | + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | ||
| 63 | + return not_found! if person.blank? | ||
| 64 | + {points: person.points_by_type(params[:profile]) } | ||
| 65 | + end | ||
| 66 | + | ||
| 67 | + get ':id/points_out_of_profiles' do | ||
| 68 | + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | ||
| 69 | + return not_found! if person.blank? | ||
| 70 | + {points: person.points_out_of_profiles } | ||
| 71 | + end | ||
| 72 | + | ||
| 39 | get ':id/level' do | 73 | get ':id/level' do |
| 40 | person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) | 74 | person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) |
| 41 | return not_found! if person.blank? | 75 | return not_found! if person.blank? |