changements de getmessageendpoint et du dto details

This commit is contained in:
gokhoal
2026-06-10 21:33:37 +02:00
parent ff317cc944
commit 7cfb3909b6
12 changed files with 40 additions and 12 deletions
@@ -6,4 +6,7 @@ public class GetMessageDetailsDto
public string? Contenu { get; set; }
public DateTime Date { get; set; }
public Boolean Type { get; set; }
public int AuthorId { get; set; }
public string AuthorName { get; set; } = "";
}
+35 -10
View File
@@ -1,3 +1,4 @@
using System.Security.Claims;
using FastEndpoints;
using Knots.DTO.Message;
using Knots.DTO.User;
@@ -5,25 +6,49 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.Message;
public class GetMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint <GetMessageDetailsDto>
public class GetMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetDiscussionMessagesRequest, List<GetMessageDetailsDto>>
{
public override void Configure()
{
Get("/messages/{@Id}");
Get("/discussions/{DiscussionId}/messages");
AllowAnonymous();
}
public override async Task HandleAsync(GetMessageDetailsDto req, CancellationToken ct)
public override async Task HandleAsync(GetDiscussionMessagesRequest req, CancellationToken ct)
{
Models.Message? databaseMessage = await db.Messages.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseMessage == null)
int userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
// l'utilisateur participe-t-il à cette discussion (privée ou via le groupe) ?
bool autorise = await db.Discussions
.Where(d => d.Id == req.DiscussionId)
.AnyAsync(d =>
d.UserDiscussions.Any(ud => ud.UserId == userId) ||
(d.Group != null && d.Group.GroupUsers.Any(gu => gu.UserId == userId)), ct);
if (!autorise)
{
await SendNotFoundAsync(ct);
await SendForbiddenAsync(ct);
return;
}
var messageDto = mapper.Map<GetMessageDetailsDto>(databaseMessage);
await SendOkAsync(messageDto, ct);
List<GetMessageDetailsDto> messages = await db.Messages
.Where(m => m.DiscussionId == req.DiscussionId)
.OrderBy(m => m.Date)
.Select(m => new GetMessageDetailsDto
{
Id = m.Id,
Contenu = m.Contenu!,
Date = m.Date,
AuthorId = m.UserId,
AuthorName = m.User.Username!
})
.ToListAsync(ct);
await SendOkAsync(messages, ct);
}
}
public class GetDiscussionMessagesRequest
{
public int DiscussionId { get; set; }
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Knots")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+79a926bc2dc0c803db06002470a5b254df5c5366")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ff317cc9449b7228da67e493ffa74b44c87b3b57")]
[assembly: System.Reflection.AssemblyProductAttribute("Knots")]
[assembly: System.Reflection.AssemblyTitleAttribute("Knots")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
6c465b3466d6b033362f776b9026e0f6319db705c736006e612bc04b20c3b80d
ca8f04f7ca657757a28c0e8abfce7eaad2aa52fcdf4495534a39ebbced49ce17
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.