Commit 64cae55bd92e700dbf34c21c003973c6c2333e6b

Authored by Hugo Melo
1 parent d05742c0

Add endpoints for points by type, profile and others

lib/ext/person.rb
... ... @@ -12,4 +12,19 @@ class Person
12 12 # FIXME: FIND OUT A WAY TO CHECK EVERY REGISTRY FIELD
13 13 false
14 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 30 end
... ...
lib/gamification_plugin/api.rb
... ... @@ -12,6 +12,15 @@ class GamificationPlugin::API < Grape::API
12 12 get 'points' do
13 13 {points: current_person.points}
14 14 end
  15 + get 'points_by_type' do
  16 + {points: current_person.points_by_type(params[:type]) }
  17 + end
  18 + get 'points_by_profile' do
  19 + {points: current_person.points_by_profile(params[:profile]) }
  20 + end
  21 + get 'points_out_of_profiles' do
  22 + {points: current_person.points_out_of_profiles }
  23 + end
15 24  
16 25 end
17 26  
... ... @@ -28,6 +37,24 @@ class GamificationPlugin::API < Grape::API
28 37 {:points => person.points}
29 38 end
30 39  
  40 + get ':id/points_by_type' do
  41 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  42 + return not_found! if person.blank?
  43 + {points: person.points_by_type(params[:type]) }
  44 + end
  45 +
  46 + get ':id/points_by_profile' do
  47 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  48 + return not_found! if person.blank?
  49 + {points: person.points_by_type(params[:profile]) }
  50 + end
  51 +
  52 + get ':id/points_out_of_profiles' do
  53 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  54 + return not_found! if person.blank?
  55 + {points: person.points_out_of_profiles }
  56 + end
  57 +
31 58 get ':id/level' do
32 59 person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
33 60 return not_found! if person.blank?
... ...