From 3f63dcc44e91ad2c394a05d5417638f32b8dfdf9 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Thu, 12 Nov 2020 21:26:08 -0300
Subject: [PATCH] add endpoint GetAdmins

---
 .../Tsi1.Api/Controllers/UserController.cs    | 16 ++++++++++++
 .../Interfaces/IUserService.cs                |  1 +
 .../Services/UserService.cs                   | 26 +++++++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
index e2abf68..1b3c612 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
@@ -332,6 +332,22 @@ namespace Tsi1.Api.Controllers
             return Ok(result.Data);
         }
 
+        [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.UdelarAdmin)]
+        [HttpGet("GetAdmins")]
+        public async Task<IActionResult> GetAdmins()
+        {
+            var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
+            var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value;
+
+            var result = await _userService.GetAdmins(tenantId, userType);
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
 
         [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
         [HttpGet("GetById/{userId}")]
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
index 1fbf3d8..d503323 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
@@ -33,5 +33,6 @@ namespace Tsi1.BusinessLayer.Interfaces
         Task<ServiceResult<List<UserPreviewDto>>> GetStudents(int tenantId);
 
         Task<ServiceResult<List<UserPreviewDto>>> GetProfessors(int tenantId);
+        Task<ServiceResult<List<UserPreviewDto>>> GetAdmins(int tenantId, string userType);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
index f7710da..1e8f2b1 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
@@ -332,5 +332,31 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<List<UserPreviewDto>>> GetAdmins(int tenantId, string userType)
+        {
+            var result = new ServiceResult<List<UserPreviewDto>>();
+
+            var users = new List<User>();
+
+            if (userType == UserTypes.UdelarAdmin)
+            {
+                users = await _context.Users
+                    .Where(x => x.UserType.Name == UserTypes.UdelarAdmin || x.UserType.Name == UserTypes.FacultyAdmin)
+                    .ToListAsync();
+            }
+            else
+            {
+                users = await _context.Users
+                    .Where(x => x.UserType.Name == UserTypes.FacultyAdmin && x.TenantId == tenantId)
+                    .ToListAsync();
+            }
+
+            var usersDto = _mapper.Map<List<UserPreviewDto>>(users);
+
+            result.Data = usersDto;
+
+            return result;
+        }
     }
 }
-- 
GitLab