38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
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);
|
|
}
|
|
}
|