Initial commit

This commit is contained in:
2026-05-05 10:39:43 +02:00
commit b590ecdc35
87 changed files with 3934 additions and 0 deletions
@@ -0,0 +1,35 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Topics;
using MetaCourse.Api.Entities;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Topics;
public class CreateTopicEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateTopicDto, GetTopicDto>
{
public override void Configure()
{
Post("api/topics");
AllowAnonymous();
Summary(s => s.Summary = "Ajoute un sujet à un cours");
}
public override async Task HandleAsync(CreateTopicDto req, CancellationToken ct)
{
var courseExists = await db.Courses.AnyAsync(c => c.Id == req.CourseId, ct);
if (!courseExists)
{
AddError(r => r.CourseId, "Le cours n'existe pas.");
await SendErrorsAsync(404, ct);
return;
}
var topic = mapper.Map<Topic>(req);
db.Topics.Add(topic);
await db.SaveChangesAsync(ct);
await SendAsync(mapper.Map<GetTopicDto>(topic), 201, ct);
}
}
@@ -0,0 +1,34 @@
using FastEndpoints;
using MetaCourse.Api.Data;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Topics;
public class DeleteTopicRequest
{
public Guid Id { get; set; }
}
public class DeleteTopicEndpoint(AppDbContext db) : Endpoint<DeleteTopicRequest>
{
public override void Configure()
{
Delete("api/topics/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Supprime un sujet et ses associations de ressources");
}
public override async Task HandleAsync(DeleteTopicRequest req, CancellationToken ct)
{
var topic = await db.Topics.FirstOrDefaultAsync(t => t.Id == req.Id, ct);
if (topic is null)
{
await SendNotFoundAsync(ct);
return;
}
db.Topics.Remove(topic);
await db.SaveChangesAsync(ct);
await SendNoContentAsync(ct);
}
}
@@ -0,0 +1,39 @@
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);
}
}
@@ -0,0 +1,61 @@
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.Entities;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Topics;
public class LinkResourceRequest
{
public Guid TopicId { get; set; }
public Guid ResourceId { get; set; }
public int Position { get; set; } = 1;
}
public class LinkResourceToTopicEndpoint(AppDbContext db) : Endpoint<LinkResourceRequest>
{
public override void Configure()
{
Post("api/topics/{topicId}/resources/{resourceId}");
AllowAnonymous();
Summary(s => s.Summary = "Associe une ressource à un sujet");
}
public override async Task HandleAsync(LinkResourceRequest req, CancellationToken ct)
{
var topicExists = await db.Topics.AnyAsync(t => t.Id == req.TopicId, ct);
if (!topicExists)
{
AddError("Le sujet n'existe pas.");
await SendErrorsAsync(404, ct);
return;
}
var resourceExists = await db.Resources.AnyAsync(r => r.Id == req.ResourceId, ct);
if (!resourceExists)
{
AddError("La ressource n'existe pas.");
await SendErrorsAsync(404, ct);
return;
}
var alreadyLinked = await db.TopicResources
.AnyAsync(tr => tr.TopicId == req.TopicId && tr.ResourceId == req.ResourceId, ct);
if (alreadyLinked)
{
AddError("Cette ressource est déjà associée à ce sujet.");
await SendErrorsAsync(409, ct);
return;
}
db.TopicResources.Add(new TopicResource
{
TopicId = req.TopicId,
ResourceId = req.ResourceId,
Position = req.Position
});
await db.SaveChangesAsync(ct);
await SendNoContentAsync(ct);
}
}
@@ -0,0 +1,37 @@
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);
}
}
@@ -0,0 +1,36 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Topics;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Topics;
public class UpdateTopicEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateTopicDto, GetTopicDto>
{
public override void Configure()
{
Put("api/topics/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Met à jour un sujet");
}
public override async Task HandleAsync(UpdateTopicDto req, CancellationToken ct)
{
var topic = await db.Topics
.Include(t => t.TopicResources)
.ThenInclude(tr => tr.Resource)
.FirstOrDefaultAsync(t => t.Id == req.Id, ct);
if (topic is null)
{
await SendNotFoundAsync(ct);
return;
}
mapper.Map(req, topic);
await db.SaveChangesAsync(ct);
await SendOkAsync(mapper.Map<GetTopicDto>(topic), ct);
}
}