From c5c0bfba11f7cf1c21188e808530ad0a3b3b4159 Mon Sep 17 00:00:00 2001
From: tmn21 <tmn21@inf.ufpr.br>
Date: Tue, 16 May 2023 11:40:15 -0300
Subject: [PATCH] ISSUE #41: UPDATE top_users route

---
 app/controllers/v1/users_controller.rb | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb
index 5fdc1d08..dcc94b82 100644
--- a/app/controllers/v1/users_controller.rb
+++ b/app/controllers/v1/users_controller.rb
@@ -178,9 +178,15 @@ class V1::UsersController < ApplicationController
     render status: :ok, json: response
   end
 
+  # GET /v1/top_users
   def show_top_users
+    # users = User.all.order("level DESC").limit(5)
     # select id, level from users order by level desc limit 5;
-    users = paginate User.all.order("level DESC").limit(6)
+
+    # select users who are not admin and order by level, limit to 5 users
+    # TO DO: the query isn't excluding admin users
+    ids = User.joins(:roles_users).where.not(roles_users: {role_id: 3})
+    users = User.where(id: ids).order("level DESC").limit(5)
     render json: users
   end
 
-- 
GitLab