Initial commit
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user