diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 980dfbfa97c4df8eb967c39f3cea865c72b567a3..f7051fcd45455b9e80c14bd8d5d76c240f1714c7 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -3,10 +3,9 @@ class V1::UsersController < ApplicationController include ::DeletedObjectsController include ::Paginator - - before_action :set_user, only: [:show, :update, :destroy, :following, :learning_objects, :collections, :liked] - before_action :authenticate_user!, only: [:create, :update, :destroy, :following, :learning_objects, :collections, :liked] - before_action :authorize_user, only: [:learning_objects, :collections, :liked] + before_action :set_user, only: [:show, :update, :destroy, :following, :own_reviews, :received_reviews, :learning_objects, :collections, :liked] + before_action :authenticate_user!, only: [:create, :update, :destroy, :following, :own_reviews, :received_reviews, :learning_objects, :collections, :liked] + before_action :authorize_user, only: [:own_reviews, :received_reviews, :learning_objects, :collections, :liked] # GET /v1/users # GET /v1/users.json @@ -31,7 +30,6 @@ class V1::UsersController < ApplicationController else render json: user.errors, status: :unprocessable_entity end - end # PUT/PATCH /v1/users/1 @@ -63,6 +61,14 @@ class V1::UsersController < ApplicationController render json: w, root: 'follows', status: :ok end + def own_reviews + render json: @user.own_reviews + end + + def received_reviews + render json: @user.received_reviews + end + def learning_objects render json: LearningObject.where(publisher: @user) end diff --git a/app/models/user.rb b/app/models/user.rb index 4a3ae3ff4899ed16653b7520b7eb1301ae753913..c07fd326ac65559cd24f2b806bb2bf17af0c3283 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -104,6 +104,16 @@ class User < ApplicationRecord lo end + def own_reviews + Review.where(user_id: id) + end + + def received_reviews + Review.joins('INNER JOIN learning_objects ON ' \ + "learning_objects.publisher_id = #{id} AND " \ + 'reviews.reviewable_id = learning_objects.id') + end + # ~~~~ followable actions ~~~~ # # An user can follow anothers users and collections # Examples: diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index e4d740df9d6d70d4e2651005876c710137902a52..44660bbc32f1691fffa3bd6107ee830ef735d7f0 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -9,6 +9,14 @@ class UserPolicy < ApplicationPolicy record if user_exists? end + def own_reviews? + record if same_user? || user.is_admin? + end + + def received_reviews? + record if same_user? || user.is_admin? + end + def learning_objects? record if same_user? || user.is_admin? end diff --git a/config/routes.rb b/config/routes.rb index 69bb604f162952f9426357b6c36c7e0dc236b519..a2e1293e8782d9936609dffd57fcda7666364ff0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,6 +68,8 @@ Rails.application.routes.draw do resources :bookmarks, module: 'users', only: [:index, :create, :destroy] get 'following/:object_type', to: 'users#following' get 'activities', to: 'activities#user_activities' + get 'reviews/own', to: 'users#own_reviews' + get 'reviews/received', to: 'users#received_reviews' get 'learning_objects', to: 'users#learning_objects' get 'collections', to: 'users#collections' get 'liked', to: 'users#liked'