Patch endpoint
This commit is contained in:
@@ -2,5 +2,6 @@ namespace Knots.DTO.User;
|
||||
|
||||
public class UpdateUserDescriptionDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Description {get; set;}
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Knots.DTO.User;
|
||||
|
||||
public class UpdateUserTelDto
|
||||
{
|
||||
public string? Tel { get; set; }
|
||||
}
|
||||
@@ -3,6 +3,5 @@ namespace Knots.DTO.User;
|
||||
public class UpdateUsernameDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string? Username { get; set; }
|
||||
}
|
||||
@@ -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<List<GetUserDto>>
|
||||
public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserDetailsDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,10 +15,9 @@ public class GetAllUsersEndpoint(KnotsDbContext db, AutoMapper.IMapper mapper) :
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetUserDto> users= await db.Users.Select(x => new GetUserDto()
|
||||
{
|
||||
Username = x.Username,
|
||||
}).ToListAsync(ct);
|
||||
var users = await db.Users
|
||||
.ProjectTo<GetUserDetailsDto>(mapper.ConfigurationProvider)
|
||||
.ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(users, ct);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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()
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
30
Knots/Endpoints/User/PatchUserDescriptionEndpoint.cs
Normal file
30
Knots/Endpoints/User/PatchUserDescriptionEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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()
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -4,17 +4,17 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,17 @@ using Microsoft.EntityFrameworkCore;
|
||||
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()
|
||||
{
|
||||
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<Upd
|
||||
return;
|
||||
}
|
||||
|
||||
databaseUser.Username = req.Username;
|
||||
await knotsDbContext.SaveChangesAsync(ct);
|
||||
mapper.Map(req, databaseUser);
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -18,13 +18,11 @@ public class UserProfile : Profile
|
||||
CreateMap<UpdateUsernameDto, User>();
|
||||
CreateMap<UpdateUserProfilePictureDto, User>();
|
||||
CreateMap<UpdateUserPasswordDto, User>();
|
||||
CreateMap<UpdateUserTelDto, User>();
|
||||
|
||||
CreateMap<User, UpdateUserContactDto>();
|
||||
CreateMap<User, UpdateUserDto>();
|
||||
CreateMap<User, UpdateUserDescriptionDto>();
|
||||
CreateMap<User, UpdateUserProfilePictureDto>();
|
||||
CreateMap<User, UpdateUserTelDto>();
|
||||
CreateMap<User, UpdateUserPasswordDto>();
|
||||
CreateMap<User, CreateUserDto>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user