using Domain.Entities;
using Domain.Exceptions;
using Features.Users.Dtos;
using Features.Users.Queries.GetAllUsers;
using Features.Users.Queries.GetPublicUserProfileById;
using Features.Users.Queries.GetUserById;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Features.Users.Controllers;
///
/// Exposes read endpoints for user accounts.
///
[ApiController]
[Route("api/[controller]")]
public class UserController(IMediator mediator) : ControllerBase
{
/// Gets a page of user accounts, ordered by creation date descending.
/// null for no limit.
/// null for no offset.
[HttpGet]
public async Task>> GetAll(
[FromQuery] int? limit,
[FromQuery] int? offset
)
{
IEnumerable users = await mediator.Send(new GetAllUsersQuery(limit, offset));
return Ok(users);
}
/// Gets the user account with the given ID.
///
/// Thrown when no user account exists with the given ID, resulting in a 404 response.
///
[HttpGet("{id:guid}")]
public async Task> GetById(Guid id)
{
UserAccount user = await mediator.Send(new GetUserByIdQuery(id));
return Ok(user);
}
/// Gets the public profile for the user account with the given ID.
///
/// Exposes only the fields that are safe to show on an unauthenticated, public profile page;
/// unlike , this never returns Email or DateOfBirth.
///
///
/// Thrown when no user account exists with the given ID, resulting in a 404 response.
///
[HttpGet("{id:guid}/profile")]
public async Task> GetPublicProfile(Guid id)
{
PublicUserProfileDto profile = await mediator.Send(new GetPublicUserProfileByIdQuery(id));
return Ok(profile);
}
}