34 lines
904 B
C#
34 lines
904 B
C#
using AutoMapper;
|
|
using FastEndpoints;
|
|
using MetaCourse.Api.Data;
|
|
using MetaCourse.Api.DTOs.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MetaCourse.Api.Endpoints.Users;
|
|
|
|
public class GetUserRequest
|
|
{
|
|
public Guid Id { get; set; }
|
|
}
|
|
|
|
public class GetUserEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetUserRequest, GetUserDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("api/users/{id}");
|
|
AllowAnonymous();
|
|
Summary(s => s.Summary = "Récupère le profil d'un utilisateur");
|
|
}
|
|
|
|
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
|
|
{
|
|
var user = await db.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Id == req.Id, ct);
|
|
if (user is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
await SendOkAsync(mapper.Map<GetUserDto>(user), ct);
|
|
}
|
|
}
|