From a49bb6d4957770e5d967c6fc1a2389b17930311b Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Wed, 21 Oct 2020 18:27:21 -0300 Subject: [PATCH] endpoint /api/User/GetAll --- .../Tsi1.Api/Controllers/UserController.cs | 15 ++++++++++++ .../Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs | 17 +++++++++++++ .../Helpers/MappingProfile.cs | 4 ++++ .../Interfaces/IUserService.cs | 3 +++ .../Services/UserService.cs | 24 +++++++++++++++++-- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index fb50bcd..21b277a 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -80,5 +80,20 @@ namespace Tsi1.Api.Controllers return Ok(); } + + + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [HttpGet("GetAll")] + public async Task<IActionResult> GetAll() + { + var result = await _userService.GetAll(); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs new file mode 100644 index 0000000..bb62ecb --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserPreviewDto.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class UserPreviewDto + { + public int Id { get; set; } + + public string Username { get; set; } + + public string FirstName { get; set; } + + public string LastName { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 4563e7b..02ab611 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -20,6 +20,8 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<PostMessage, PostMessagePreviewDto>(); CreateMap<Message, MessagePreviewDto>(); CreateMap<Message, MessageCreateDto>(); + CreateMap<User, UserPreviewDto>(); + CreateMap<User, UserRegisterDto>(); CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumPreviewDto, Forum>(); @@ -29,6 +31,8 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<PostMessagePreviewDto, PostMessage>(); CreateMap<MessagePreviewDto, Message>(); CreateMap<MessageCreateDto, Message>(); + CreateMap<UserPreviewDto, User>(); + CreateMap<UserRegisterDto, User>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index fb5470f..3e67427 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -11,6 +11,9 @@ namespace Tsi1.BusinessLayer.Interfaces public interface IUserService { Task<ServiceResult<User>> Authenticate(string username, string password); + Task<ServiceResult<User>> Create(UserRegisterDto dto, string type); + + Task<ServiceResult<List<UserPreviewDto>>> GetAll(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs index b134703..e824af2 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs @@ -1,6 +1,8 @@ -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; @@ -15,9 +17,12 @@ namespace Tsi1.BusinessLayer.Services { private readonly Tsi1Context _context; - public UserService(Tsi1Context context) + private readonly IMapper _mapper; + + public UserService(Tsi1Context context, IMapper mapper) { _context = context; + _mapper = mapper; } public async Task<ServiceResult<User>> Authenticate(string username, string password) @@ -84,5 +89,20 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<List<UserPreviewDto>>> GetAll() + { + var result = new ServiceResult<List<UserPreviewDto>>(); + + var users = await _context.Users + .Where(x => x.UserType.Name != UserTypes.FacultyAdmin) + .ToListAsync(); + + var usersDto = _mapper.Map<List<UserPreviewDto>>(users); + + result.Data = usersDto; + + return result; + } } } -- GitLab