4 Commits

Author SHA1 Message Date
4c0e7df9de MAJ 2025-10-16 17:09:29 +02:00
5506eab8ff Merge branch 'feature/mathy' into develop
# Conflicts:
#	PyroFetes/Endpoints/Brand/UpdateBrandEndpoint.cs
#	PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs
#	PyroFetes/Endpoints/Warehouse/GetWarehouseEndpoint.cs
#	PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs
2025-10-16 17:08:38 +02:00
15545980af Normalement c'est réglé ! 2025-10-16 17:03:51 +02:00
2112605cf3 Ajout de Login et du model réuni avec tout les autres Sujet 2025-10-16 16:30:17 +02:00
24 changed files with 345 additions and 44 deletions

View File

@@ -0,0 +1,7 @@
namespace PyroFetes.DTO.Login.Request;
public class ConnectLoginDto
{
public string? Username { get; set; }
public string? Password { get; set; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -0,0 +1,6 @@
namespace PyroFetes.DTO.Login.Response;
public class GetLoginConnectDto
{
public string? Token { get; set; }
}

View File

@@ -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; }
}

View File

@@ -8,7 +8,7 @@ public class UpdateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
{
public override void Configure()
{
Put("/api/brands");
Put("/api/brands/{Id}");
AllowAnonymous();
}

View File

@@ -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<CreateLoginDto, GetLoginDto>
{
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);
}
}

View File

@@ -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<DeleteLoginRequest>
{
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);
}
}

View File

@@ -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<List<GetLoginDto>>
{
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);
}
}

View File

@@ -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<GetLoginRequest, GetLoginDto>
{
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);
}
}

View File

@@ -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<UpdateLoginDto, GetLoginDto>
{
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);
}
}

View File

@@ -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<ConnectLoginDto, GetLoginConnectDto>
{
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);
}
}

View File

@@ -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,

View File

@@ -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 nest 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);
}
}
}

View File

@@ -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 nest 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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -13,11 +13,9 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint<DeleteWar
{
public override void Configure()
{
// Lannotation correcte du paramètre est {id}, pas {@id}
Delete("/api/warehouse/{@id}", x => 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

View File

@@ -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();
}

View File

@@ -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();
}

13
PyroFetes/Models/Login.cs Normal file
View File

@@ -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; }
}

View File

@@ -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; }

View File

@@ -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<PyroFetesDbContext>();
// On construit l'application en lui donnant vie
WebApplication app = builder.Build();
app.UseFastEndpoints().UseSwaggerGen();
app.UseAuthentication()
.UseAuthorization()
.UseFastEndpoints()
.UseSwaggerGen();
app.UseHttpsRedirection();

View File

@@ -7,16 +7,19 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FastEndpoints" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Security" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Swagger" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.20" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
<PackageReference Include="PasswordGenerator" Version="2.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
</Project>

View File

@@ -49,6 +49,7 @@ public class PyroFetesDbContext : DbContext
public DbSet<User> Users { get; set; }
public DbSet<Warehouse> Warehouses { get; set; }
public DbSet<WarehouseProduct> WarehouseProducts { get; set; }
public DbSet<Login> Logins { get; set; }
// Database configuration
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)