From b21666703d4191be9e7508aa2cadf5972dbd7f28 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 24 Oct 2020 17:18:38 -0300
Subject: [PATCH] add GetById endpoint

---
 .../Tsi1.Api/Controllers/UserController.cs    | 15 +++++++
 .../Dtos/ProfessorPreviewDto.cs               | 11 +++++
 .../Dtos/StudentPreviewDto.cs                 | 13 ++++++
 .../Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs |  4 ++
 .../Helpers/MappingProfile.cs                 |  6 ++-
 .../Interfaces/IUserService.cs                |  2 +
 .../Services/UserService.cs                   | 40 +++++++++++++++++++
 7 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorPreviewDto.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentPreviewDto.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
index f5f1340..b1fb277 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs
@@ -102,5 +102,20 @@ namespace Tsi1.Api.Controllers
 
             return Ok(result.Data);
         }
+
+        [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
+        [HttpGet("GetById/{userId}")]
+        public async Task<IActionResult> GetById(int userId)
+        {
+            var result = await _userService.GetById(userId);
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
+
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorPreviewDto.cs
new file mode 100644
index 0000000..374a36a
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorPreviewDto.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class ProfessorPreviewDto
+    {
+        public string IdentityCard { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentPreviewDto.cs
new file mode 100644
index 0000000..e05efa4
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentPreviewDto.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class StudentPreviewDto
+    {
+        public string IdentityCard { get; set; }
+
+        public int Age { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs
index bb62ecb..1c9804b 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs
@@ -13,5 +13,9 @@ namespace Tsi1.BusinessLayer.Dtos
         public string FirstName { get; set; }
 
         public string LastName { get; set; }
+
+        public StudentPreviewDto Student { get; set; }
+
+        public ProfessorPreviewDto Professor { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
index 6542de7..8fab13c 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
@@ -22,6 +22,8 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<Message, MessageCreateDto>();
             CreateMap<User, UserPreviewDto>();
             CreateMap<User, UserRegisterDto>();
+            CreateMap<Student, StudentPreviewDto>();
+            CreateMap<Professor, ProfessorPreviewDto>();
             CreateMap<Course, CourseCreateDto>();
             CreateMap<Course, CoursePreviewDto>();
 
@@ -36,8 +38,10 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<MessageCreateDto, Message>();
             CreateMap<UserPreviewDto, User>();
             CreateMap<UserRegisterDto, User>();
+            CreateMap<StudentPreviewDto, Student>();
+            CreateMap<ProfessorPreviewDto, Professor>();
             CreateMap<CourseCreateDto, Course>();
-            CreateMap<CoursePreviewDto, Course>();
+            CreateMap<CoursePreviewDto, Course>();        
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
index 8b2f9d7..ed78095 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
@@ -15,5 +15,7 @@ namespace Tsi1.BusinessLayer.Interfaces
         Task<ServiceResult<User>> Create(UserRegisterDto dto, string type);
 
         Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId);
+
+        Task<ServiceResult<UserPreviewDto>> GetById(int userId);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
index e523016..cc163b9 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
@@ -1,5 +1,6 @@
 using AutoMapper;
 using Microsoft.EntityFrameworkCore;
+using Org.BouncyCastle.Math.EC.Rfc7748;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -98,5 +99,44 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<UserPreviewDto>> GetById(int userId)
+        {
+            var result = new ServiceResult<UserPreviewDto>();
+
+            var user = await _context.Users
+                .Include(x => x.UserType)
+                .Include(x => x.Student)
+                .Include(x => x.Professor)
+                .FirstOrDefaultAsync(x => x.Id == userId);
+
+            if (user == null)
+            {
+                result.HasError = true;
+                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
+                return result;
+            }
+
+            var userType = user.UserType.Name;
+
+            if (userType == UserTypes.Student && user.Student == null)
+            {
+                result.HasError = true;
+                result.Message = string.Format(ErrorMessages.StudentDoesNotExist, userId);
+                return result;                
+            }
+            else if(userType == UserTypes.Professor && user.Professor == null)
+            {
+                result.HasError = true;
+                result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, userId);
+                return result;
+            }
+
+            var userDto = _mapper.Map<UserPreviewDto>(user);
+
+            result.Data = userDto;
+
+            return result;
+        }
     }
 }
-- 
GitLab