From e6adb739d3601e0f1c7b2a8af6eb075f770eeda7 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Mon, 26 Oct 2020 19:51:08 -0300
Subject: [PATCH] commit

---
 .../Tsi1.Api/Controllers/UserController.cs    | 56 ++++++++++++++++++-
 .../Dtos/UserRegisterDto.cs                   |  3 -
 .../Tsi1.BusinessLayer/Dtos/UserTypeDto.cs    | 13 +++++
 .../Helpers/ErrorMessages.cs                  |  2 +
 .../Helpers/MappingProfile.cs                 |  2 +
 .../Interfaces/IUserService.cs                |  2 +-
 .../Interfaces/IUserTypeService.cs            |  7 ++-
 .../Services/UserService.cs                   |  7 +--
 .../Services/UserTypeService.cs               | 45 +++++++++++++--
 9 files changed, 118 insertions(+), 19 deletions(-)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
index a26c760..4689d23 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
@@ -83,8 +83,8 @@ namespace Tsi1.Api.Controllers
             });
         }
 
+        [AllowAnonymous]
         [HttpPost("RefreshToken")]
-        [Authorize]
         public async Task<ActionResult> RefreshToken([FromBody] RefreshTokenRequest request)
         {
             try
@@ -118,10 +118,38 @@ namespace Tsi1.Api.Controllers
         public async Task<IActionResult> Register(UserRegisterDto dto)
         {
             var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
-            dto.TenantId = tenantId;
 
             var userTypeResult = await _userTypeService.GetById(dto.UserTypeId);
 
+            if (userTypeResult.HasError)
+            {
+                return BadRequest(userTypeResult.Message);
+            }
+
+            var userType = userTypeResult.Data;
+
+            if (userType.Name == UserTypes.UdelarAdmin ||
+                userType.Name == UserTypes.FacultyAdmin)
+            {
+                return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name));
+            }
+
+            var userServiceResult = await _userService.Create(dto, userType.Name, tenantId);
+
+            if (userServiceResult.HasError)
+            {
+                return BadRequest(userServiceResult.Message);
+            }
+
+            return Ok();
+        }
+
+        [Authorize(Roles = UserTypes.UdelarAdmin)]
+        [HttpPost("RegisterAdmin/{tenantId}")]
+        public async Task<IActionResult> RegisterAdmin(UserRegisterDto dto, int tenantId)
+        {
+            var userTypeResult = await _userTypeService.GetById(dto.UserTypeId);
+
             if (userTypeResult.HasError)
             {
                 BadRequest(userTypeResult.Message);
@@ -129,7 +157,13 @@ namespace Tsi1.Api.Controllers
 
             var userType = userTypeResult.Data;
 
-            var userServiceResult = await _userService.Create(dto, userType.Name);
+            if (userType.Name == UserTypes.Student ||
+                userType.Name == UserTypes.Professor)
+            {
+                return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name));
+            }
+
+            var userServiceResult = await _userService.Create(dto, userType.Name, tenantId);
 
             if (userServiceResult.HasError)
             {
@@ -170,5 +204,21 @@ namespace Tsi1.Api.Controllers
             return Ok(result.Data);
         }
 
