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