From e80497175eba51a700df9e70ac3c8c2909d01757 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 12 Dec 2020 16:38:33 -0300
Subject: [PATCH] controller and services

---
 .../Tsi1.Api/Controllers/UserController.cs    | 25 +++++++++++++++++--
 .../Interfaces/IBedeliaService.cs             |  2 ++
 .../Interfaces/IUserService.cs                |  2 ++
 .../Services/BedeliaService.cs                | 24 ++++++++++++++++++
 .../Services/UserService.cs                   | 22 ++++++++++++++++
 Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs    |  1 +
 6 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
index efdcc33..ae078b3 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
@@ -11,6 +11,7 @@ using Tsi1.Api.Models;
 using Tsi1.BusinessLayer.Dtos;
 using Tsi1.BusinessLayer.Helpers;
 using Tsi1.BusinessLayer.Interfaces;
+using Tsi1.DataLayer.Entities;
 
 namespace Tsi1.Api.Controllers
 {
@@ -24,19 +25,22 @@ namespace Tsi1.Api.Controllers
         private readonly IUserTypeService _userTypeService;
         private readonly ITenantService _tenantService;
         private readonly IEmailService _emailService;
+        private readonly IBedeliaService _bedeliaService;
 
         public UserController(
             IJwtAuthManager jwtAuthManager,
             IUserService userService,
             IUserTypeService userTypeService,
             ITenantService tenantService,
-            IEmailService emailService)
+            IEmailService emailService,
+            IBedeliaService bedeliaService)
         {
             _jwtAuthManager = jwtAuthManager;
             _userService = userService;
             _userTypeService = userTypeService;
             _tenantService = tenantService;
             _emailService = emailService;
+            _bedeliaService = bedeliaService;
         }
 
         [AllowAnonymous]
@@ -59,7 +63,24 @@ namespace Tsi1.Api.Controllers
                 return BadRequest(tenant.Message);
             }
 
-            var result = await _userService.Authenticate(userName, request.Password, tenant.Data.Id);
+            var result = new ServiceResult<User>();
+
+            if (tenant.Data.IsLoginBedelia)
+            {
+                var bedeliaResult = await _bedeliaService.Login(userName, request.Password);
+
+                if (bedeliaResult.HasError)
+                {
+                    return BadRequest(bedeliaResult.Message);
+                }
+
+                result = await _userService.GetByIdentityCard(userName);
+            }
+            else
+            {
+                result = await _userService.Authenticate(userName, request.Password, tenant.Data.Id);
+            }
+            
             if (result.HasError)
             {
                 return BadRequest(result.Message);
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs
index 88a6f29..0c58b2d 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs
@@ -12,5 +12,7 @@ namespace Tsi1.BusinessLayer.Interfaces
         Task<ServiceResult<bool>> IsValidUser(string identityCard);
 
         Task<ServiceResult<bool>> CloseRecord(string courseName, List<UserGradeDto> userGrades);
+
+        Task<ServiceResult<bool>> Login(string identityCard, string password);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
index e3a8920..5b6c77f 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
@@ -18,6 +18,8 @@ namespace Tsi1.BusinessLayer.Interfaces
 
         Task<ServiceResult<UserDetailDto>> GetById(int userId);
 
+        Task<ServiceResult<User>> GetByIdentityCard(string IdentityCard);
+
         Task<ServiceResult<User>> GetByUsername(string username, int tenantId);
 
         Task<ServiceResult<bool>> UpdatePassword(int userId, string password);
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs
index cd7a08a..8ea29f9 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs
@@ -57,5 +57,29 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<bool>> Login(string identityCard, string password)
+        {
+            var result = new ServiceResult<bool>();
+
+            var loginDto = new BedeliaLoginDto()
+            {
+                IdentityCard = identityCard,
+                Password = password
+            };
+
+            var jsonInString = JsonConvert.SerializeObject(loginDto);
+            var model = new StringContent(jsonInString, Encoding.UTF8, "application/json");
+
+            var response = await _httpClient.PostAsync($"api/Users/login", model);
+            if (response.StatusCode != HttpStatusCode.OK)
+            {
+                result.HasError = true;
+                var errorMessage = await response.Content.ReadAsStringAsync();
+                result.AddMessage(errorMessage);
+            }
+
+            return result;
+        }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
index d50c978..9ebcbda 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
@@ -382,5 +382,27 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<User>> GetByIdentityCard(string IdentityCard)
+        {
+            var result = new ServiceResult<User>();
+
+            var user = await _context.Users
+                .AsNoTracking()
+                .Include(x => x.Student)
+                .Include(x => x.UserType)
+                .FirstOrDefaultAsync(x => x.Student.IdentityCard == IdentityCard);
+
+            if (user == null)
+            {
+                result.HasError = true;
+                result.Message = string.Format(ErrorMessages.UserDoesNotExist, IdentityCard);
+                return result;
+            }
+
+            result.Data = user;
+
+            return result;           
+        }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
index b5a2754..5a97225 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
@@ -19,6 +19,7 @@ namespace Tsi1.DataLayer.Entities
         public int Id { get; set; }
         public string Name { get; set; }
         public string Theme { get; set; }
+        public bool IsLoginBedelia { get; set; }
 
         public ICollection<Course> Courses { get; set; }
         public ICollection<Professor> Professors { get; set; }
-- 
GitLab