Initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Users;
|
||||
using MetaCourse.Api.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Users;
|
||||
|
||||
public class RegisterEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<RegisterUserDto, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("api/users/register");
|
||||
AllowAnonymous();
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "Inscription d'un nouvel utilisateur";
|
||||
s.Description = "Crée un compte utilisateur avec email unique et mot de passe haché.";
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(RegisterUserDto req, CancellationToken ct)
|
||||
{
|
||||
var emailExists = await db.Users.AnyAsync(u => u.Email == req.Email, ct);
|
||||
if (emailExists)
|
||||
{
|
||||
AddError(r => r.Email, "Cet email est déjà utilisé.");
|
||||
await SendErrorsAsync(409, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var user = mapper.Map<User>(req);
|
||||
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(req.Password);
|
||||
|
||||
db.Users.Add(user);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
await SendAsync(mapper.Map<GetUserDto>(user), 201, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user