From 07bd241e0ca272037e6cd053901041b4f684356b Mon Sep 17 00:00:00 2001 From: carteronm Date: Thu, 26 Mar 2026 16:38:04 +0100 Subject: [PATCH] Endpoint P2 --- Knots/DTO/Message/GetMessageDetailsDto.cs | 1 + .../Discussion/DeleteDiscussionEndpoint.cs | 17 +++++++++++- .../Discussion/GetDiscussionEndpoint.cs | 25 ++++++++++++++++- Knots/Endpoints/Group/DeleteGroupEndpoint.cs | 17 +++++++++++- Knots/Endpoints/Group/GetGroupEndpoint.cs | 25 ++++++++++++++++- Knots/Endpoints/Key/DeleteKeyEndpoint.cs | 17 +++++++++++- Knots/Endpoints/Key/GetKeyEndpoint.cs | 27 +++++++++++++++++-- .../Message/DeleteMessageEndpoint.cs | 17 +++++++++++- Knots/Endpoints/Message/GetMessageEndpoint.cs | 27 +++++++++++++++++-- Knots/Endpoints/Role/DeleteRoleEndpoint.cs | 17 +++++++++++- Knots/Endpoints/Role/GetRoleEndpoint.cs | 12 +++------ Knots/Endpoints/User/CreateUserEndpoint.cs | 25 ++++------------- Knots/Endpoints/User/DeleteUserEndpoint.cs | 19 +++++-------- Knots/Endpoints/User/GetAllUsersEndpoint.cs | 4 +-- Knots/Endpoints/User/GetUserEndpoint.cs | 21 +++++---------- 15 files changed, 202 insertions(+), 69 deletions(-) diff --git a/Knots/DTO/Message/GetMessageDetailsDto.cs b/Knots/DTO/Message/GetMessageDetailsDto.cs index e1373c5..cc5eab0 100644 --- a/Knots/DTO/Message/GetMessageDetailsDto.cs +++ b/Knots/DTO/Message/GetMessageDetailsDto.cs @@ -2,6 +2,7 @@ namespace Knots.DTO.Message; public class GetMessageDetailsDto { + public int Id { get; set; } public string? Contenu { get; set; } public DateTime Date { get; set; } public Boolean Type { get; set; } diff --git a/Knots/Endpoints/Discussion/DeleteDiscussionEndpoint.cs b/Knots/Endpoints/Discussion/DeleteDiscussionEndpoint.cs index 236d400..9c6b0f8 100644 --- a/Knots/Endpoints/Discussion/DeleteDiscussionEndpoint.cs +++ b/Knots/Endpoints/Discussion/DeleteDiscussionEndpoint.cs @@ -1,6 +1,21 @@ +using FastEndpoints; +using Knots.DTO.Discussion; + namespace Knots.Endpoints.Discussion; -public class DeleteDiscussionEndpoint +public class DeleteDiscussionEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Delete("/discussions"); + AllowAnonymous(); + } + public override async Task HandleAsync(DeleteDiscussionDto req, CancellationToken ct) + { + Models.Discussion? discussion = mapper.Map(req); + db.Discussions.Remove(discussion); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Discussion/GetDiscussionEndpoint.cs b/Knots/Endpoints/Discussion/GetDiscussionEndpoint.cs index 82c9c6b..ae5e89f 100644 --- a/Knots/Endpoints/Discussion/GetDiscussionEndpoint.cs +++ b/Knots/Endpoints/Discussion/GetDiscussionEndpoint.cs @@ -1,6 +1,29 @@ +using Knots.DTO.Discussion; +using Knots.DTO.Key; +using Microsoft.EntityFrameworkCore; +using FastEndpoints; + namespace Knots.Endpoints.Discussion; -public class GetDiscussionEndpoint +public class GetDiscussionEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Get("/groups"); + AllowAnonymous(); + } + public override async Task HandleAsync(GetDiscussionDto req, CancellationToken ct) + { + Models.Discussion? databaseDiscussion = await db.Discussions.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseDiscussion == null) + { + await Send.NotFoundAsync(ct); + return; + } + + var keyDto = mapper.Map(databaseDiscussion); + await Send.OkAsync(keyDto, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Group/DeleteGroupEndpoint.cs b/Knots/Endpoints/Group/DeleteGroupEndpoint.cs index b2a297f..7542efb 100644 --- a/Knots/Endpoints/Group/DeleteGroupEndpoint.cs +++ b/Knots/Endpoints/Group/DeleteGroupEndpoint.cs @@ -1,6 +1,21 @@ +using FastEndpoints; +using Knots.DTO.Group; + namespace Knots.Endpoints.Group; -public class DeleteGroupEndpoint +public class DeleteGroupEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Delete("/groups"); + AllowAnonymous(); + } + public override async Task HandleAsync(DeleteGroupDto req, CancellationToken ct) + { + Models.Group? group = mapper.Map(req); + db.Groups.Remove(group); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Group/GetGroupEndpoint.cs b/Knots/Endpoints/Group/GetGroupEndpoint.cs index 52cd58f..7d20813 100644 --- a/Knots/Endpoints/Group/GetGroupEndpoint.cs +++ b/Knots/Endpoints/Group/GetGroupEndpoint.cs @@ -1,6 +1,29 @@ +using FastEndpoints; +using Knots.DTO.Group; +using Knots.DTO.Key; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.Group; -public class GetGroupEndpoint +public class GetGroupEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Get("/groups"); + AllowAnonymous(); + } + public override async Task HandleAsync(GetGroupDetailsDto req, CancellationToken ct) + { + Models.Group? databaseGroup = await db.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseGroup == null) + { + await Send.NotFoundAsync(ct); + return; + } + + var keyDto = mapper.Map(databaseGroup); + await Send.OkAsync(keyDto, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Key/DeleteKeyEndpoint.cs b/Knots/Endpoints/Key/DeleteKeyEndpoint.cs index 92436d6..428679b 100644 --- a/Knots/Endpoints/Key/DeleteKeyEndpoint.cs +++ b/Knots/Endpoints/Key/DeleteKeyEndpoint.cs @@ -1,6 +1,21 @@ +using FastEndpoints; +using Knots.DTO.Key; + namespace Knots.Endpoints.Key; -public class DeleteKeyEndpoint +public class DeleteKeyEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Delete("/groups"); + AllowAnonymous(); + } + public override async Task HandleAsync(DeleteKeyDto req, CancellationToken ct) + { + Models.Key? key = mapper.Map(req); + db.Keys.Remove(key); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Key/GetKeyEndpoint.cs b/Knots/Endpoints/Key/GetKeyEndpoint.cs index e890914..a612859 100644 --- a/Knots/Endpoints/Key/GetKeyEndpoint.cs +++ b/Knots/Endpoints/Key/GetKeyEndpoint.cs @@ -1,6 +1,29 @@ +using FastEndpoints; +using Knots.DTO.Key; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.Key; -public class GetKeyEndpoint +public class GetKeyEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { - + public override void Configure() + { + Get("/keys/{@Id}"); + AllowAnonymous(); + } + + public override async Task HandleAsync(GetKeyDetailsDto req, CancellationToken ct) + { + Models.Key? databaseKey = await db.Keys.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseKey == null) + { + await Send.NotFoundAsync(ct); + return; + } + + var keyDto = mapper.Map(databaseKey); + await Send.OkAsync(keyDto, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Message/DeleteMessageEndpoint.cs b/Knots/Endpoints/Message/DeleteMessageEndpoint.cs index 63691ad..8f38442 100644 --- a/Knots/Endpoints/Message/DeleteMessageEndpoint.cs +++ b/Knots/Endpoints/Message/DeleteMessageEndpoint.cs @@ -1,6 +1,21 @@ +using FastEndpoints; +using Knots.DTO.Message; + namespace Knots.Endpoints.Message; -public class DeleteMessageEndpoint +public class DeleteMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Delete("/messages"); + AllowAnonymous(); + } + public override async Task HandleAsync(DeleteMessageDto req, CancellationToken ct) + { + Models.Message? message = mapper.Map(req); + db.Messages.Remove(message); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Message/GetMessageEndpoint.cs b/Knots/Endpoints/Message/GetMessageEndpoint.cs index d23b69e..bb3a1d3 100644 --- a/Knots/Endpoints/Message/GetMessageEndpoint.cs +++ b/Knots/Endpoints/Message/GetMessageEndpoint.cs @@ -1,6 +1,29 @@ +using FastEndpoints; +using Knots.DTO.Message; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.Message; -public class GetMessageEndpoint +public class GetMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { - + public override void Configure() + { + Get("/messages/{@Id}"); + AllowAnonymous(); + } + + public override async Task HandleAsync(GetMessageDetailsDto req, CancellationToken ct) + { + Models.Message? databaseMessage = await db.Messages.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseMessage == null) + { + await Send.NotFoundAsync(ct); + return; + } + + var messageDto = mapper.Map(databaseMessage); + await Send.OkAsync(messageDto, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Role/DeleteRoleEndpoint.cs b/Knots/Endpoints/Role/DeleteRoleEndpoint.cs index ce3976d..c50d44d 100644 --- a/Knots/Endpoints/Role/DeleteRoleEndpoint.cs +++ b/Knots/Endpoints/Role/DeleteRoleEndpoint.cs @@ -1,6 +1,21 @@ +using FastEndpoints; +using Knots.DTO.Role; + namespace Knots.Endpoints.Role; -public class DeleteRoleEndpoint +public class DeleteRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { + public override void Configure() + { + Delete("/roles"); + AllowAnonymous(); + } + public override async Task HandleAsync(DeleteRoleDto req, CancellationToken ct) + { + Models.Role? role = mapper.Map(req); + db.Roles.Remove(role); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/Role/GetRoleEndpoint.cs b/Knots/Endpoints/Role/GetRoleEndpoint.cs index 22f86d5..758d280 100644 --- a/Knots/Endpoints/Role/GetRoleEndpoint.cs +++ b/Knots/Endpoints/Role/GetRoleEndpoint.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.Role; -public class GetRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class GetRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -15,7 +15,7 @@ public class GetRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint x.Libelle == req.Libelle, cancellationToken: ct); + Models.Role? databaseRole = await db.Roles.SingleOrDefaultAsync(x => x.Libelle == req.Libelle, cancellationToken: ct); if (databaseRole == null) { @@ -23,11 +23,7 @@ public class GetRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint (databaseRole); + await Send.OkAsync(roleDto, ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/CreateUserEndpoint.cs b/Knots/Endpoints/User/CreateUserEndpoint.cs index 54e562a..8dbabf5 100644 --- a/Knots/Endpoints/User/CreateUserEndpoint.cs +++ b/Knots/Endpoints/User/CreateUserEndpoint.cs @@ -3,7 +3,7 @@ using Knots.DTO.User; namespace Knots.Endpoints.User; -public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class CreateUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -13,24 +13,9 @@ public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint(req); + db.Users.Add(user); + await db.SaveChangesAsync(ct); + await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/DeleteUserEndpoint.cs b/Knots/Endpoints/User/DeleteUserEndpoint.cs index ceb6b70..a18583e 100644 --- a/Knots/Endpoints/User/DeleteUserEndpoint.cs +++ b/Knots/Endpoints/User/DeleteUserEndpoint.cs @@ -4,25 +4,18 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class DeleteUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class DeleteUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { - Delete ("/users/{@Id}", x => new { x.Username }); + Delete ("/users/{@Id}"); } - public override async Task HandleAsync(GetUserDto req, CancellationToken ct) + public override async Task HandleAsync(DeleteUserDto req, CancellationToken ct) { - Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct); - - if (databaseUser == null) - { - await Send.NotFoundAsync(ct); - return; - } - knotsDbContext.Users.Remove(databaseUser); - await knotsDbContext.SaveChangesAsync(ct); - + Models.User? user = mapper.Map(req); + db.Users.Add(user); + await db.SaveChangesAsync(ct); await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/GetAllUsersEndpoint.cs b/Knots/Endpoints/User/GetAllUsersEndpoint.cs index 20dd935..ea0c6b0 100644 --- a/Knots/Endpoints/User/GetAllUsersEndpoint.cs +++ b/Knots/Endpoints/User/GetAllUsersEndpoint.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class GetAllUsersEndpoint(KnotsDbContext knotsDbContext) : EndpointWithoutRequest> +public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest> { public override void Configure() { @@ -14,7 +14,7 @@ public class GetAllUsersEndpoint(KnotsDbContext knotsDbContext) : EndpointWithou public override async Task HandleAsync(CancellationToken ct) { - List users= await knotsDbContext.Users.Select(x => new GetUserDto() + List users= await db.Users.Select(x => new GetUserDto() { Username = x.Username, }).ToListAsync(ct); diff --git a/Knots/Endpoints/User/GetUserEndpoint.cs b/Knots/Endpoints/User/GetUserEndpoint.cs index 619a803..ab73594 100644 --- a/Knots/Endpoints/User/GetUserEndpoint.cs +++ b/Knots/Endpoints/User/GetUserEndpoint.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class GetUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -12,26 +12,17 @@ public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint x.Username == req.Username, cancellationToken: ct); + Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct); - if (databaseLogin == null) + if (databaseUser == null) { await Send.NotFoundAsync(ct); return; } - GetUserDetailsDto dto = new() - { - Username = databaseLogin.Username, - Description = databaseLogin.Description, - Password = databaseLogin.Password, - Email = databaseLogin.Email, - Tel = databaseLogin.Tel, - ProfilePicture = databaseLogin.ProfilePicture - }; - - await Send.OkAsync(dto, ct); + var userDto = mapper.Map(databaseUser); + await Send.OkAsync(userDto, ct); } } \ No newline at end of file