Début endpoints

This commit is contained in:
2026-03-26 15:34:49 +01:00
parent df6e559f00
commit 579f50a2de
15 changed files with 182 additions and 158 deletions
@@ -1,32 +1,21 @@
using FastEndpoints;
using Knots.DTO.Discussion;
using Knots.DTO.User;
namespace Knots.Endpoints.Discussion;
public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint<CreateDiscussionDto, GetDiscussionDto>
public class CreateDiscussionEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateDiscussionDto>
{
public override void Configure()
{
Post("/users");
Post("/discussions");
AllowAnonymous();
}
public override async Task HandleAsync(CreateDiscussionDto req, CancellationToken ct)
{
Models.Discussion discussion = new()
{
};
knotsDbContext.Add(discussion);
await knotsDbContext.SaveChangesAsync(ct);
GetDiscussionDto response = new()
{
Id = discussion.Id,
};
await Send.OkAsync(response, ct);
Models.Discussion? discussion = mapper.Map<Models.Discussion>(req);
db.Discussions.Add(discussion);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}
+16 -1
View File
@@ -1,6 +1,21 @@
using Knots.DTO.Group;
using FastEndpoints;
namespace Knots.Endpoints.Group;
public class CreateGroupEndpoint
public class CreateGroupEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateGroupDto>
{
public override void Configure()
{
Post("/groups");
AllowAnonymous();
}
public override async Task HandleAsync(CreateGroupDto req, CancellationToken ct)
{
Models.Group? group = mapper.Map<Models.Group>(req);
db.Groups.Add(group);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}
+16 -1
View File
@@ -1,6 +1,21 @@
using Knots.DTO.Key;
using FastEndpoints;
namespace Knots.Endpoints.Key;
public class CreateKeyEndpoint
public class CreateKeyEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateKeyDto>
{
public override void Configure()
{
Post("/groups");
AllowAnonymous();
}
public override async Task HandleAsync(CreateKeyDto req, CancellationToken ct)
{
Models.Key? key = mapper.Map<Models.Key>(req);
db.Keys.Add(key);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}
@@ -1,6 +1,21 @@
using FastEndpoints;
using Knots.DTO.Message;
namespace Knots.Endpoints.Message;
public class CreateMessageEndpoint
public class CreateMessageEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateMessageDto>
{
public override void Configure()
{
Post("/messages");
AllowAnonymous();
}
public override async Task HandleAsync(CreateMessageDto req, CancellationToken ct)
{
Models.Message? message = mapper.Map<Models.Message>(req);
db.Messages.Add(message);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}
+5 -15
View File
@@ -4,7 +4,7 @@ using Knots.DTO.User;
namespace Knots.Endpoints.Role;
public class CreateRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint<CreateRoleDto, GetRoleDto>
public class CreateRoleEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<CreateRoleDto>
{
public override void Configure()
{
@@ -14,19 +14,9 @@ public class CreateRoleEndpoint(KnotsDbContext knotsDbContext) : Endpoint<Create
public override async Task HandleAsync(CreateRoleDto req, CancellationToken ct)
{
Models.Role role = new()
{
Libelle = req.Libelle
};
knotsDbContext.Add(role);
await knotsDbContext.SaveChangesAsync(ct);
GetRoleDto response = new()
{
Libelle = role.Libelle
};
await Send.OkAsync(response, ct);
Models.Role? role = mapper.Map<Models.Role>(req);
db.Roles.Add(role);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}