Changements dans le Group Model + endpoints Patch des Groupes et Users

This commit is contained in:
oistig
2026-03-19 16:18:52 +01:00
parent 24613de57c
commit 7c2e77ed99
17 changed files with 226 additions and 14 deletions

View File

@@ -0,0 +1,7 @@
namespace Knots.DTO.Group;
public class UpdateGroupMembersAmountDto
{
public int Id { get; set; }
public int MembersAmount { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace Knots.DTO.Group;
public class UpdateGroupNameDto
{
public int Id { get; set; }
public string? Name { get; set; }
}

View File

@@ -1,6 +0,0 @@
namespace Knots.DTO.Group;
public class UpdateGroupNomDto
{
public string? Nom { get; set; }
}

View File

@@ -1,6 +0,0 @@
namespace Knots.DTO.Group;
public class UpdateGroupNombreMembresDto
{
public int NombreMembres { get; set; }
}

View File

@@ -2,5 +2,6 @@ namespace Knots.DTO.Group;
public class UpdateGroupProfilePictureDto public class UpdateGroupProfilePictureDto
{ {
public int Id { get; set; }
public string? ProfilePicture { get; set; } public string? ProfilePicture { get; set; }
} }

View File

@@ -2,5 +2,6 @@ namespace Knots.DTO.User;
public class UpdateUserEmailDto public class UpdateUserEmailDto
{ {
public int Id { get; set; }
public string? Email { get; set; } public string? Email { get; set; }
} }

View File

@@ -2,5 +2,6 @@ namespace Knots.DTO.User;
public class UpdateUserPasswordDto public class UpdateUserPasswordDto
{ {
public int Id { get; set; }
public string? Password { get; set; } public string? Password { get; set; }
} }

View File

@@ -2,5 +2,6 @@ namespace Knots.DTO.User;
public class UpdateUserProfilePictureDto public class UpdateUserProfilePictureDto
{ {
public int Id { get; set; }
public string? ProfilePicture { get; set; } public string? ProfilePicture { get; set; }
} }

View File

@@ -2,5 +2,7 @@ namespace Knots.DTO.User;
public class UpdateUsernameDto public class UpdateUsernameDto
{ {
public int Id { get; set; }
public string? Username { get; set; } public string? Username { get; set; }
} }

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.Group;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.Group;
public class PatchGroupMembersAmountEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateGroupMembersAmountDto>
{
public override void Configure()
{
Patch("/groups/{@Id}/membersAmount/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateGroupMembersAmountDto req, CancellationToken ct)
{
Models.Group? databaseGroup = await knotsDbContext.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseGroup is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseGroup.MembersAmount = req.MembersAmount;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.Group;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.Group;
public class PatchGroupNameEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateGroupNameDto>
{
public override void Configure()
{
Patch("/groups/{@Id}/name/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateGroupNameDto req, CancellationToken ct)
{
Models.Group? databaseGroup = await knotsDbContext.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseGroup is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseGroup.Name = req.Name;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.Group;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.Group;
public class PatchGroupProfilePictureEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateGroupProfilePictureDto>
{
public override void Configure()
{
Patch("/groups/{@Id}/profilePicture/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateGroupProfilePictureDto req, CancellationToken ct)
{
Models.Group? databaseGroup = await knotsDbContext.Groups.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseGroup is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseGroup.ProfilePicture = req.ProfilePicture;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUserEmailEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserEmailDto>
{
public override void Configure()
{
Patch("/users/{@Id}/email/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserEmailDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseUser.Email = req.Email;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserPasswordDto>
{
public override void Configure()
{
Patch("/users/{@Id}/password/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserPasswordDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseUser.Password = req.Password;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserProfilePictureDto>
{
public override void Configure()
{
Patch("/users/{@Id}/profilePicture/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserProfilePictureDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseUser.ProfilePicture = req.ProfilePicture;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUsernameEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUsernameDto>
{
public override void Configure()
{
Patch("/users/{@Id}/username/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUsernameDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseUser.Username = req.Username;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -6,7 +6,7 @@ namespace Knots.Models;
public class Group public class Group
{ {
[Key] public int Id { get; set; } [Key] public int Id { get; set; }
[Required, MaxLength(50)] public string? Nom { get; set; } [Required, MaxLength(50)] public string? Name { get; set; }
[Required] public int NombreMembres { get; set; } [Required] public int MembersAmount { get; set; }
public string? ProfilePicture { get; set; } public string? ProfilePicture { get; set; }
} }