Files
MetaCourseApi/MetaCourse.Api/Endpoints/UserCourses/GetUserEnrollmentsEndpoint.cs
T
2026-05-05 10:39:43 +02:00

36 lines
1.0 KiB
C#

using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.UserCourses;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.UserCourses;
public class GetUserEnrollmentsRequest
{
public Guid UserId { get; set; }
}
public class GetUserEnrollmentsEndpoint(AppDbContext db, AutoMapper.IMapper mapper)
: Endpoint<GetUserEnrollmentsRequest, List<GetEnrollmentDto>>
{
public override void Configure()
{
Get("api/users/{userId}/enrollments");
AllowAnonymous();
Summary(s => s.Summary = "Liste les cours auxquels un utilisateur est inscrit");
}
public override async Task HandleAsync(GetUserEnrollmentsRequest req, CancellationToken ct)
{
var enrollments = await db.UserCourses
.AsNoTracking()
.Include(uc => uc.Course)
.Where(uc => uc.UserId == req.UserId)
.OrderByDescending(uc => uc.EnrolledAt)
.ToListAsync(ct);
await SendOkAsync(mapper.Map<List<GetEnrollmentDto>>(enrollments), ct);
}
}