forked from sanchezvem/PyroFetes
Compare commits
2 Commits
f60d3443ca
...
0a8258017a
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a8258017a | |||
| 78e5a4e960 |
7
PyroFetes/DTO/Login/Request/ConnectLoginDto.cs
Normal file
7
PyroFetes/DTO/Login/Request/ConnectLoginDto.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PyroFetes.DTO.Login.Request;
|
||||
|
||||
public class ConnectLoginDto
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
24
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal file
24
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
// Nécessaire pour les validations
|
||||
|
||||
namespace PyroFetes.DTO.Login.Request;
|
||||
|
||||
public class CreateLoginDto
|
||||
{
|
||||
[Required(ErrorMessage = "Le nom est requis.")]
|
||||
[StringLength(50, MinimumLength = 3, ErrorMessage = "L'identifiant doit faire entre 3 et 50 caractères.")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "L'emil est requis.")]
|
||||
[StringLength(50, MinimumLength = 3)]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Le mot de passe est requis.")]
|
||||
[MinLength(6, ErrorMessage = "Le mot de passe doit contenir au moins 6 caractères.")]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
// Ajout du champ Rôle (Optionnel, par défaut "User")
|
||||
// Cela te permet d'envoyer "Admin" via Swagger
|
||||
public string Fonction { get; set; } = "User";
|
||||
}
|
||||
8
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal file
8
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace PyroFetes.DTO.Login.Request;
|
||||
|
||||
public class UpdateLoginDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
6
PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs
Normal file
6
PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PyroFetes.DTO.Login.Response;
|
||||
|
||||
public class GetLoginConnectDto
|
||||
{
|
||||
public string? Token { get; set; }
|
||||
}
|
||||
10
PyroFetes/DTO/Login/Response/GetLoginDto.cs
Normal file
10
PyroFetes/DTO/Login/Response/GetLoginDto.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PyroFetes.DTO.Login.Response;
|
||||
|
||||
public class GetLoginDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; } = string.Empty;
|
||||
public string? Email { get; set; } = string.Empty;
|
||||
public string? Password { get; set; } = string.Empty;
|
||||
public string? Fonction { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -9,7 +9,6 @@ public class CreateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/brands");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateBrandDto req, CancellationToken ct)
|
||||
|
||||
@@ -13,7 +13,6 @@ public class DeleteBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/brands/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteBrandRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllBrandsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/brands");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<G
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/brands/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetBrandRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class UpdateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/brands/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateBrandDto req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class CreateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/classifications");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateClassificationDto req, CancellationToken ct)
|
||||
|
||||
@@ -13,7 +13,6 @@ public class DeleteClassificationEndpoint(PyroFetesDbContext libraryDbContext) :
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/classifications/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteClassificationRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllClassificationsEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/classifications");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) :E
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/classifications/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetClassificationRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class UpdateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/classifications");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateClassificationDto req, CancellationToken ct)
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
using API.DTO.Color.Request;
|
||||
using API.DTO.Color.Response;
|
||||
using FastEndpoints;
|
||||
using PyroFetes;
|
||||
|
||||
namespace API.Endpoints.Color;
|
||||
namespace PyroFetes.Endpoints.Color;
|
||||
|
||||
public class CreateColorEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateColorDto, GetColorDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateColorDto et l'élement de réponse GetColorDto
|
||||
{
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Post("/colors"); //Création d'un endpoint pour créer une couleur avec les données de CreateColorDto
|
||||
AllowAnonymous(); //Laisser passer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateColorDto req, CancellationToken ct) //La méthode HandleAsync est appelée lorsqu'une requête est envoyée à l'endpoint
|
||||
|
||||
@@ -13,7 +13,6 @@ public class DeleteColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/colors/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteColorRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllColorsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/colors");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/colors/{@id}", x => new { x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetColorRequest req, CancellationToken ct)
|
||||
|
||||
@@ -10,7 +10,6 @@ public class UpdateColorEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/colors/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateColorDto req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class CreateEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/effects");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateEffectDto req, CancellationToken ct)
|
||||
|
||||
@@ -12,7 +12,6 @@ public class DeleteEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/effects/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteEffectRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllEffectsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endp
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/effects");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/effects/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetEffectRequest req, CancellationToken ct)
|
||||
|
||||
@@ -10,8 +10,7 @@ public class UpdateEffectEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpo
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/effects/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateEffectDto req, CancellationToken ct)
|
||||
{
|
||||
|
||||
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PasswordGenerator;
|
||||
using PyroFetes.DTO.Login.Request;
|
||||
using PyroFetes.DTO.Login.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class CreateLoginEndpoint(PyroFetesDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/logins");
|
||||
//Roles("Admin");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct)
|
||||
{
|
||||
bool exists = await database.Users.AnyAsync(x => x.Name == req.Name, ct);
|
||||
if (exists)
|
||||
{
|
||||
AddError("Ce nom d'utilisateur est déjà utilisé.");
|
||||
await Send.ErrorsAsync(400, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||
|
||||
Models.User login = new Models.User()
|
||||
{
|
||||
Name = req.Name,
|
||||
Email = req.Email,
|
||||
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
|
||||
Salt = salt,
|
||||
|
||||
Fonction = string.IsNullOrEmpty(req.Fonction) ? "User" : req.Fonction
|
||||
};
|
||||
|
||||
database.Users.Add(login);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetLoginDto responseDto = new()
|
||||
{
|
||||
Id = login.Id,
|
||||
Name = login.Name,
|
||||
Email = login.Email,
|
||||
Fonction = login.Fonction
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
34
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal file
34
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class DeleteLoginRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteLoginEndpoint(PyroFetesDbContext database) : Endpoint<DeleteLoginRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/logins/{@Id}", x => new {x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteLoginRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||
|
||||
if (login == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
database.Users.Remove(login);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
29
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal file
29
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Login.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class GetAllLoginEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetLoginDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/logins");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetLoginDto> logins = await database.Users
|
||||
.Select(login => new GetLoginDto()
|
||||
{
|
||||
Id = login.Id,
|
||||
Name = login.Name,
|
||||
Password = login.Password,
|
||||
Fonction = login.Fonction
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(logins, ct);
|
||||
}
|
||||
}
|
||||
40
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal file
40
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Login.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class GetLoginRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetLoginEndpoint(PyroFetesDbContext database) : Endpoint<GetLoginRequest, GetLoginDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/logins/{@Id}", x => new {x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetLoginRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.User? login = await database.Users
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||
|
||||
if (login == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetLoginDto responseDto = new()
|
||||
{
|
||||
Id = login.Id,
|
||||
Name = login.Name,
|
||||
Fonction = login.Fonction
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
43
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal file
43
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PasswordGenerator;
|
||||
using PyroFetes.DTO.Login.Request;
|
||||
using PyroFetes.DTO.Login.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class UpdateLoginEndpoint(PyroFetesDbContext database) : Endpoint<UpdateLoginDto, GetLoginDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/logins/{@Id}", x => new {x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateLoginDto req, CancellationToken ct)
|
||||
{
|
||||
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||
|
||||
if (login == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||
|
||||
login.Name = req.Name;
|
||||
login.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt);
|
||||
login.Salt = salt;
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetLoginDto responseDto = new()
|
||||
{
|
||||
Id = login.Id,
|
||||
Name = login.Name,
|
||||
Fonction = login.Fonction
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
48
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal file
48
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Security;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Login.Request;
|
||||
using PyroFetes.DTO.Login.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Login;
|
||||
|
||||
public class UserLoginEndpoint(PyroFetesDbContext database) : Endpoint<ConnectLoginDto, GetLoginConnectDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/login");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(ConnectLoginDto req, CancellationToken ct)
|
||||
{
|
||||
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
|
||||
|
||||
if (login == null)
|
||||
{
|
||||
await Send.UnauthorizedAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (BCrypt.Net.BCrypt.Verify(req.Password + login.Salt, login.Password))
|
||||
{
|
||||
string jwtToken = JwtBearer.CreateToken(
|
||||
o =>
|
||||
{
|
||||
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
||||
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||
if (login.Fonction != null) o.User.Roles.Add(login.Fonction);
|
||||
o.User.Claims.Add(("Username", login.Name)!);
|
||||
o.User["UserId"] = "001";
|
||||
});
|
||||
|
||||
GetLoginConnectDto responseDto = new()
|
||||
{
|
||||
Token = jwtToken
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
else await Send.UnauthorizedAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ public class CreateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/materials");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateMaterialDto req, CancellationToken ct)
|
||||
|
||||
@@ -11,7 +11,6 @@ public class DeleteMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllMaterialsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/materials");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetMaterialRequest req, CancellationToken ct)
|
||||
|
||||
@@ -10,7 +10,6 @@ public class UpdateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class CreateMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/movements");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateMovementDto req, CancellationToken ct)
|
||||
|
||||
@@ -13,7 +13,6 @@ public class DeleteMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/Movements/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteMovementRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class GetAllMovementsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/movements");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class GetMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoin
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/movements/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetMovementRequest req, CancellationToken ct)
|
||||
|
||||
@@ -9,7 +9,6 @@ public class UpdateMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/movements");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateMovementDto req, CancellationToken ct)
|
||||
|
||||
@@ -12,7 +12,6 @@ public class CreateProductEndpoint(PyroFetesDbContext db)
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/products");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class DeleteProductEndpoint(PyroFetesDbContext db) : Endpoint<DeleteProdu
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct)
|
||||
|
||||
@@ -12,7 +12,6 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/products");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -17,7 +17,6 @@ public class GetProductEndpoint(PyroFetesDbContext db)
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
|
||||
@@ -25,7 +24,7 @@ public class GetProductEndpoint(PyroFetesDbContext db)
|
||||
// Inclure toutes les relations : Prices + WarehouseProducts + Warehouse
|
||||
var product = await db.Products
|
||||
.Include(p => p.Prices)
|
||||
.Include(p => p.WarehouseProducts)
|
||||
.Include(p => p.WarehouseProducts)!
|
||||
.ThenInclude(wp => wp.Warehouse)
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
|
||||
|
||||
|
||||
@@ -6,36 +6,29 @@ using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
// Endpoint permettant de mettre à jour un produit existant
|
||||
public class UpdateProductEndpoint(PyroFetesDbContext db)
|
||||
: Endpoint<UpdateProductDto, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
// Route HTTP PUT avec un paramètre d'identifiant dans l'URL
|
||||
Put("/products/{@id}", x => new { x.Id });
|
||||
|
||||
// Autorise les requêtes anonymes (sans authentification)
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
|
||||
{
|
||||
// Recherche du produit à mettre à jour, en incluant les relations Prices et WarehouseProducts
|
||||
var product = await db.Products
|
||||
.Include(p => p.Prices)
|
||||
.Include(p => p.WarehouseProducts)
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
|
||||
|
||||
// Si le produit n'existe pas, on retourne une réponse 404
|
||||
if (product is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mise à jour des propriétés principales du produit
|
||||
product.Reference = req.Reference; // Converti int → string
|
||||
product.Reference = req.Reference;
|
||||
product.Name = req.Name;
|
||||
product.Duration = req.Duration;
|
||||
product.Caliber = req.Caliber;
|
||||
@@ -47,7 +40,6 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
|
||||
product.ClassificationId = req.ClassificationId;
|
||||
product.ProductCategoryId = req.ProductCategoryId;
|
||||
|
||||
// Mise à jour des prix fournisseurs associés
|
||||
db.Prices.RemoveRange(product.Prices);
|
||||
foreach (var s in req.Suppliers)
|
||||
{
|
||||
@@ -59,7 +51,6 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
|
||||
});
|
||||
}
|
||||
|
||||
// Mise à jour des entrepôts associés
|
||||
db.WarehouseProducts.RemoveRange(product.WarehouseProducts);
|
||||
foreach (var w in req.Warehouses)
|
||||
{
|
||||
@@ -73,18 +64,16 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
// Construction de la réponse renvoyée au client
|
||||
var response = new GetProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Reference = req.Reference, // DTO garde int pour cohérence
|
||||
Reference = req.Reference,
|
||||
Name = req.Name,
|
||||
Duration = req.Duration,
|
||||
Caliber = req.Caliber,
|
||||
ApprovalNumber = req.ApprovalNumber,
|
||||
Weight = req.Weight,
|
||||
Nec = req.Nec,
|
||||
// Le prix de vente est pris depuis Prices
|
||||
SellingPrice = req.Suppliers.FirstOrDefault()?.SellingPrice ?? 0,
|
||||
Image = req.Image,
|
||||
Link = req.Link,
|
||||
|
||||
@@ -9,7 +9,6 @@ public class CreateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/productcategories");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateProductCategoryDto req, CancellationToken ct)
|
||||
|
||||
@@ -13,7 +13,6 @@ public class DeleteProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/productcategories/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductCategoryRequest req, CancellationToken ct)
|
||||
|
||||
@@ -4,12 +4,11 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class GetAllProductCategoriesEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductCategoryDto>>
|
||||
public class GetAllProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductCategoryDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/productcategories");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
@@ -15,7 +15,6 @@ public class GetProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) :
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductCategoryRequest req, CancellationToken ct)
|
||||
|
||||
@@ -10,7 +10,6 @@ public class UpdateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductCategoryDto req, CancellationToken ct)
|
||||
|
||||
@@ -12,7 +12,6 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/suppliers");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/suppliers/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
|
||||
|
||||
@@ -12,7 +12,6 @@ public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/suppliers");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -16,7 +16,6 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/suppliers/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct)
|
||||
|
||||
@@ -11,7 +11,6 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/suppliers/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
|
||||
|
||||
@@ -11,7 +11,6 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext db)
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/warehouse");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct)
|
||||
|
||||
@@ -14,7 +14,6 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint<DeleteWar
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/warehouse/{id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,6 @@ public class GetAllWarehouseEndpoint(PyroFetesDbContext db)
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/warehouses");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
|
||||
@@ -17,7 +17,6 @@ public class GetWarehouseEndpoint(PyroFetesDbContext db)
|
||||
{
|
||||
// Pas de "@id" ici, juste {id}
|
||||
Get("/warehouses/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetWarehouseRequest req, CancellationToken ct)
|
||||
|
||||
@@ -13,7 +13,6 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext db)
|
||||
{
|
||||
// Utilise {id} plutôt que {@id}
|
||||
Put("/warehouses/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct)
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Security;
|
||||
using FastEndpoints.Swagger;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes;
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Services
|
||||
builder.Services
|
||||
.AddAuthenticationJwtBearer(s => s.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong")
|
||||
.AddAuthentication();
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
options.AddDefaultPolicy(policyBuilder =>
|
||||
policyBuilder
|
||||
.WithOrigins("http://localhost:4200") // mettre le port Angular exact
|
||||
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
|
||||
.AllowAnyHeader()
|
||||
policyBuilder.WithOrigins("http://localhost:4200")
|
||||
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials()
|
||||
)
|
||||
);
|
||||
|
||||
builder.Services.AddFastEndpoints().SwaggerDocument(options =>
|
||||
builder.Services.AddFastEndpoints().SwaggerDocument(options =>
|
||||
{
|
||||
options.ShortSchemaNames = true;
|
||||
});
|
||||
@@ -23,20 +30,19 @@ builder.Services.AddDbContext<PyroFetesDbContext>();
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
// Middleware
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// CORS doit être avant les endpoints
|
||||
app.UseCors();
|
||||
|
||||
// FastEndpoints et Swagger
|
||||
app.UseAuthentication();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseFastEndpoints(options =>
|
||||
{
|
||||
options.Endpoints.RoutePrefix = "API";
|
||||
options.Endpoints.RoutePrefix = "API";
|
||||
options.Endpoints.ShortNames = true;
|
||||
}).UseSwaggerGen();
|
||||
|
||||
// app.UseAuthorization();
|
||||
// app.UseAuthentication();
|
||||
|
||||
app.Run();
|
||||
18
package-lock.json
generated
Normal file
18
package-lock.json
generated
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "PyroFete",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"zone.js": "^0.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/zone.js": {
|
||||
"version": "0.16.0",
|
||||
"resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.16.0.tgz",
|
||||
"integrity": "sha512-LqLPpIQANebrlxY6jKcYKdgN5DTXyyHAKnnWWjE5pPfEQ4n7j5zn7mOEEpwNZVKGqx3kKKmvplEmoBrvpgROTA==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
5
package.json
Normal file
5
package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"zone.js": "^0.16.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user