38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
using FastEndpoints;
|
|
using MetaCourse.Api.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MetaCourse.Api.Endpoints.Topics;
|
|
|
|
public class UnlinkResourceRequest
|
|
{
|
|
public Guid TopicId { get; set; }
|
|
public Guid ResourceId { get; set; }
|
|
}
|
|
|
|
public class UnlinkResourceFromTopicEndpoint(AppDbContext db) : Endpoint<UnlinkResourceRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("api/topics/{topicId}/resources/{resourceId}");
|
|
AllowAnonymous();
|
|
Summary(s => s.Summary = "Dissocie une ressource d'un sujet");
|
|
}
|
|
|
|
public override async Task HandleAsync(UnlinkResourceRequest req, CancellationToken ct)
|
|
{
|
|
var link = await db.TopicResources
|
|
.FirstOrDefaultAsync(tr => tr.TopicId == req.TopicId && tr.ResourceId == req.ResourceId, ct);
|
|
|
|
if (link is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
db.TopicResources.Remove(link);
|
|
await db.SaveChangesAsync(ct);
|
|
await SendNoContentAsync(ct);
|
|
}
|
|
}
|