diff --git a/Knots/DTO/User/UpdateUserDescriptionDto.cs b/Knots/DTO/User/UpdateUserDescriptionDto.cs index c65d9e6..1d23b37 100644 --- a/Knots/DTO/User/UpdateUserDescriptionDto.cs +++ b/Knots/DTO/User/UpdateUserDescriptionDto.cs @@ -2,5 +2,6 @@ namespace Knots.DTO.User; public class UpdateUserDescriptionDto { + public int Id { get; set; } public string? Description {get; set;} } \ No newline at end of file diff --git a/Knots/DTO/User/UpdateUserDto.cs b/Knots/DTO/User/UpdateUserDto.cs index d0f8a0a..d4397ca 100644 --- a/Knots/DTO/User/UpdateUserDto.cs +++ b/Knots/DTO/User/UpdateUserDto.cs @@ -2,10 +2,5 @@ namespace Knots.DTO.User; public class UpdateUserDto { - public string? Username { 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; } + public int Id { get; set; } } \ No newline at end of file diff --git a/Knots/DTO/User/UpdateUserTelDto.cs b/Knots/DTO/User/UpdateUserTelDto.cs deleted file mode 100644 index 3507bec..0000000 --- a/Knots/DTO/User/UpdateUserTelDto.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Knots.DTO.User; - -public class UpdateUserTelDto -{ - public string? Tel { get; set; } -} \ No newline at end of file diff --git a/Knots/DTO/User/UpdateUsernameDto.cs b/Knots/DTO/User/UpdateUsernameDto.cs index 8c45a0e..44d09e7 100644 --- a/Knots/DTO/User/UpdateUsernameDto.cs +++ b/Knots/DTO/User/UpdateUsernameDto.cs @@ -3,6 +3,5 @@ namespace Knots.DTO.User; public class UpdateUsernameDto { public int Id { get; set; } - public string? Username { get; set; } } \ No newline at end of file diff --git a/Knots/Endpoints/User/GetAllUsersEndpoint.cs b/Knots/Endpoints/User/GetAllUsersEndpoint.cs index ea0c6b0..d038b25 100644 --- a/Knots/Endpoints/User/GetAllUsersEndpoint.cs +++ b/Knots/Endpoints/User/GetAllUsersEndpoint.cs @@ -1,10 +1,11 @@ +using AutoMapper.QueryableExtensions; using FastEndpoints; using Knots.DTO.User; using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest> +public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest> { public override void Configure() { @@ -14,10 +15,9 @@ public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : public override async Task HandleAsync(CancellationToken ct) { - List users= await db.Users.Select(x => new GetUserDto() - { - Username = x.Username, - }).ToListAsync(ct); + var users = await db.Users + .ProjectTo(mapper.ConfigurationProvider) + .ToListAsync(ct); await Send.OkAsync(users, ct); } diff --git a/Knots/Endpoints/User/PatchUserContactEndpoint.cs b/Knots/Endpoints/User/PatchUserContactEndpoint.cs index ba38c26..187d8be 100644 --- a/Knots/Endpoints/User/PatchUserContactEndpoint.cs +++ b/Knots/Endpoints/User/PatchUserContactEndpoint.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class PatchUserContactEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -14,7 +14,7 @@ public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint< 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) { @@ -39,7 +39,9 @@ public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint< databaseUser.Tel = databaseUser.Tel; } - await knotsDbContext.SaveChangesAsync(ct); + mapper.Map(req, databaseUser); + + await db.SaveChangesAsync(ct); await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/PatchUserDescriptionEndpoint.cs b/Knots/Endpoints/User/PatchUserDescriptionEndpoint.cs new file mode 100644 index 0000000..78b7b40 --- /dev/null +++ b/Knots/Endpoints/User/PatchUserDescriptionEndpoint.cs @@ -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 +{ + 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); + } +} \ No newline at end of file diff --git a/Knots/Endpoints/User/PatchUserPasswordEndpoint.cs b/Knots/Endpoints/User/PatchUserPasswordEndpoint.cs index cf84060..d565fed 100644 --- a/Knots/Endpoints/User/PatchUserPasswordEndpoint.cs +++ b/Knots/Endpoints/User/PatchUserPasswordEndpoint.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class PatchUserPasswordEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -14,7 +14,7 @@ public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint 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) { @@ -22,8 +22,9 @@ public class PatchUserPasswordEndpoint(KnotsDbContext knotsDbContext) : Endpoint return; } - databaseUser.Password = req.Password; - await knotsDbContext.SaveChangesAsync(ct); + mapper.Map(req, databaseUser); + + await db.SaveChangesAsync(ct); await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/PatchUserProfilePictureEndpoint.cs b/Knots/Endpoints/User/PatchUserProfilePictureEndpoint.cs index 979965c..bc9ea93 100644 --- a/Knots/Endpoints/User/PatchUserProfilePictureEndpoint.cs +++ b/Knots/Endpoints/User/PatchUserProfilePictureEndpoint.cs @@ -4,17 +4,17 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class PatchUserProfilePictureEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { - Patch("/users/{@Id}/profilePicture/", x => new {x.Id}); + 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); + Models.User? databaseUser = await db.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); if (databaseUser is null) { @@ -22,8 +22,9 @@ public class PatchUserProfilePictureEndpoint(KnotsDbContext knotsDbContext) : En return; } - databaseUser.ProfilePicture = req.ProfilePicture; - await knotsDbContext.SaveChangesAsync(ct); + mapper.Map(req, databaseUser); + + await db.SaveChangesAsync(ct); await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/Knots/Endpoints/User/PatchUsernameEndpoint.cs b/Knots/Endpoints/User/PatchUsernameEndpoint.cs index ff326e3..1c0a5b0 100644 --- a/Knots/Endpoints/User/PatchUsernameEndpoint.cs +++ b/Knots/Endpoints/User/PatchUsernameEndpoint.cs @@ -5,17 +5,17 @@ using Microsoft.EntityFrameworkCore; namespace Knots.Endpoints.User; -public class PatchUsernameEndpoint(KnotsDbContext knotsDbContext) : Endpoint +public class PatchUsernameEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { - Patch("/users/{@Id}/username/", x => new {x.Id}); + Patch("/users/{@Id}/username/"); AllowAnonymous(); } 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) { @@ -23,8 +23,9 @@ public class PatchUsernameEndpoint(KnotsDbContext knotsDbContext) : Endpoint -{ - 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); - } -} \ No newline at end of file diff --git a/Knots/Profiles/UserProfile.cs b/Knots/Profiles/UserProfile.cs index 99074fb..941f786 100644 --- a/Knots/Profiles/UserProfile.cs +++ b/Knots/Profiles/UserProfile.cs @@ -18,13 +18,11 @@ public class UserProfile : Profile CreateMap(); CreateMap(); CreateMap(); - CreateMap(); CreateMap(); CreateMap(); CreateMap(); CreateMap(); - CreateMap(); CreateMap(); CreateMap(); }