Initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using MetaCourse.Api.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class CreateCourseEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateCourseDto, GetCourseDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("api/courses");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Crée un nouveau cours (statut Brouillon par défaut)");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateCourseDto req, CancellationToken ct)
|
||||
{
|
||||
var creatorExists = await db.Users.AnyAsync(u => u.Id == req.CreatorId, ct);
|
||||
if (!creatorExists)
|
||||
{
|
||||
AddError(r => r.CreatorId, "L'utilisateur créateur n'existe pas.");
|
||||
await SendErrorsAsync(404, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var course = mapper.Map<Course>(req);
|
||||
db.Courses.Add(course);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
var created = await db.Courses
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.FirstAsync(c => c.Id == course.Id, ct);
|
||||
|
||||
await SendAsync(mapper.Map<GetCourseDto>(created), 201, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class DeleteCourseRequest
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteCourseEndpoint(AppDbContext db) : Endpoint<DeleteCourseRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("api/courses/{id}");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Supprime un cours (uniquement si brouillon sans inscriptions)");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteCourseRequest req, CancellationToken ct)
|
||||
{
|
||||
var course = await db.Courses
|
||||
.Include(c => c.UserCourses)
|
||||
.FirstOrDefaultAsync(c => c.Id == req.Id, ct);
|
||||
|
||||
if (course is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (course.UserCourses.Any())
|
||||
{
|
||||
AddError("Impossible de supprimer un cours auquel des utilisateurs sont inscrits.");
|
||||
await SendErrorsAsync(409, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
db.Courses.Remove(course);
|
||||
await db.SaveChangesAsync(ct);
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class GetCourseRequest
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetCourseEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetCourseRequest, GetCourseDetailsDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("api/courses/{id}");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Récupère le détail d'un cours avec ses sujets et ressources");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetCourseRequest req, CancellationToken ct)
|
||||
{
|
||||
var course = await db.Courses
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics.OrderBy(t => t.Position))
|
||||
.ThenInclude(t => t.TopicResources.OrderBy(tr => tr.Position))
|
||||
.ThenInclude(tr => tr.Resource)
|
||||
.FirstOrDefaultAsync(c => c.Id == req.Id, ct);
|
||||
|
||||
if (course is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await SendOkAsync(mapper.Map<GetCourseDetailsDto>(course), ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class GetCoursesRequest
|
||||
{
|
||||
public string? Search { get; set; }
|
||||
}
|
||||
|
||||
public class GetCoursesEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetCoursesRequest, List<GetCourseDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("api/courses");
|
||||
AllowAnonymous();
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "Liste les cours publiés";
|
||||
s.Description = "Retourne tous les cours avec le statut Publié, avec recherche optionnelle.";
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetCoursesRequest req, CancellationToken ct)
|
||||
{
|
||||
var query = db.Courses
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.Where(c => c.Status == Entities.CourseStatus.Published);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(req.Search))
|
||||
{
|
||||
var search = req.Search.ToLower();
|
||||
query = query.Where(c =>
|
||||
c.Title.ToLower().Contains(search) ||
|
||||
c.Description.ToLower().Contains(search) ||
|
||||
c.Creator.Name.ToLower().Contains(search));
|
||||
}
|
||||
|
||||
var courses = await query.OrderByDescending(c => c.CreatedAt).ToListAsync(ct);
|
||||
await SendOkAsync(mapper.Map<List<GetCourseDto>>(courses), ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class GetUserCoursesRequest
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
}
|
||||
|
||||
public class GetUserCoursesEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetUserCoursesRequest, List<GetCourseDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("api/users/{userId}/courses");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Liste les cours créés par un utilisateur");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetUserCoursesRequest req, CancellationToken ct)
|
||||
{
|
||||
var courses = await db.Courses
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.Where(c => c.CreatorId == req.UserId)
|
||||
.OrderByDescending(c => c.UpdatedAt)
|
||||
.ToListAsync(ct);
|
||||
|
||||
await SendOkAsync(mapper.Map<List<GetCourseDto>>(courses), ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using MetaCourse.Api.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class PublishCourseRequest
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class PublishCourseEndpoint(AppDbContext db) : Endpoint<PublishCourseRequest, GetCourseDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("api/courses/{id}/publish");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Publie un cours (le rend visible dans le catalogue)");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(PublishCourseRequest req, CancellationToken ct)
|
||||
{
|
||||
var course = await db.Courses
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.FirstOrDefaultAsync(c => c.Id == req.Id, ct);
|
||||
|
||||
if (course is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!course.Topics.Any())
|
||||
{
|
||||
AddError("Un cours doit avoir au moins un sujet pour être publié.");
|
||||
await SendErrorsAsync(422, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
course.Status = CourseStatus.Published;
|
||||
course.UpdatedAt = DateTime.UtcNow;
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
await SendOkAsync(new GetCourseDto
|
||||
{
|
||||
Id = course.Id,
|
||||
Title = course.Title,
|
||||
Description = course.Description,
|
||||
Status = course.Status.ToString(),
|
||||
CreatorId = course.CreatorId,
|
||||
CreatorName = course.Creator.Name,
|
||||
TopicCount = course.Topics.Count,
|
||||
CreatedAt = course.CreatedAt,
|
||||
UpdatedAt = course.UpdatedAt
|
||||
}, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class UpdateCourseEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateCourseDto, GetCourseDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("api/courses/{id}");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Met à jour le titre et la description d'un cours");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateCourseDto req, CancellationToken ct)
|
||||
{
|
||||
var course = await db.Courses
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.FirstOrDefaultAsync(c => c.Id == req.Id, ct);
|
||||
|
||||
if (course is null)
|
||||
{
|
||||
await SendNotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
mapper.Map(req, course);
|
||||
course.UpdatedAt = DateTime.UtcNow;
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
await SendOkAsync(mapper.Map<GetCourseDto>(course), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user