diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index fb50bcde10ce784bd151b42eba21551aa2df2507..21b277adb29c2e7e5a64bec7d1dd1c4d56c349ed 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 0000000000000000000000000000000000000000..bb62ecbee0d56efccbdf725ed267b51d08c31dd3 --- /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 4563e7b0d11066b335c2adbb35f37de4732d47e8..02ab611713d3080fc3ac99271dcd1787cde1518a 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 fb5470f6ae4f7e64e5ebdee73b060926276e18a3..3e6742718302d55ca3993f3e526e8871efa5a5cd 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 b134703ea0c88827d65103896b470236209503b4..e824af21ada188ab0a3221025a1959230a1135ef 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; + } } }