40 lines
1.0 KiB
C#
40 lines
1.0 KiB
C#
using AutoMapper;
|
|
using FastEndpoints;
|
|
using MetaCourse.Api.Data;
|
|
using MetaCourse.Api.DTOs.Topics;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MetaCourse.Api.Endpoints.Topics;
|
|
|
|
public class GetTopicRequest
|
|
{
|
|
public Guid Id { get; set; }
|
|
}
|
|
|
|
public class GetTopicEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetTopicRequest, GetTopicDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("api/topics/{id}");
|
|
AllowAnonymous();
|
|
Summary(s => s.Summary = "Récupère un sujet avec ses ressources");
|
|
}
|
|
|
|
public override async Task HandleAsync(GetTopicRequest req, CancellationToken ct)
|
|
{
|
|
var topic = await db.Topics
|
|
.AsNoTracking()
|
|
.Include(t => t.TopicResources.OrderBy(tr => tr.Position))
|
|
.ThenInclude(tr => tr.Resource)
|
|
.FirstOrDefaultAsync(t => t.Id == req.Id, ct);
|
|
|
|
if (topic is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
await SendOkAsync(mapper.Map<GetTopicDto>(topic), ct);
|
|
}
|
|
}
|