From 2112605cf3767ebe894b7cfef7f0b02d63f45ca5 Mon Sep 17 00:00:00 2001 From: reignem Date: Thu, 16 Oct 2025 16:30:17 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Ajout=20de=20Login=20et=20du=20model=20r?= =?UTF-8?q?=C3=A9uni=20avec=20tout=20les=20autres=20Sujet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DTO/Login/Request/ConnectLoginDto.cs | 7 +++ PyroFetes/DTO/Login/Request/CreateLoginDto.cs | 8 +++ PyroFetes/DTO/Login/Request/UpdateLoginDto.cs | 9 ++++ .../DTO/Login/Response/GetLoginConnectDto.cs | 6 +++ PyroFetes/DTO/Login/Response/GetLoginDto.cs | 10 ++++ .../Endpoints/Login/CreateLoginEndpoint.cs | 44 ++++++++++++++++ .../Endpoints/Login/DeleteLoginEndpoint.cs | 35 +++++++++++++ .../Endpoints/Login/GetAllLoginEndpoint.cs | 30 +++++++++++ PyroFetes/Endpoints/Login/GetLoginEndpoint.cs | 41 +++++++++++++++ .../Endpoints/Login/UpdateLoginEndpoint.cs | 45 +++++++++++++++++ .../Endpoints/Login/UserLoginEndpoint.cs | 50 +++++++++++++++++++ .../Product/CreateProductEndpoint.cs | 5 +- .../Product/GetAllProductsEndpoint.cs | 16 +++--- .../Endpoints/Product/GetProductEndpoint.cs | 16 +++--- .../Product/UpdateProductEndpoint.cs | 20 +++----- PyroFetes/Models/Login.cs | 13 +++++ PyroFetes/Models/Product.cs | 3 +- PyroFetes/Program.cs | 13 ++++- PyroFetes/PyroFetes.csproj | 4 ++ PyroFetes/PyroFetesDbContext.cs | 1 + 20 files changed, 340 insertions(+), 36 deletions(-) create mode 100644 PyroFetes/DTO/Login/Request/ConnectLoginDto.cs create mode 100644 PyroFetes/DTO/Login/Request/CreateLoginDto.cs create mode 100644 PyroFetes/DTO/Login/Request/UpdateLoginDto.cs create mode 100644 PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs create mode 100644 PyroFetes/DTO/Login/Response/GetLoginDto.cs create mode 100644 PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs create mode 100644 PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs create mode 100644 PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs create mode 100644 PyroFetes/Endpoints/Login/GetLoginEndpoint.cs create mode 100644 PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs create mode 100644 PyroFetes/Endpoints/Login/UserLoginEndpoint.cs create mode 100644 PyroFetes/Models/Login.cs diff --git a/PyroFetes/DTO/Login/Request/ConnectLoginDto.cs b/PyroFetes/DTO/Login/Request/ConnectLoginDto.cs new file mode 100644 index 0000000..19dc9f0 --- /dev/null +++ b/PyroFetes/DTO/Login/Request/ConnectLoginDto.cs @@ -0,0 +1,7 @@ +namespace PyroFetes.DTO.Login.Request; + +public class ConnectLoginDto +{ + public string? Username { get; set; } + public string? Password { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Login/Request/CreateLoginDto.cs b/PyroFetes/DTO/Login/Request/CreateLoginDto.cs new file mode 100644 index 0000000..7576655 --- /dev/null +++ b/PyroFetes/DTO/Login/Request/CreateLoginDto.cs @@ -0,0 +1,8 @@ +namespace PyroFetes.DTO.Login.Request; + +public class CreateLoginDto +{ + public string? Username { get; set; } + public string? FullName { get; set; } + public string? Password { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Login/Request/UpdateLoginDto.cs b/PyroFetes/DTO/Login/Request/UpdateLoginDto.cs new file mode 100644 index 0000000..54f8ccf --- /dev/null +++ b/PyroFetes/DTO/Login/Request/UpdateLoginDto.cs @@ -0,0 +1,9 @@ +namespace PyroFetes.DTO.Login.Request; + +public class UpdateLoginDto +{ + public int Id { get; set; } + public string? Username { get; set; } + public string? FullName { get; set; } + public string? Password { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs b/PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs new file mode 100644 index 0000000..6ae4c01 --- /dev/null +++ b/PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs @@ -0,0 +1,6 @@ +namespace PyroFetes.DTO.Login.Response; + +public class GetLoginConnectDto +{ + public string? Token { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Login/Response/GetLoginDto.cs b/PyroFetes/DTO/Login/Response/GetLoginDto.cs new file mode 100644 index 0000000..300724e --- /dev/null +++ b/PyroFetes/DTO/Login/Response/GetLoginDto.cs @@ -0,0 +1,10 @@ +namespace PyroFetes.DTO.Login.Response; + +public class GetLoginDto +{ + public int Id { get; set; } + public string? Username { get; set; } + public string? FullName { get; set; } + public string? Password { get; set; } + public string? Salt { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs b/PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs new file mode 100644 index 0000000..6fefac4 --- /dev/null +++ b/PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs @@ -0,0 +1,44 @@ +using PyroFetes.DTO.Login.Request; +using PyroFetes.DTO.Login.Response; +using PasswordGenerator; + +namespace PyroFetes.Endpoints.Login; +using FastEndpoints; + +public class CreateLoginEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/logins"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct) + { + string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next(); + + var login = new Models.Login() + { + Username = req.Username, + FullName = req.FullName, + Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt), + Salt = salt + }; + + database.Logins.Add(login); + + await database.SaveChangesAsync(ct); + // Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400); + + GetLoginDto responseDto = new() + { + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs b/PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs new file mode 100644 index 0000000..73fdfd8 --- /dev/null +++ b/PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs @@ -0,0 +1,35 @@ +using PyroFetes.DTO.Login.Request; +using PyroFetes.DTO.Login.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Login; + +public class DeleteLoginRequest +{ + public int Id { get; set; } +} + +public class DeleteLoginEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/logins/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(DeleteLoginRequest req, CancellationToken ct) + { + var login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (login == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Logins.Remove(login); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs b/PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs new file mode 100644 index 0000000..4d35151 --- /dev/null +++ b/PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs @@ -0,0 +1,30 @@ +using PyroFetes.DTO.Login.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes; + +namespace PyroFetes.Endpoints.Login; + +public class GetAllLoginEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/logins"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var logins = await database.Logins + .Select(login => new GetLoginDto() + { + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt + }) + .ToListAsync(ct); + + await Send.OkAsync(logins, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/GetLoginEndpoint.cs b/PyroFetes/Endpoints/Login/GetLoginEndpoint.cs new file mode 100644 index 0000000..04cb70a --- /dev/null +++ b/PyroFetes/Endpoints/Login/GetLoginEndpoint.cs @@ -0,0 +1,41 @@ +using PyroFetes.DTO.Login.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Login; + +public class GetLoginRequest +{ + public int Id { get; set; } +} + +public class GetLoginEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/logins/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(GetLoginRequest req, CancellationToken ct) + { + var login = await database.Logins + .SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (login == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetLoginDto responseDto = new() + { + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs b/PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs new file mode 100644 index 0000000..ef36a19 --- /dev/null +++ b/PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs @@ -0,0 +1,45 @@ +using PyroFetes.DTO.Login.Request; +using PyroFetes.DTO.Login.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PasswordGenerator; + +namespace PyroFetes.Endpoints.Login; + +public class UpdateLoginEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("/api/logins/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(UpdateLoginDto req, CancellationToken ct) + { + var login = await database.Logins.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.Username = req.Username; + login.FullName = req.FullName; + login.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt); + login.Salt = salt; + await database.SaveChangesAsync(ct); + + GetLoginDto responseDto = new() + { + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Login/UserLoginEndpoint.cs b/PyroFetes/Endpoints/Login/UserLoginEndpoint.cs new file mode 100644 index 0000000..bdad4e6 --- /dev/null +++ b/PyroFetes/Endpoints/Login/UserLoginEndpoint.cs @@ -0,0 +1,50 @@ +using PyroFetes.DTO.Login.Request; +using FastEndpoints.Security; +using PyroFetes.DTO.Login.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes; + +namespace PyroFetes.Endpoints.Login; + +public class UserLoginEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/login"); + AllowAnonymous(); + } + + public override async Task HandleAsync(ConnectLoginDto req, CancellationToken ct) + { + var login = await database.Logins.SingleOrDefaultAsync(x => x.Username == req.Username, ct); + + if (login == null) + { + await Send.UnauthorizedAsync(ct); + return; + } + + if (BCrypt.Net.BCrypt.Verify(req.Password + login.Salt, login.Password)) + { + var jwtToken = JwtBearer.CreateToken( + o => + { + o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong"; + o.ExpireAt = DateTime.UtcNow.AddMinutes(15); + if (login.Role != null) o.User.Roles.Add(login.Role); + o.User.Claims.Add(("Username", login.Username)!); + o.User.Claims.Add(("FullName", login.FullName)!); + o.User["UserId"] = "001"; + }); + + GetLoginConnectDto responseDto = new() + { + Token = jwtToken + }; + + await Send.OkAsync(responseDto, ct); + } + else await Send.UnauthorizedAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs b/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs index 5deb4fe..d6bc518 100644 --- a/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/CreateProductEndpoint.cs @@ -2,8 +2,6 @@ using PyroFetes.DTO.Product.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; using PyroFetes.Models; namespace PyroFetes.Endpoints.Product; @@ -21,14 +19,13 @@ public class CreateProductEndpoint(PyroFetesDbContext db) { var product = new Models.Product { - References = req.References, + Reference = req.References.ToString(), Name = req.Name!, Duration = req.Duration, Caliber = req.Caliber, ApprovalNumber = req.ApprovalNumber, Weight = req.Weight, Nec = req.Nec, - SellingPrice = req.SellingPrice, Image = req.Image!, Link = req.Link!, ProductCategoryId = req.ProductCategoryId, diff --git a/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs index ee5bb08..069f16e 100644 --- a/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs +++ b/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs @@ -1,9 +1,7 @@ -using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.Product.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; using PyroFetes.Models; namespace PyroFetes.Endpoints.Product; @@ -23,20 +21,22 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db) var products = await db.Products .Include(p => p.Prices) .Include(p => p.WarehouseProducts) - .ThenInclude(wp => wp.Warehouse) + .ThenInclude(wp => wp.Warehouse) .ToListAsync(ct); var responseDto = products.Select(p => new GetProductDto { Id = p.Id, - Reference = p.References, + // Le modèle Product contient "Reference" (string) — pas "References" (int) + Reference = int.TryParse(p.Reference, out var refInt) ? refInt : 0, Name = p.Name, Duration = p.Duration, Caliber = p.Caliber, ApprovalNumber = p.ApprovalNumber, Weight = p.Weight, Nec = p.Nec, - SellingPrice = p.SellingPrice, + // Le prix de vente n’est pas dans Product, on le récupère via Prices + SellingPrice = p.Prices.FirstOrDefault()?.SellingPrice ?? 0, Image = p.Image, Link = p.Link, ClassificationId = p.ClassificationId, @@ -49,7 +49,7 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db) SellingPrice = pr.SellingPrice }).ToList(), - // Liste des entrepôts via WarehouseProduct + // Liste des entrepôts liés via WarehouseProduct Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto { WarehouseId = wp.WarehouseId, @@ -60,4 +60,4 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db) await Send.OkAsync(responseDto, ct); } -} \ No newline at end of file +} diff --git a/PyroFetes/Endpoints/Product/GetProductEndpoint.cs b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs index 695ac8e..8c04747 100644 --- a/PyroFetes/Endpoints/Product/GetProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/GetProductEndpoint.cs @@ -1,9 +1,7 @@ -using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.Product.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; using PyroFetes.Models; namespace PyroFetes.Endpoints.Product; @@ -41,14 +39,20 @@ public class GetProductEndpoint(PyroFetesDbContext db) var responseDto = new GetProductDto { Id = product.Id, - Reference = product.References, + + // Le modèle Product contient "Reference" (string), pas "References" (int) + Reference = int.TryParse(product.Reference, out var refInt) ? refInt : 0, + Name = product.Name, Duration = product.Duration, Caliber = product.Caliber, ApprovalNumber = product.ApprovalNumber, Weight = product.Weight, Nec = product.Nec, - SellingPrice = product.SellingPrice, + + // Le prix de vente n’est pas dans Product → récupéré via Price + SellingPrice = product.Prices.FirstOrDefault()?.SellingPrice ?? 0, + Image = product.Image, Link = product.Link, ClassificationId = product.ClassificationId, @@ -72,4 +76,4 @@ public class GetProductEndpoint(PyroFetesDbContext db) await Send.OkAsync(responseDto, ct); } -} \ No newline at end of file +} diff --git a/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs index 2640ec9..0577f83 100644 --- a/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs +++ b/PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs @@ -2,8 +2,6 @@ using PyroFetes.DTO.Product.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -using PyroFetes.DTO.Product.Request; -using PyroFetes.DTO.Product.Response; using PyroFetes.Models; namespace PyroFetes.Endpoints.Product; @@ -37,21 +35,19 @@ public class UpdateProductEndpoint(PyroFetesDbContext db) } // Mise à jour des propriétés principales du produit - product.References = req.References; + product.Reference = req.References.ToString(); // Converti int → string product.Name = req.Name; product.Duration = req.Duration; product.Caliber = req.Caliber; product.ApprovalNumber = req.ApprovalNumber; product.Weight = req.Weight; product.Nec = req.Nec; - product.SellingPrice = req.SellingPrice; product.Image = req.Image; product.Link = req.Link; product.ClassificationId = req.ClassificationId; product.ProductCategoryId = req.ProductCategoryId; // Mise à jour des prix fournisseurs associés - // On supprime les anciens enregistrements pour les remplacer db.Prices.RemoveRange(product.Prices); foreach (var s in req.Suppliers) { @@ -64,7 +60,6 @@ public class UpdateProductEndpoint(PyroFetesDbContext db) } // Mise à jour des entrepôts associés - // On supprime les anciens liens avant d'ajouter les nouveaux db.WarehouseProducts.RemoveRange(product.WarehouseProducts); foreach (var w in req.Warehouses) { @@ -76,44 +71,41 @@ public class UpdateProductEndpoint(PyroFetesDbContext db) }); } - // Sauvegarde des modifications dans la base de données await db.SaveChangesAsync(ct); // Construction de la réponse renvoyée au client - // On reconstruit les listes Suppliers et Warehouses au bon format de DTO var response = new GetProductDto { Id = product.Id, - Reference = req.References, + Reference = req.References, // DTO garde int pour cohérence Name = req.Name, Duration = req.Duration, Caliber = req.Caliber, ApprovalNumber = req.ApprovalNumber, Weight = req.Weight, Nec = req.Nec, - SellingPrice = req.SellingPrice, + // Le prix de vente est pris depuis Prices + SellingPrice = req.Suppliers.FirstOrDefault()?.SellingPrice ?? 0, Image = req.Image, Link = req.Link, ClassificationId = req.ClassificationId, ProductCategoryId = req.ProductCategoryId, - // Mapping des fournisseurs pour la réponse Suppliers = req.Suppliers.Select(s => new ProductSupplierPriceDto { SupplierId = s.SupplierId, SellingPrice = s.SellingPrice }).ToList(), - // Mapping des entrepôts pour la réponse Warehouses = req.Warehouses.Select(w => new GetProductWarehouseDto { WarehouseId = w.WarehouseId, Quantity = w.Quantity, - WarehouseName = db.Warehouses.FirstOrDefault(x => x.Id == w.WarehouseId)?.Name ?? string.Empty + WarehouseName = db.Warehouses + .FirstOrDefault(x => x.Id == w.WarehouseId)?.Name ?? string.Empty }).ToList() }; - // Envoi de la réponse HTTP 200 avec les données du produit mis à jour await Send.OkAsync(response, ct); } } diff --git a/PyroFetes/Models/Login.cs b/PyroFetes/Models/Login.cs new file mode 100644 index 0000000..89b7eaf --- /dev/null +++ b/PyroFetes/Models/Login.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace PyroFetes.Models; + +public class Login +{ + [Key] public int Id { get; set; } + [Required, MaxLength(100)] public string? Username { get; set; } + [Required, MaxLength(200)] public string? FullName { get; set; } + [Required, Length(60, 60)] public string? Password { get; set; } + [Required, Length(24, 24)] public string? Salt { get; set; } + [Required, MaxLength(100)] public string? Role { get; set; } +} diff --git a/PyroFetes/Models/Product.cs b/PyroFetes/Models/Product.cs index b0f0d7f..f48ee46 100644 --- a/PyroFetes/Models/Product.cs +++ b/PyroFetes/Models/Product.cs @@ -5,14 +5,13 @@ namespace PyroFetes.Models public class Product { [Key] public int Id { get; set; } - [Required] public int References { get; set; } + [Required, MaxLength(20)] public string? Reference { get; set; } [Required, MaxLength(100)] public string? Name { get; set; } [Required] public decimal Duration {get; set;} [Required] public decimal Caliber { get; set; } [Required] public int ApprovalNumber { get; set; } [Required] public decimal Weight { get; set; } [Required] public decimal Nec { get; set; } - [Required] public decimal SellingPrice { get; set; } [Required] public string? Image { get; set; } [Required, MaxLength(200)] public string? Link { get; set; } [Required] public int MinimalQuantity { get; set; } diff --git a/PyroFetes/Program.cs b/PyroFetes/Program.cs index e610bf4..170f1b8 100644 --- a/PyroFetes/Program.cs +++ b/PyroFetes/Program.cs @@ -2,18 +2,27 @@ using API; using FastEndpoints; using FastEndpoints.Swagger; using PyroFetes; +using FastEndpoints.Security; + WebApplicationBuilder builder = WebApplication.CreateBuilder(args); // On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet -builder.Services.AddFastEndpoints().SwaggerDocument(); +builder.Services + .AddAuthenticationJwtBearer(s => s.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong") + .AddAuthorization() + .AddFastEndpoints() + .SwaggerDocument(); // On ajoute ici la configuration de la base de données builder.Services.AddDbContext(); // On construit l'application en lui donnant vie WebApplication app = builder.Build(); -app.UseFastEndpoints().UseSwaggerGen(); +app.UseAuthentication() + .UseAuthorization() + .UseFastEndpoints() + .UseSwaggerGen(); app.UseHttpsRedirection(); diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index b301a92..dd8fc9b 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,7 +7,10 @@ + + + @@ -16,6 +19,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/PyroFetes/PyroFetesDbContext.cs b/PyroFetes/PyroFetesDbContext.cs index b5ce8c7..b4958b2 100644 --- a/PyroFetes/PyroFetesDbContext.cs +++ b/PyroFetes/PyroFetesDbContext.cs @@ -49,6 +49,7 @@ public class PyroFetesDbContext : DbContext public DbSet Users { get; set; } public DbSet Warehouses { get; set; } public DbSet WarehouseProducts { get; set; } + public DbSet Logins { get; set; } // Database configuration protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) From 15545980af3d457f97b8b6e00369cc9ce3f69f79 Mon Sep 17 00:00:00 2001 From: reignem Date: Thu, 16 Oct 2025 17:03:51 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Normalement=20c'est=20r=C3=A9gl=C3=A9=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs | 2 +- .../Endpoints/Classification/UpdateClassificationEndpoint.cs | 2 +- PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs | 2 +- PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs | 4 +--- PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs | 2 +- PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs | 2 +- PyroFetes/PyroFetes.csproj | 5 ++--- 7 files changed, 8 insertions(+), 11 deletions(-) diff --git a/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs b/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs index 4e3b63f..fdb4996 100644 --- a/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs +++ b/PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs @@ -8,7 +8,7 @@ public class UpdateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi { public override void Configure() { - Post("/api/brands"); + Put("/api/brands/{Id}"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs b/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs index ba1d0d3..8569cc7 100644 --- a/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs +++ b/PyroFetes/Endpoints/Classification/UpdateClassificationEndpoint.cs @@ -8,7 +8,7 @@ public class UpdateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) { public override void Configure() { - Post("/api/classifications"); + Put("/api/classifications"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs b/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs index 151ffc9..9e69545 100644 --- a/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs +++ b/PyroFetes/Endpoints/Movement/UpdateMovementEndpoint.cs @@ -8,7 +8,7 @@ public class UpdateMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End { public override void Configure() { - Post("/api/movements"); + Put("/api/movements"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs index c2e131e..4ea299d 100644 --- a/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs @@ -13,11 +13,9 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint new { x.Id }); + Delete("/api/warehouse/{id}"); AllowAnonymous(); } - public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct) { // On charge aussi les WarehouseProducts liés pour les supprimer proprement diff --git a/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs index 1744bac..5b27573 100644 --- a/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs @@ -16,7 +16,7 @@ public class GetWarehouseEndpoint(PyroFetesDbContext db) public override void Configure() { // Pas de "@id" ici, juste {id} - Get("/api/warehouses/{id}", x => new { x.Id }); + Get("/api/warehouses/{Id}"); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs b/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs index 7f3afc5..d6a540f 100644 --- a/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs +++ b/PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs @@ -12,7 +12,7 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext db) public override void Configure() { // Utilise {id} plutôt que {@id} - Put("/api/warehouses/{id}", x => new { x.Id }); + Put("/api/warehouses/{Id}"); AllowAnonymous(); } diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index dd8fc9b..9f5e23f 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,12 +7,11 @@ - - + all @@ -20,7 +19,7 @@ - +