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,25 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Resources;
using MetaCourse.Api.Entities;
namespace MetaCourse.Api.Endpoints.Resources;
public class CreateResourceEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateResourceDto, GetResourceDto>
{
public override void Configure()
{
Post("api/resources");
AllowAnonymous();
Summary(s => s.Summary = "Crée une nouvelle ressource dans le catalogue");
}
public override async Task HandleAsync(CreateResourceDto req, CancellationToken ct)
{
var resource = mapper.Map<Resource>(req);
db.Resources.Add(resource);
await db.SaveChangesAsync(ct);
await SendAsync(mapper.Map<GetResourceDto>(resource), 201, ct);
}
}
@@ -0,0 +1,34 @@
using FastEndpoints;
using MetaCourse.Api.Data;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Resources;
public class DeleteResourceRequest
{
public Guid Id { get; set; }
}
public class DeleteResourceEndpoint(AppDbContext db) : Endpoint<DeleteResourceRequest>
{
public override void Configure()
{
Delete("api/resources/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Supprime une ressource (et ses associations aux sujets)");
}
public override async Task HandleAsync(DeleteResourceRequest req, CancellationToken ct)
{
var resource = await db.Resources.FirstOrDefaultAsync(r => r.Id == req.Id, ct);
if (resource is null)
{
await SendNotFoundAsync(ct);
return;
}
db.Resources.Remove(resource);
await db.SaveChangesAsync(ct);
await SendNoContentAsync(ct);
}
}
@@ -0,0 +1,33 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Resources;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Resources;
public class GetResourceRequest
{
public Guid Id { get; set; }
}
public class GetResourceEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetResourceRequest, GetResourceDto>
{
public override void Configure()
{
Get("api/resources/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Récupère une ressource par son identifiant");
}
public override async Task HandleAsync(GetResourceRequest req, CancellationToken ct)
{
var resource = await db.Resources.AsNoTracking().FirstOrDefaultAsync(r => r.Id == req.Id, ct);
if (resource is null)
{
await SendNotFoundAsync(ct);
return;
}
await SendOkAsync(mapper.Map<GetResourceDto>(resource), ct);
}
}
@@ -0,0 +1,27 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Resources;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Resources;
public class GetResourcesEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetResourceDto>>
{
public override void Configure()
{
Get("api/resources");
AllowAnonymous();
Summary(s => s.Summary = "Liste toutes les ressources du catalogue");
}
public override async Task HandleAsync(CancellationToken ct)
{
var resources = await db.Resources
.AsNoTracking()
.OrderByDescending(r => r.CreatedAt)
.ToListAsync(ct);
await SendOkAsync(mapper.Map<List<GetResourceDto>>(resources), ct);
}
}
@@ -0,0 +1,31 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Resources;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Resources;
public class UpdateResourceEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateResourceDto, GetResourceDto>
{
public override void Configure()
{
Put("api/resources/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Met à jour une ressource");
}
public override async Task HandleAsync(UpdateResourceDto req, CancellationToken ct)
{
var resource = await db.Resources.FirstOrDefaultAsync(r => r.Id == req.Id, ct);
if (resource is null)
{
await SendNotFoundAsync(ct);
return;
}
mapper.Map(req, resource);
await db.SaveChangesAsync(ct);
await SendOkAsync(mapper.Map<GetResourceDto>(resource), ct);
}
}