Endpoint P2

This commit is contained in:
2026-03-26 16:38:04 +01:00
parent 579f50a2de
commit 07bd241e0c
15 changed files with 202 additions and 69 deletions

View File

@@ -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; }

View File

@@ -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<DeleteDiscussionDto>
{
public override void Configure()
{
Delete("/discussions");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteDiscussionDto req, CancellationToken ct)
{
Models.Discussion? discussion = mapper.Map<Models.Discussion>(req);
db.Discussions.Remove(discussion);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -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<GetDiscussionDto>
{
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<GetKeyDetailsDto>(databaseDiscussion);
await Send.OkAsync(keyDto, ct);
}
}

View File

@@ -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<DeleteGroupDto>
{
public override void Configure()
{
Delete("/groups");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteGroupDto req, CancellationToken ct)
{
Models.Group? group = mapper.Map<Models.Group>(req);
db.Groups.Remove(group);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -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<GetGroupDetailsDto>
{
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<GetKeyDetailsDto>(databaseGroup);
await Send.OkAsync(keyDto, ct);
}
}

View File

@@ -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<DeleteKeyDto>
{
public override void Configure()
{
Delete("/groups");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteKeyDto req, CancellationToken ct)
{
Models.Key? key = mapper.Map<Models.Key>(req);
db.Keys.Remove(key);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -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 <GetKeyDetailsDto>
{
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<GetKeyDetailsDto>(databaseKey);
await Send.OkAsync(keyDto, ct);
}
}

View File

@@ -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<DeleteMessageDto>
{
public override void Configure()
{
Delete("/messages");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteMessageDto req, CancellationToken ct)
{
Models.Message? message = mapper.Map<Models.Message>(req);
db.Messages.Remove(message);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -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 <GetMessageDetailsDto>
{
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<GetMessageDetailsDto>(databaseMessage);
await Send.OkAsync(messageDto, ct);
}
}

View File

@@ -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<DeleteRoleDto>
{
public override void Configure()
{
Delete("/roles");
AllowAnonymous();
}
public override async Task HandleAsync(DeleteRoleDto req, CancellationToken ct)
{
Models.Role? role = mapper.Map<Models.Role>(req);
db.Roles.Remove(role);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.Role;
public class GetRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetRoleDto>
public class GetRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint <GetRoleDto>
{
public override void Configure()
{
@@ -15,7 +15,7 @@ public class GetRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetRoleD
public override async Task HandleAsync(GetRoleDto req, CancellationToken ct)
{
Models.Role? databaseRole = await knotsDbContext.Roles.SingleOrDefaultAsync(x => 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 <GetRoleD
return;
}
GetRoleDto dto = new()
{
Libelle = databaseRole.Libelle,
};
await Send.OkAsync(dto, ct);
var roleDto = mapper.Map<GetRoleDto>(databaseRole);
await Send.OkAsync(roleDto, ct);
}
}

View File

@@ -3,7 +3,7 @@ using Knots.DTO.User;
namespace Knots.Endpoints.User;
public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint<CreateUserDto, GetUserDto>
public class CreateUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateUserDto, GetUserDto>
{
public override void Configure()
{
@@ -13,24 +13,9 @@ public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint<Create
public override async Task HandleAsync(CreateUserDto req, CancellationToken ct)
{
Models.User user = new()
{
Username = req.Username,
Description = req.Description,
Password = req.Password,
Email = req.Email,
Tel = req.Tel,
ProfilePicture = req.ProfilePicture
};
knotsDbContext.Add(user);
await knotsDbContext.SaveChangesAsync(ct);
GetUserDto response = new()
{
Username = user.Username
};
await Send.OkAsync(response, ct);
Models.User? user = mapper.Map<Models.User>(req);
db.Users.Add(user);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -4,25 +4,18 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class DeleteUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserDto, GetUserDetailsDto>
public class DeleteUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint <DeleteUserDto>
{
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<Models.User>(req);
db.Users.Add(user);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class GetAllUsersEndpoint(KnotsDbContext knotsDbContext) : EndpointWithoutRequest<List<GetUserDto>>
public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserDto>>
{
public override void Configure()
{
@@ -14,7 +14,7 @@ public class GetAllUsersEndpoint(KnotsDbContext knotsDbContext) : EndpointWithou
public override async Task HandleAsync(CancellationToken ct)
{
List<GetUserDto> users= await knotsDbContext.Users.Select(x => new GetUserDto()
List<GetUserDto> users= await db.Users.Select(x => new GetUserDto()
{
Username = x.Username,
}).ToListAsync(ct);

View File

@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserDto, GetUserDetailsDto>
public class GetUserEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint <GetUserDetailsDto>
{
public override void Configure()
{
@@ -12,26 +12,17 @@ public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <GetUserD
AllowAnonymous();
}
public override async Task HandleAsync(GetUserDto req, CancellationToken ct)
public override async Task HandleAsync(GetUserDetailsDto req, CancellationToken ct)
{
Models.User? databaseLogin = await knotsDbContext.Users.SingleOrDefaultAsync(x => 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<GetUserDetailsDto>(databaseUser);
await Send.OkAsync(userDto, ct);
}
}