diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index f5f1340800c14067c16d6d09f4c60d1276497ee4..b1fb2775ccac22639321795130fadcddae79ffeb 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 0000000000000000000000000000000000000000..374a36a35db2c765c86ca2ac35d1322abee8e370 --- /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 0000000000000000000000000000000000000000..e05efa4dfd93c73fd367a9f40f73540a46769708 --- /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 bb62ecbee0d56efccbdf725ed267b51d08c31dd3..1c9804bd001f492f7f22dd84357ef2b56b528790 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 6542de74a25d2b9043de3865ba8e9b498388c8fb..8fab13c1e816f8afb5cbf38d9981684b7555ab7b 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 8b2f9d7ce3ae138b4428776b0f908011466dd142..ed780959cdca1def549471d306c0ad64ef0f7ac6 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 e52301662ae58a972dab5df49d48c95279e19c6f..cc163b9cfe160324a40270d3098fefd68f75ad87 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; + } } }