+        [Authorize(Roles = UserTypes.UdelarAdmin + ", " + UserTypes.FacultyAdmin)]
+        [HttpGet("GetUserTypes")]
+        public async Task<IActionResult> GetUserTypes()
+        {
+            var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value;
+
+            var result = await _userTypeService.GetAll(userType);
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
+
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
index 2cc5d27..d01877d 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
@@ -29,9 +29,6 @@ namespace Tsi1.BusinessLayer.Dtos
         [Required]
         public string IdentityCard { get; set; }
 
-        [JsonIgnore]
-        public int TenantId { get; set; }
-
         public int Age { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs
new file mode 100644
index 0000000..5ba1793
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class UserTypeDto
+    {
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs
index 29bacb3..99c521a 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs
@@ -34,5 +34,7 @@ namespace Tsi1.BusinessLayer.Helpers
         public const string TenantDoesNotExist = "La Facultad '{0}' no existe";
         public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'";
 
+        public const string InvalidUserType = "Tipo de usuario invalido '{0}'";
+
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
index c00cf1a..5d666d1 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
@@ -28,6 +28,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<Course, CoursePreviewDto>();
             CreateMap<Tenant, TenantPreviewDto>();
             CreateMap<Tenant, TenantCreateDto>();
+            CreateMap<UserType, UserTypeDto>();
 
             CreateMap<ForumCreateDto, Forum>();
             CreateMap<ForumPreviewDto, Forum>();
@@ -45,6 +46,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<CoursePreviewDto, Course>();
             CreateMap<TenantPreviewDto, Tenant>();
             CreateMap<TenantCreateDto, Tenant>();
+            CreateMap<UserTypeDto, UserType>();
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
index ed78095..12444e6 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
@@ -12,7 +12,7 @@ namespace Tsi1.BusinessLayer.Interfaces
     {
         Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId);
 
-        Task<ServiceResult<User>> Create(UserRegisterDto dto, string type);
+        Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId);
 
         Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId);
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs
index 733e3d0..e84218a 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs
@@ -2,13 +2,16 @@
 using System.Collections.Generic;
 using System.Text;
 using System.Threading.Tasks;
+using Tsi1.BusinessLayer.Dtos;
 using Tsi1.BusinessLayer.Helpers;
-using Tsi1.DataLayer.Entities;
+
 
 namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface IUserTypeService
     {
-        public Task<ServiceResult<UserType>> GetById(int id);
+        public Task<ServiceResult<UserTypeDto>> GetById(int id);
+
+        public Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
index b38e807..3aa39cc 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
@@ -52,19 +52,19 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type)
+        public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId)
         {
             var result = new ServiceResult<User>();
 
             var user = _mapper.Map<User>(dto);
-
+            user.TenantId = tenantId;
+    
             if (type == UserTypes.Student)
             {
                 user.Student = new Student()
                 {
                     IdentityCard = dto.IdentityCard,
                     Age = dto.Age,
-                    TenantId = dto.TenantId
                 };
             }
 
@@ -73,7 +73,6 @@ namespace Tsi1.BusinessLayer.Services
                 user.Professor = new Professor()
                 {
                     IdentityCard = dto.IdentityCard,
-                    TenantId = dto.TenantId
                 };
             }
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs
index 18180fd..e11f550 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs
@@ -1,8 +1,11 @@
-using Microsoft.EntityFrameworkCore;
+using AutoMapper;
+using Microsoft.EntityFrameworkCore;
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Tsi1.BusinessLayer.Dtos;
 using Tsi1.BusinessLayer.Helpers;
 using Tsi1.BusinessLayer.Interfaces;
 using Tsi1.DataLayer;
@@ -14,14 +17,43 @@ namespace Tsi1.BusinessLayer.Services
     {
         private readonly Tsi1Context _context;
 
-        public UserTypeService(Tsi1Context context)
+        private readonly IMapper _mapper;
+
+        public UserTypeService(Tsi1Context context, IMapper mapper)
         {
             _context = context;
+            _mapper = mapper;
         }
 
-        public async Task<ServiceResult<UserType>> GetById(int id)
+        public async Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType)
         {
-            var result = new ServiceResult<UserType>();
+            var result = new ServiceResult<List<UserTypeDto>>();
+
+            List<UserType> userTypes = null; 
+
+            if (userType == UserTypes.UdelarAdmin)
+            {
+                userTypes = await _context.UserTypes
+                    .Where(x => x.Name == UserTypes.UdelarAdmin ||
+                    x.Name == UserTypes.FacultyAdmin)
+                    .ToListAsync();
+            }
+            else if (userType == UserTypes.FacultyAdmin)
+            {
+                userTypes = await _context.UserTypes
+                    .Where(x => x.Name == UserTypes.Student ||
+                    x.Name == UserTypes.Professor)
+                    .ToListAsync();
+            }
+
+            result.Data = _mapper.Map<List<UserTypeDto>>(userTypes);
+
+            return result;
+        }
+
+        public async Task<ServiceResult<UserTypeDto>> GetById(int id)
+        {
+            var result = new ServiceResult<UserTypeDto>();
 
             var userType = await _context.UserTypes.FirstOrDefaultAsync(x => x.Id == id);
 
@@ -30,10 +62,11 @@ namespace Tsi1.BusinessLayer.Services
                 result.HasError = true;
                 result.Message = string.Format(ErrorMessages.UserTypeDoesNotExist, id);
             }
-
-            result.Data = userType;
+            
+            result.Data = _mapper.Map<UserTypeDto>(userType);
 
             return result;
         }
+
     }
 }
-- 
GitLab