Initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Progress;
|
||||
using MetaCourse.Api.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Progress;
|
||||
|
||||
public class MarkTopicProgressEndpoint(AppDbContext db) : Endpoint<MarkTopicProgressDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("api/topics/{topicId}/progress");
|
||||
AllowAnonymous();
|
||||
Summary(s => s.Summary = "Marque un sujet comme terminé ou non terminé pour un utilisateur");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(MarkTopicProgressDto req, CancellationToken ct)
|
||||
{
|
||||
var progress = await db.UserTopicProgresses
|
||||
.FirstOrDefaultAsync(p => p.UserId == req.UserId && p.TopicId == req.TopicId, ct);
|
||||
|
||||
if (progress is null)
|
||||
{
|
||||
db.UserTopicProgresses.Add(new UserTopicProgress
|
||||
{
|
||||
UserId = req.UserId,
|
||||
TopicId = req.TopicId,
|
||||
Completed = req.Completed,
|
||||
CompletedAt = req.Completed ? DateTime.UtcNow : null
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
progress.Completed = req.Completed;
|
||||
progress.CompletedAt = req.Completed ? DateTime.UtcNow : null;
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user