Initial commit

This commit is contained in:
2026-05-05 10:39:43 +02:00
commit b590ecdc35
87 changed files with 3934 additions and 0 deletions
@@ -0,0 +1,33 @@
using AutoMapper;
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Users;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Users;
public class GetUserRequest
{
public Guid Id { get; set; }
}
public class GetUserEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetUserRequest, GetUserDto>
{
public override void Configure()
{
Get("api/users/{id}");
AllowAnonymous();
Summary(s => s.Summary = "Récupère le profil d'un utilisateur");
}
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
{
var user = await db.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Id == req.Id, ct);
if (user is null)
{
await SendNotFoundAsync(ct);
return;
}
await SendOkAsync(mapper.Map<GetUserDto>(user), ct);
}
}
@@ -0,0 +1,39 @@
using FastEndpoints;
using MetaCourse.Api.Data;
using MetaCourse.Api.DTOs.Users;
using Microsoft.EntityFrameworkCore;
namespace MetaCourse.Api.Endpoints.Users;
public class LoginEndpoint(AppDbContext db) : Endpoint<LoginUserDto, LoginResponseDto>
{
public override void Configure()
{
Post("api/users/login");
AllowAnonymous();
Summary(s =>
{
s.Summary = "Connexion d'un utilisateur";
s.Description = "Authentifie l'utilisateur avec email et mot de passe.";
});
}
public override async Task HandleAsync(LoginUserDto req, CancellationToken ct)
{
var user = await db.Users.FirstOrDefaultAsync(u => u.Email == req.Email, ct);
if (user is null || !BCrypt.Net.BCrypt.Verify(req.Password, user.PasswordHash))
{
AddError("Email ou mot de passe incorrect.");
await SendErrorsAsync(401, ct);
return;
}
await SendOkAsync(new LoginResponseDto
{
UserId = user.Id,
Name = user.Name,
Email = user.Email
}, ct);
}
}
@@ -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);
}
}