43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using BeReadyBackend.DTO.Users;
|
|
using BeReadyBackend.Models;
|
|
using BeReadyBackend.Repositories;
|
|
using BeReadyBackend.Services;
|
|
using BeReadyBackend.Specifications.Users;
|
|
using FastEndpoints;
|
|
|
|
namespace BeReadyBackend.Endpoints.Users;
|
|
|
|
public class UpdateUserEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : Endpoint<UpdateUserDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("/Users/");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
|
|
{
|
|
int userId = userService.GetUserIdFromToken();
|
|
|
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByCriteriaSpec(req.Username!, req.Email!, userId), ct);
|
|
|
|
if (user is not null)
|
|
{
|
|
await Send.StringAsync("Un utilisateur possède déjà ce pseudo ou cette email", 400, cancellation: ct);
|
|
return;
|
|
}
|
|
|
|
user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId), ct);
|
|
|
|
if (user is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
mapper.Map(req, user);
|
|
|
|
await usersRepository.SaveChangesAsync(ct);
|
|
await Send.OkAsync(await usersRepository.ProjectToSingleAsync<GetUserDetailsDto>(new GetUserByIdSpec(userId), ct), ct);
|
|
}
|
|
} |