skantravels/API/Application/Users/Queries/QueryAllUsers.cs
2024-08-13 13:29:01 +02:00

34 lines
893 B
C#

using API.Models;
using API.Persistence.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace API.Application.Users.Queries
{
public class QueryAllUsers
{
private readonly IUserRepository _repository;
public QueryAllUsers(IUserRepository repository)
{
_repository = repository;
}
public async Task<ActionResult<List<UserDTO>>> Handle()
{
List<User> users = await _repository.QueryAllUsersAsync();
if (!users.Any())
{
return new ConflictObjectResult(new { message = "No users found." });
}
List<UserDTO> userDTOs = users.Select(user => new UserDTO
{
Id = user.Id,
Email = user.Email,
Username = user.Username
}).ToList();
return userDTOs;
}
}
}