diff --git a/Knots/DTO/Message/GetMessageDetailsDto.cs b/Knots/DTO/Message/GetMessageDetailsDto.cs index cc5eab0..5e4c1a4 100644 --- a/Knots/DTO/Message/GetMessageDetailsDto.cs +++ b/Knots/DTO/Message/GetMessageDetailsDto.cs @@ -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; } = ""; } \ No newline at end of file diff --git a/Knots/Endpoints/Message/GetMessageEndpoint.cs b/Knots/Endpoints/Message/GetMessageEndpoint.cs index bb866a8..a79a4d0 100644 --- a/Knots/Endpoints/Message/GetMessageEndpoint.cs +++ b/Knots/Endpoints/Message/GetMessageEndpoint.cs @@ -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 +public class GetMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint> { 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(databaseMessage); - await SendOkAsync(messageDto, ct); + + List 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; } } \ No newline at end of file diff --git a/Knots/bin/Debug/net8.0/Knots.dll b/Knots/bin/Debug/net8.0/Knots.dll index 52a487f..0352e3e 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.dll and b/Knots/bin/Debug/net8.0/Knots.dll differ diff --git a/Knots/bin/Debug/net8.0/Knots.exe b/Knots/bin/Debug/net8.0/Knots.exe index 532b663..f2acea3 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.exe and b/Knots/bin/Debug/net8.0/Knots.exe differ diff --git a/Knots/bin/Debug/net8.0/Knots.pdb b/Knots/bin/Debug/net8.0/Knots.pdb index 6a2c09e..a7dde11 100644 Binary files a/Knots/bin/Debug/net8.0/Knots.pdb and b/Knots/bin/Debug/net8.0/Knots.pdb differ diff --git a/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs b/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs index ba4bdfd..3c26555 100644 --- a/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs +++ b/Knots/obj/Debug/net8.0/Knots.AssemblyInfo.cs @@ -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")] diff --git a/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache b/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache index dd12141..7d9547b 100644 --- a/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache @@ -1 +1 @@ -6c465b3466d6b033362f776b9026e0f6319db705c736006e612bc04b20c3b80d +ca8f04f7ca657757a28c0e8abfce7eaad2aa52fcdf4495534a39ebbced49ce17 diff --git a/Knots/obj/Debug/net8.0/Knots.dll b/Knots/obj/Debug/net8.0/Knots.dll index 52a487f..0352e3e 100644 Binary files a/Knots/obj/Debug/net8.0/Knots.dll and b/Knots/obj/Debug/net8.0/Knots.dll differ diff --git a/Knots/obj/Debug/net8.0/Knots.pdb b/Knots/obj/Debug/net8.0/Knots.pdb index 6a2c09e..a7dde11 100644 Binary files a/Knots/obj/Debug/net8.0/Knots.pdb and b/Knots/obj/Debug/net8.0/Knots.pdb differ diff --git a/Knots/obj/Debug/net8.0/apphost.exe b/Knots/obj/Debug/net8.0/apphost.exe index 532b663..f2acea3 100644 Binary files a/Knots/obj/Debug/net8.0/apphost.exe and b/Knots/obj/Debug/net8.0/apphost.exe differ diff --git a/Knots/obj/Debug/net8.0/ref/Knots.dll b/Knots/obj/Debug/net8.0/ref/Knots.dll index cc78f44..04beb3e 100644 Binary files a/Knots/obj/Debug/net8.0/ref/Knots.dll and b/Knots/obj/Debug/net8.0/ref/Knots.dll differ diff --git a/Knots/obj/Debug/net8.0/refint/Knots.dll b/Knots/obj/Debug/net8.0/refint/Knots.dll index cc78f44..04beb3e 100644 Binary files a/Knots/obj/Debug/net8.0/refint/Knots.dll and b/Knots/obj/Debug/net8.0/refint/Knots.dll differ