Skip to content
Snippets Groups Projects
Commit a49bb6d4 authored by esantangelo's avatar esantangelo
Browse files

endpoint /api/User/GetAll

parent e681edef
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
}
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; }
}
}
......@@ -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>();
}
}
}
......@@ -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();
}
}
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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment