Patch endpoint

This commit is contained in:
2026-03-26 17:49:43 +01:00
parent 07bd241e0c
commit 1a3ca3fc2d
12 changed files with 59 additions and 83 deletions

View File

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

View File

@@ -2,10 +2,5 @@ namespace Knots.DTO.User;
public class UpdateUserDto public class UpdateUserDto
{ {
public string? Username { get; set; } public int Id { get; set; }
public string? Description {get; set;}
public string? Password { get; set; }
public string? Email { get; set; }
public string? Tel { get; set; }
public string? ProfilePicture { get; set; }
} }

View File

@@ -1,6 +0,0 @@
namespace Knots.DTO.User;
public class UpdateUserTelDto
{
public string? Tel { get; set; }
}

View File

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

View File

@@ -1,10 +1,11 @@
using AutoMapper.QueryableExtensions;
using FastEndpoints; using FastEndpoints;
using Knots.DTO.User; using Knots.DTO.User;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User; namespace Knots.Endpoints.User;
public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserDto>> public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserDetailsDto>>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,10 +15,9 @@ public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) :
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
List<GetUserDto> users= await db.Users.Select(x => new GetUserDto() var users = await db.Users
{ .ProjectTo<GetUserDetailsDto>(mapper.ConfigurationProvider)
Username = x.Username, .ToListAsync(ct);
}).ToListAsync(ct);
await Send.OkAsync(users, ct); await Send.OkAsync(users, ct);
} }

View File

@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User; namespace Knots.Endpoints.User;
public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserContactDto> public class PatchUserContactEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateUserContactDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,7 +14,7 @@ public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint<
public override async Task HandleAsync(UpdateUserContactDto req, CancellationToken ct) public override async Task HandleAsync(UpdateUserContactDto req, CancellationToken ct)
{ {
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null) if (databaseUser is null)
{ {
@@ -39,7 +39,9 @@ public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint<
databaseUser.Tel = databaseUser.Tel; databaseUser.Tel = databaseUser.Tel;
} }
await knotsDbContext.SaveChangesAsync(ct); mapper.Map(req, databaseUser);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(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 PatchUserDescriptionEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateUserDescriptionDto>
{
public override void Configure()
{
Patch("/users/{@Id}/description/");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserDescriptionDto req, CancellationToken ct)
{
Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
mapper.Map(req, databaseUser);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User; namespace Knots.Endpoints.User;
public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserPasswordDto> public class PatchUserPasswordEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateUserPasswordDto>
{ {
public override void Configure() public override void Configure()
{ {
@@ -14,7 +14,7 @@ public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint
public override async Task HandleAsync(UpdateUserPasswordDto req, CancellationToken ct) public override async Task HandleAsync(UpdateUserPasswordDto req, CancellationToken ct)
{ {
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null) if (databaseUser is null)
{ {
@@ -22,8 +22,9 @@ public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint
return; return;
} }
databaseUser.Password = req.Password; mapper.Map(req, databaseUser);
await knotsDbContext.SaveChangesAsync(ct);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }
} }

View File

@@ -4,17 +4,17 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User; namespace Knots.Endpoints.User;
public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserProfilePictureDto> public class PatchUserProfilePictureEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateUserProfilePictureDto>
{ {
public override void Configure() public override void Configure()
{ {
Patch("/users/{@Id}/profilePicture/", x => new {x.Id}); Patch("/users/{@Id}/profilepicture/", x => new {x.Id});
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(UpdateUserProfilePictureDto req, CancellationToken ct) public override async Task HandleAsync(UpdateUserProfilePictureDto req, CancellationToken ct)
{ {
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null) if (databaseUser is null)
{ {
@@ -22,8 +22,9 @@ public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : En
return; return;
} }
databaseUser.ProfilePicture = req.ProfilePicture; mapper.Map(req, databaseUser);
await knotsDbContext.SaveChangesAsync(ct);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }
} }

View File

@@ -5,17 +5,17 @@ using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User; namespace Knots.Endpoints.User;
public class PatchUsernameEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUsernameDto> public class PatchUsernameEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint<UpdateUsernameDto>
{ {
public override void Configure() public override void Configure()
{ {
Patch("/users/{@Id}/username/", x => new {x.Id}); Patch("/users/{@Id}/username/");
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(UpdateUsernameDto req, CancellationToken ct) public override async Task HandleAsync(UpdateUsernameDto req, CancellationToken ct)
{ {
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null) if (databaseUser is null)
{ {
@@ -23,8 +23,9 @@ public class PatchUsernameEndpoint(KnotsDbContext knotsDbContext) : Endpoint<Upd
return; return;
} }
databaseUser.Username = req.Username; mapper.Map(req, databaseUser);
await knotsDbContext.SaveChangesAsync(ct);
await db.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }
} }

View File

@@ -1,46 +0,0 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class UpdateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint <UpdateUserDto, GetUserDetailsDto>
{
public override void Configure()
{
Put ("/users/{@Id}", x => new { x.Username });
}
public override async Task HandleAsync(UpdateUserDto 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;
}
else
{
databaseUser.Username = req.Username;
databaseUser.Password = req.Password;
databaseUser.Description = req.Description;
databaseUser.Tel = req.Tel;
databaseUser.Email = req.Email;
databaseUser.ProfilePicture = req.ProfilePicture;
}
await knotsDbContext.SaveChangesAsync(ct);
GetUserDetailsDto dto = new()
{
Username = databaseUser.Username,
Password = databaseUser.Password,
Description = databaseUser.Description,
Tel = databaseUser.Tel,
Email = databaseUser.Email,
ProfilePicture = databaseUser.ProfilePicture
};
await Send.OkAsync(dto, ct);
}
}

View File

@@ -18,13 +18,11 @@ public class UserProfile : Profile
CreateMap<UpdateUsernameDto, User>(); CreateMap<UpdateUsernameDto, User>();
CreateMap<UpdateUserProfilePictureDto, User>(); CreateMap<UpdateUserProfilePictureDto, User>();
CreateMap<UpdateUserPasswordDto, User>(); CreateMap<UpdateUserPasswordDto, User>();
CreateMap<UpdateUserTelDto, User>();
CreateMap<User, UpdateUserContactDto>(); CreateMap<User, UpdateUserContactDto>();
CreateMap<User, UpdateUserDto>(); CreateMap<User, UpdateUserDto>();
CreateMap<User, UpdateUserDescriptionDto>(); CreateMap<User, UpdateUserDescriptionDto>();
CreateMap<User, UpdateUserProfilePictureDto>(); CreateMap<User, UpdateUserProfilePictureDto>();
CreateMap<User, UpdateUserTelDto>();
CreateMap<User, UpdateUserPasswordDto>(); CreateMap<User, UpdateUserPasswordDto>();
CreateMap<User, CreateUserDto>(); CreateMap<User, CreateUserDto>();
} }