diff --git a/Knots/Endpoints/Discussion/GetDiscussionMembersEndpoint.cs b/Knots/Endpoints/Discussion/GetDiscussionMembersEndpoint.cs new file mode 100644 index 0000000..9adf1a1 --- /dev/null +++ b/Knots/Endpoints/Discussion/GetDiscussionMembersEndpoint.cs @@ -0,0 +1,35 @@ +using System.Security.Claims; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace Knots.Endpoints.Discussion; + +public class GetDiscussionMembersEndpoint(KnotsDbContext db) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/discussions/{discussionId}/members"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int discussionId = Route("discussionId"); + + Models.Discussion? discussion = await db.Discussions + .Include(d => d.UserDiscussions) + .ThenInclude(ud => ud.User) + .SingleOrDefaultAsync(d => d.Id == discussionId, ct); + + if (discussion is null) + { + await SendNotFoundAsync(ct); + return; + } + + List members = discussion.UserDiscussions + .Select(ud => ud.User.Username!) + .ToList(); + + await SendOkAsync(members, ct); + } +} \ No newline at end of file diff --git a/Knots/Endpoints/Discussion/GetDiscussionMembersWithRolesEndpoint.cs b/Knots/Endpoints/Discussion/GetDiscussionMembersWithRolesEndpoint.cs new file mode 100644 index 0000000..456eb27 --- /dev/null +++ b/Knots/Endpoints/Discussion/GetDiscussionMembersWithRolesEndpoint.cs @@ -0,0 +1,52 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace Knots.Endpoints.Discussion; + +public class GetDiscussionMembersWithRolesEndpoint(KnotsDbContext db) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/discussions/{discussionId}/members/roles"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int discussionId = Route("discussionId"); + + Models.Discussion? discussion = await db.Discussions + .Include(d => d.Group) + .ThenInclude(g => g!.GroupUsers) + .ThenInclude(gu => gu.User) + .Include(d => d.Group) + .ThenInclude(g => g!.GroupUsers) + .ThenInclude(gu => gu.Role) + .SingleOrDefaultAsync(d => d.Id == discussionId, ct); + + if (discussion?.Group is null) + { + await SendNotFoundAsync(ct); + return; + } + + List members = discussion.Group.GroupUsers + .Select(gu => new MemberWithRoleDto + { + UserId = gu.UserId, + Username = gu.User.Username!, + RoleId = gu.RoleId, + RoleLibelle = gu.Role?.Libelle + }) + .ToList(); + + await SendOkAsync(members, ct); + } +} + +public class MemberWithRoleDto +{ + public int UserId { get; set; } + public string Username { get; set; } = ""; + public int? RoleId { get; set; } + public string? RoleLibelle { get; set; } +} \ No newline at end of file diff --git a/Knots/Endpoints/Role/AssignRoleEndpoint.cs b/Knots/Endpoints/Role/AssignRoleEndpoint.cs new file mode 100644 index 0000000..1f93e4e --- /dev/null +++ b/Knots/Endpoints/Role/AssignRoleEndpoint.cs @@ -0,0 +1,45 @@ +using FastEndpoints; +using Knots.Models; +using Microsoft.EntityFrameworkCore; + +namespace Knots.Endpoints.Role; + +public class AssignRoleEndpoint(KnotsDbContext db) : Endpoint +{ + public override void Configure() + { + Post("/groups/{groupId}/members/{userId}/role"); + } + + public override async Task HandleAsync(AssignRoleRequest req, CancellationToken ct) + { + int groupId = Route("groupId"); + int userId = Route("userId"); + + GroupUser? groupUser = await db.GroupUsers + .SingleOrDefaultAsync(gu => gu.GroupId == groupId && gu.UserId == userId, ct); + + if (groupUser is null) + { + await SendNotFoundAsync(ct); + return; + } + + bool roleExists = await db.Roles.AnyAsync(r => r.Id == req.RoleId, ct); + if (!roleExists) + { + await SendNotFoundAsync(ct); + return; + } + + groupUser.RoleId = req.RoleId; + await db.SaveChangesAsync(ct); + + await SendOkAsync(ct); + } +} + +public class AssignRoleRequest +{ + public int RoleId { get; set; } +} \ No newline at end of file diff --git a/Knots/Endpoints/Role/CreateRoleEndpoint.cs b/Knots/Endpoints/Role/CreateRoleEndpoint.cs index 420e983..777ccf5 100644 --- a/Knots/Endpoints/Role/CreateRoleEndpoint.cs +++ b/Knots/Endpoints/Role/CreateRoleEndpoint.cs @@ -1,22 +1,32 @@ using FastEndpoints; -using Knots.DTO.Role; -using Knots.DTO.User; +using Knots.Models; namespace Knots.Endpoints.Role; -public class CreateRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint +public class CreateRoleEndpoint(KnotsDbContext db) : Endpoint { public override void Configure() { Post("/roles"); - AllowAnonymous(); } - - public override async Task HandleAsync(CreateRoleDto req, CancellationToken ct) + + public override async Task HandleAsync(CreateRoleRequest req, CancellationToken ct) { - Models.Role? role = mapper.Map(req); + Models.Role role = new() { Libelle = req.Libelle }; db.Roles.Add(role); await db.SaveChangesAsync(ct); - await SendNoContentAsync(ct); + + await SendOkAsync(new RoleDto { Id = role.Id, Libelle = role.Libelle! }, ct); } +} + +public class CreateRoleRequest +{ + public string Libelle { get; set; } = ""; +} + +public class RoleDto +{ + public int Id { get; set; } + public string Libelle { 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 757337a..b763aa5 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 62fe9f5..e589a3c 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 d946266..c0a430e 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 bbcf5b1..b345bd9 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+f1cdc5de19a96d553f0eaf2fbe65392602c54f85")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1d2f96b2f44a77ffc60910e80ff641d569336938")] [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 5a32b3c..daa44bc 100644 --- a/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.AssemblyInfoInputs.cache @@ -1 +1 @@ -0618e480c1221c208fb53c91259f81c85675af45b5772bccc9fee873e927c02b +811716b679de87289165cd43ea2a1df8f028fb5afe28ba98b508d691f4d0a71f diff --git a/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache index 6fdabe8..f074142 100644 --- a/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache +++ b/Knots/obj/Debug/net8.0/Knots.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -1afb5c57f375954b5501d52f6ccbfcdd0c9a4a7d75abca85439c878c12264607 +eae1c606f235f5f51466aca07da485bdaa9b0072133d65d312d41bb94837718a diff --git a/Knots/obj/Debug/net8.0/Knots.dll b/Knots/obj/Debug/net8.0/Knots.dll index 757337a..b763aa5 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 d946266..c0a430e 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 62fe9f5..e589a3c 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 5e341dc..5adbafb 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 5e341dc..5adbafb 100644 Binary files a/Knots/obj/Debug/net8.0/refint/Knots.dll and b/Knots/obj/Debug/net8.0/refint/Knots.dll differ