From fcfe567872232cc9b522d0772b7fbc5e4bd3238b Mon Sep 17 00:00:00 2001
From: Lucca Santangelo <luccasant95@gmail.com>
Date: Tue, 8 Dec 2020 22:45:30 -0300
Subject: [PATCH] asd

---
 .../Tsi1.Api/Controllers/UserController.cs    | 27 ++++++++++---------
 Tsi1.Api/Tsi1.Api/Models/LoginResult.cs       |  2 ++
 .../Dtos/TenantDetailDto.cs                   | 13 +++++++++
 .../Helpers/MappingProfile.cs                 |  2 ++
 .../Interfaces/ITenantService.cs              |  2 +-
 .../Services/TenantService.cs                 |  6 ++---
 6 files changed, 35 insertions(+), 17 deletions(-)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantDetailDto.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
index fe3afbb..323eee2 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
@@ -52,14 +52,14 @@ namespace Tsi1.Api.Controllers
 
             var userName = resultSplit[0];
             var tenantName = resultSplit[1];
-            var tenantId = await _tenantService.GetByName(tenantName);
+            var tenant = await _tenantService.GetByName(tenantName);
 
-            if (tenantId.HasError)
+            if (tenant.HasError)
             {
-                return BadRequest(tenantId.Message);
+                return BadRequest(tenant.Message);
             }
 
-            var result = await _userService.Authenticate(userName, request.Password, tenantId.Data);
+            var result = await _userService.Authenticate(userName, request.Password, tenant.Data.Id);
             if (result.HasError)
             {
                 return BadRequest(result.Message);
@@ -83,7 +83,8 @@ namespace Tsi1.Api.Controllers
                 UserName = user.Username,
                 Role = user.UserType.Name,
                 AccessToken = jwtResult.AccessToken,
-                RefreshToken = jwtResult.RefreshToken.TokenString
+                RefreshToken = jwtResult.RefreshToken.TokenString,
+                Tenant = tenant.Data
             });
         }
 
@@ -392,14 +393,14 @@ namespace Tsi1.Api.Controllers
 
             username = resultSplit[0];
             var tenantName = resultSplit[1];
-            var tenantId = await _tenantService.GetByName(tenantName);
+            var tenant = await _tenantService.GetByName(tenantName);
 
-            if (tenantId.HasError)
+            if (tenant.HasError)
             {
-                return BadRequest(tenantId.Message);
+                return BadRequest(tenant.Message);
             }
 
-            var userResult = await _userService.GetByUsername(username, tenantId.Data);
+            var userResult = await _userService.GetByUsername(username, tenant.Data.Id);
 
             if (userResult.HasError)
             {
@@ -430,11 +431,11 @@ namespace Tsi1.Api.Controllers
 
             username = resultSplit[0];
             var tenantName = resultSplit[1];
-            var tenantId = await _tenantService.GetByName(tenantName);
+            var tenant = await _tenantService.GetByName(tenantName);
 
-            if (tenantId.HasError)
+            if (tenant.HasError)
             {
-                return BadRequest(tenantId.Message);
+                return BadRequest(tenant.Message);
             }
 
             if (!_jwtAuthManager.ValidateVerificationCode(username, code))
@@ -442,7 +443,7 @@ namespace Tsi1.Api.Controllers
                 return BadRequest("Código de verificación incorrecto");
             }
 
-            var userResult = await _userService.GetByUsername(username, tenantId.Data);
+            var userResult = await _userService.GetByUsername(username, tenant.Data.Id);
             if (userResult.HasError)
             {
                 return BadRequest(userResult.Message);
diff --git a/Tsi1.Api/Tsi1.Api/Models/LoginResult.cs b/Tsi1.Api/Tsi1.Api/Models/LoginResult.cs
index 8fc589a..69c47d8 100644
--- a/Tsi1.Api/Tsi1.Api/Models/LoginResult.cs
+++ b/Tsi1.Api/Tsi1.Api/Models/LoginResult.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text.Json.Serialization;
 using System.Threading.Tasks;
+using Tsi1.BusinessLayer.Dtos;
 
 namespace Tsi1.Api.Models
 {
@@ -22,5 +23,6 @@ namespace Tsi1.Api.Models
 
         [JsonPropertyName("refreshToken")]
         public string RefreshToken { get; set; }
+        public TenantDetailDto Tenant { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantDetailDto.cs
new file mode 100644
index 0000000..f8327cd
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantDetailDto.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class TenantDetailDto
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public string Theme { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
index 10ad515..9e8a5bc 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
@@ -32,6 +32,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<Professor, ProfessorPreviewDto>();
             CreateMap<Course, CourseCreateDto>();
             CreateMap<Course, CoursePreviewDto>();
+            CreateMap<Tenant, TenantDetailDto>();
             CreateMap<Tenant, TenantPreviewDto>();
             CreateMap<Tenant, TenantCreateDto>();
             CreateMap<Tenant, TenantCourseDto>().ForMember(x => x.Courses, opt => opt.Ignore());         
@@ -84,6 +85,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<ProfessorPreviewDto, Professor>();
             CreateMap<CourseCreateDto, Course>();
             CreateMap<CoursePreviewDto, Course>();
+            CreateMap<TenantDetailDto, Tenant>();
             CreateMap<TenantPreviewDto, Tenant>();
             CreateMap<TenantCreateDto, Tenant>();
             CreateMap<TenantCourseDto, Tenant>();
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
index 8c6ae9d..7e714c5 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
@@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface ITenantService
     {
-        Task<ServiceResult<int>> GetByName(string tenantName);
+        Task<ServiceResult<TenantDetailDto>> GetByName(string tenantName);
 
         Task<ServiceResult<int>> GetById(int tenantId);
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
index 38fbb00..cfd406e 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
@@ -38,9 +38,9 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<int>> GetByName(string tenantName)
+        public async Task<ServiceResult<TenantDetailDto>> GetByName(string tenantName)
         {
-            var result = new ServiceResult<int>();
+            var result = new ServiceResult<TenantDetailDto>();
 
             var tenant = await _context.Tenants
                 .FirstOrDefaultAsync(x => x.Name == tenantName);
@@ -52,7 +52,7 @@ namespace Tsi1.BusinessLayer.Services
                 return result;
             }
 
-            result.Data = tenant.Id;
+            result.Data = _mapper.Map<TenantDetailDto>(tenant);
 
             return result;
         }
-- 
GitLab