From 68e9824794fc1e3dfb42a9f32184eeffa3c3dcdc Mon Sep 17 00:00:00 2001 From: Hugo Melo Date: Sat, 26 Sep 2015 18:05:38 -0300 Subject: [PATCH] Add endpoints for points by type, profile and others --- lib/ext/person.rb | 15 +++++++++++++++ lib/gamification_plugin/api.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 0 deletions(-) diff --git a/lib/ext/person.rb b/lib/ext/person.rb index c7a56c2..f246da2 100644 --- a/lib/ext/person.rb +++ b/lib/ext/person.rb @@ -12,4 +12,19 @@ class Person # FIXME: FIND OUT A WAY TO CHECK EVERY REGISTRY FIELD false end + + def points_by_type type + categorizations = GamificationPlugin::PointsCategorization.by_type(type) + categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } + end + + def points_by_profile profile + categorizations = GamificationPlugin::PointsCategorization.by_profile(profile) + categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } + end + + def points_out_of_profiles + categorizations = GamificationPlugin::PointsCategorization.where(profile_id: nil) + categorizations.inject(0) { |sum, c| sum += self.points(category: c.id.to_s) } + end end diff --git a/lib/gamification_plugin/api.rb b/lib/gamification_plugin/api.rb index 757ea9b..6d74dcf 100644 --- a/lib/gamification_plugin/api.rb +++ b/lib/gamification_plugin/api.rb @@ -11,6 +11,22 @@ class GamificationPlugin::API < Grape::API authenticate! present current_person.badges, :with => Noosfero::API::Entities::Badge end + get 'points' do + authenticate! + {points: current_person.points} + end + get 'points_by_type' do + authenticate! + {points: current_person.points_by_type(params[:type]) } + end + get 'points_by_profile' do + authenticate! + {points: current_person.points_by_profile(params[:profile]) } + end + get 'points_out_of_profiles' do + authenticate! + {points: current_person.points_out_of_profiles } + end get 'level' do authenticate! @@ -36,6 +52,24 @@ class GamificationPlugin::API < Grape::API {:points => person.points} end + get ':id/points_by_type' do + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) + return not_found! if person.blank? + {points: person.points_by_type(params[:type]) } + end + + get ':id/points_by_profile' do + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) + return not_found! if person.blank? + {points: person.points_by_type(params[:profile]) } + end + + get ':id/points_out_of_profiles' do + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) + return not_found! if person.blank? + {points: person.points_out_of_profiles } + end + get ':id/level' do person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) return not_found! if person.blank? -- libgit2 0.21.2