forked from sanchezvem/PyroFetes
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
This commit is contained in:
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? Username { get; set; }
|
||||||
|
public string? Password { get; set; }
|
||||||
|
}
|
8
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal file
8
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal 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; }
|
||||||
|
}
|
9
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal file
9
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal 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; }
|
||||||
|
}
|
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? Username { get; set; }
|
||||||
|
public string? FullName { get; set; }
|
||||||
|
public string? Password { get; set; }
|
||||||
|
public string? Salt { get; set; }
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
|
using API.DTO.Brand.Request;
|
||||||
using API.DTO.Brand.Response;
|
using API.DTO.Brand.Response;
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using PyroFetes.DTO.Brand.Request;
|
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Brand;
|
namespace PyroFetes.Endpoints.Brand;
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ public class UpdateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
|||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
Put("/api/brands");
|
Put("/api/brands/{Id}");
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
44
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
35
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal file
35
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
30
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal file
30
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
41
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal file
41
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
45
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal file
45
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
50
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal file
50
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,8 +2,6 @@
|
|||||||
using PyroFetes.DTO.Product.Response;
|
using PyroFetes.DTO.Product.Response;
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.Product.Request;
|
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Product;
|
namespace PyroFetes.Endpoints.Product;
|
||||||
@@ -21,14 +19,13 @@ public class CreateProductEndpoint(PyroFetesDbContext db)
|
|||||||
{
|
{
|
||||||
var product = new Models.Product
|
var product = new Models.Product
|
||||||
{
|
{
|
||||||
References = req.References,
|
Reference = req.References.ToString(),
|
||||||
Name = req.Name!,
|
Name = req.Name!,
|
||||||
Duration = req.Duration,
|
Duration = req.Duration,
|
||||||
Caliber = req.Caliber,
|
Caliber = req.Caliber,
|
||||||
ApprovalNumber = req.ApprovalNumber,
|
ApprovalNumber = req.ApprovalNumber,
|
||||||
Weight = req.Weight,
|
Weight = req.Weight,
|
||||||
Nec = req.Nec,
|
Nec = req.Nec,
|
||||||
SellingPrice = req.SellingPrice,
|
|
||||||
Image = req.Image!,
|
Image = req.Image!,
|
||||||
Link = req.Link!,
|
Link = req.Link!,
|
||||||
ProductCategoryId = req.ProductCategoryId,
|
ProductCategoryId = req.ProductCategoryId,
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
using PyroFetes.DTO.Product.Request;
|
using PyroFetes.DTO.Product.Response;
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.Product.Request;
|
using PyroFetes.DTO.Product.Request;
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Product;
|
namespace PyroFetes.Endpoints.Product;
|
||||||
@@ -23,20 +21,22 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
|
|||||||
var products = await db.Products
|
var products = await db.Products
|
||||||
.Include(p => p.Prices)
|
.Include(p => p.Prices)
|
||||||
.Include(p => p.WarehouseProducts)
|
.Include(p => p.WarehouseProducts)
|
||||||
.ThenInclude(wp => wp.Warehouse)
|
.ThenInclude(wp => wp.Warehouse)
|
||||||
.ToListAsync(ct);
|
.ToListAsync(ct);
|
||||||
|
|
||||||
var responseDto = products.Select(p => new GetProductDto
|
var responseDto = products.Select(p => new GetProductDto
|
||||||
{
|
{
|
||||||
Id = p.Id,
|
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,
|
Name = p.Name,
|
||||||
Duration = p.Duration,
|
Duration = p.Duration,
|
||||||
Caliber = p.Caliber,
|
Caliber = p.Caliber,
|
||||||
ApprovalNumber = p.ApprovalNumber,
|
ApprovalNumber = p.ApprovalNumber,
|
||||||
Weight = p.Weight,
|
Weight = p.Weight,
|
||||||
Nec = p.Nec,
|
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,
|
Image = p.Image,
|
||||||
Link = p.Link,
|
Link = p.Link,
|
||||||
ClassificationId = p.ClassificationId,
|
ClassificationId = p.ClassificationId,
|
||||||
@@ -49,7 +49,7 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
|
|||||||
SellingPrice = pr.SellingPrice
|
SellingPrice = pr.SellingPrice
|
||||||
}).ToList(),
|
}).ToList(),
|
||||||
|
|
||||||
// Liste des entrepôts via WarehouseProduct
|
// Liste des entrepôts liés via WarehouseProduct
|
||||||
Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto
|
Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto
|
||||||
{
|
{
|
||||||
WarehouseId = wp.WarehouseId,
|
WarehouseId = wp.WarehouseId,
|
||||||
@@ -60,4 +60,4 @@ public class GetAllProductsEndpoint(PyroFetesDbContext db)
|
|||||||
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
await Send.OkAsync(responseDto, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
using PyroFetes.DTO.Product.Request;
|
using PyroFetes.DTO.Product.Response;
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.Product.Request;
|
using PyroFetes.DTO.Product.Request;
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Product;
|
namespace PyroFetes.Endpoints.Product;
|
||||||
@@ -41,14 +39,20 @@ public class GetProductEndpoint(PyroFetesDbContext db)
|
|||||||
var responseDto = new GetProductDto
|
var responseDto = new GetProductDto
|
||||||
{
|
{
|
||||||
Id = product.Id,
|
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,
|
Name = product.Name,
|
||||||
Duration = product.Duration,
|
Duration = product.Duration,
|
||||||
Caliber = product.Caliber,
|
Caliber = product.Caliber,
|
||||||
ApprovalNumber = product.ApprovalNumber,
|
ApprovalNumber = product.ApprovalNumber,
|
||||||
Weight = product.Weight,
|
Weight = product.Weight,
|
||||||
Nec = product.Nec,
|
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,
|
Image = product.Image,
|
||||||
Link = product.Link,
|
Link = product.Link,
|
||||||
ClassificationId = product.ClassificationId,
|
ClassificationId = product.ClassificationId,
|
||||||
@@ -72,4 +76,4 @@ public class GetProductEndpoint(PyroFetesDbContext db)
|
|||||||
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
await Send.OkAsync(responseDto, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,8 +2,6 @@
|
|||||||
using PyroFetes.DTO.Product.Response;
|
using PyroFetes.DTO.Product.Response;
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.Product.Request;
|
|
||||||
using PyroFetes.DTO.Product.Response;
|
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Product;
|
namespace PyroFetes.Endpoints.Product;
|
||||||
@@ -37,21 +35,19 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mise à jour des propriétés principales du produit
|
// 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.Name = req.Name;
|
||||||
product.Duration = req.Duration;
|
product.Duration = req.Duration;
|
||||||
product.Caliber = req.Caliber;
|
product.Caliber = req.Caliber;
|
||||||
product.ApprovalNumber = req.ApprovalNumber;
|
product.ApprovalNumber = req.ApprovalNumber;
|
||||||
product.Weight = req.Weight;
|
product.Weight = req.Weight;
|
||||||
product.Nec = req.Nec;
|
product.Nec = req.Nec;
|
||||||
product.SellingPrice = req.SellingPrice;
|
|
||||||
product.Image = req.Image;
|
product.Image = req.Image;
|
||||||
product.Link = req.Link;
|
product.Link = req.Link;
|
||||||
product.ClassificationId = req.ClassificationId;
|
product.ClassificationId = req.ClassificationId;
|
||||||
product.ProductCategoryId = req.ProductCategoryId;
|
product.ProductCategoryId = req.ProductCategoryId;
|
||||||
|
|
||||||
// Mise à jour des prix fournisseurs associés
|
// Mise à jour des prix fournisseurs associés
|
||||||
// On supprime les anciens enregistrements pour les remplacer
|
|
||||||
db.Prices.RemoveRange(product.Prices);
|
db.Prices.RemoveRange(product.Prices);
|
||||||
foreach (var s in req.Suppliers)
|
foreach (var s in req.Suppliers)
|
||||||
{
|
{
|
||||||
@@ -64,7 +60,6 @@ public class UpdateProductEndpoint(PyroFetesDbContext db)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mise à jour des entrepôts associés
|
// Mise à jour des entrepôts associés
|
||||||
// On supprime les anciens liens avant d'ajouter les nouveaux
|
|
||||||
db.WarehouseProducts.RemoveRange(product.WarehouseProducts);
|
db.WarehouseProducts.RemoveRange(product.WarehouseProducts);
|
||||||
foreach (var w in req.Warehouses)
|
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);
|
await db.SaveChangesAsync(ct);
|
||||||
|
|
||||||
// Construction de la réponse renvoyée au client
|
// 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
|
var response = new GetProductDto
|
||||||
{
|
{
|
||||||
Id = product.Id,
|
Id = product.Id,
|
||||||
Reference = req.References,
|
Reference = req.References, // DTO garde int pour cohérence
|
||||||
Name = req.Name,
|
Name = req.Name,
|
||||||
Duration = req.Duration,
|
Duration = req.Duration,
|
||||||
Caliber = req.Caliber,
|
Caliber = req.Caliber,
|
||||||
ApprovalNumber = req.ApprovalNumber,
|
ApprovalNumber = req.ApprovalNumber,
|
||||||
Weight = req.Weight,
|
Weight = req.Weight,
|
||||||
Nec = req.Nec,
|
Nec = req.Nec,
|
||||||
SellingPrice = req.SellingPrice,
|
// Le prix de vente est pris depuis Prices
|
||||||
|
SellingPrice = req.Suppliers.FirstOrDefault()?.SellingPrice ?? 0,
|
||||||
Image = req.Image,
|
Image = req.Image,
|
||||||
Link = req.Link,
|
Link = req.Link,
|
||||||
ClassificationId = req.ClassificationId,
|
ClassificationId = req.ClassificationId,
|
||||||
ProductCategoryId = req.ProductCategoryId,
|
ProductCategoryId = req.ProductCategoryId,
|
||||||
|
|
||||||
// Mapping des fournisseurs pour la réponse
|
|
||||||
Suppliers = req.Suppliers.Select(s => new ProductSupplierPriceDto
|
Suppliers = req.Suppliers.Select(s => new ProductSupplierPriceDto
|
||||||
{
|
{
|
||||||
SupplierId = s.SupplierId,
|
SupplierId = s.SupplierId,
|
||||||
SellingPrice = s.SellingPrice
|
SellingPrice = s.SellingPrice
|
||||||
}).ToList(),
|
}).ToList(),
|
||||||
|
|
||||||
// Mapping des entrepôts pour la réponse
|
|
||||||
Warehouses = req.Warehouses.Select(w => new GetProductWarehouseDto
|
Warehouses = req.Warehouses.Select(w => new GetProductWarehouseDto
|
||||||
{
|
{
|
||||||
WarehouseId = w.WarehouseId,
|
WarehouseId = w.WarehouseId,
|
||||||
Quantity = w.Quantity,
|
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()
|
}).ToList()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Envoi de la réponse HTTP 200 avec les données du produit mis à jour
|
|
||||||
await Send.OkAsync(response, ct);
|
await Send.OkAsync(response, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,11 +13,9 @@ public class DeleteWarehouseEndpoint(PyroFetesDbContext db) : Endpoint<DeleteWar
|
|||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
// L’annotation correcte du paramètre est {id}, pas {@id}
|
Delete("/api/warehouse/{id}");
|
||||||
Delete("/api/warehouse/{@id}", x => new { x.Id });
|
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct)
|
public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
// On charge aussi les WarehouseProducts liés pour les supprimer proprement
|
// On charge aussi les WarehouseProducts liés pour les supprimer proprement
|
||||||
|
@@ -16,7 +16,7 @@ public class GetWarehouseEndpoint(PyroFetesDbContext db)
|
|||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
// Pas de "@id" ici, juste {id}
|
// Pas de "@id" ici, juste {id}
|
||||||
Get("/api/warehouses/{@id}", x => new { x.Id });
|
Get("/api/warehouses/{Id}");
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,7 +12,7 @@ public class UpdateWarehouseEndpoint(PyroFetesDbContext db)
|
|||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
// Utilise {id} plutôt que {@id}
|
// Utilise {id} plutôt que {@id}
|
||||||
Put("/api/warehouses/{@id}", x => new { x.Id });
|
Put("/api/warehouses/{Id}");
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
PyroFetes/Models/Login.cs
Normal file
13
PyroFetes/Models/Login.cs
Normal 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; }
|
||||||
|
}
|
@@ -5,14 +5,13 @@ namespace PyroFetes.Models
|
|||||||
public class Product
|
public class Product
|
||||||
{
|
{
|
||||||
[Key] public int Id { get; set; }
|
[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, MaxLength(100)] public string? Name { get; set; }
|
||||||
[Required] public decimal Duration {get; set;}
|
[Required] public decimal Duration {get; set;}
|
||||||
[Required] public decimal Caliber { get; set; }
|
[Required] public decimal Caliber { get; set; }
|
||||||
[Required] public int ApprovalNumber { get; set; }
|
[Required] public int ApprovalNumber { get; set; }
|
||||||
[Required] public decimal Weight { get; set; }
|
[Required] public decimal Weight { get; set; }
|
||||||
[Required] public decimal Nec { get; set; }
|
[Required] public decimal Nec { get; set; }
|
||||||
[Required] public decimal SellingPrice { get; set; }
|
|
||||||
[Required] public string? Image { get; set; }
|
[Required] public string? Image { get; set; }
|
||||||
[Required, MaxLength(200)] public string? Link { get; set; }
|
[Required, MaxLength(200)] public string? Link { get; set; }
|
||||||
[Required] public int MinimalQuantity { get; set; }
|
[Required] public int MinimalQuantity { get; set; }
|
||||||
|
@@ -2,18 +2,27 @@ using API;
|
|||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using FastEndpoints.Swagger;
|
using FastEndpoints.Swagger;
|
||||||
using PyroFetes;
|
using PyroFetes;
|
||||||
|
using FastEndpoints.Security;
|
||||||
|
|
||||||
|
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet
|
// 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
|
// On ajoute ici la configuration de la base de données
|
||||||
builder.Services.AddDbContext<PyroFetesDbContext>();
|
builder.Services.AddDbContext<PyroFetesDbContext>();
|
||||||
|
|
||||||
// On construit l'application en lui donnant vie
|
// On construit l'application en lui donnant vie
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
app.UseFastEndpoints().UseSwaggerGen();
|
app.UseAuthentication()
|
||||||
|
.UseAuthorization()
|
||||||
|
.UseFastEndpoints()
|
||||||
|
.UseSwaggerGen();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
@@ -7,16 +7,19 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||||
<PackageReference Include="FastEndpoints" Version="7.0.1" />
|
<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="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" Version="8.0.20" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.20">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.20">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.20" />
|
<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>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -49,6 +49,7 @@ public class PyroFetesDbContext : DbContext
|
|||||||
public DbSet<User> Users { get; set; }
|
public DbSet<User> Users { get; set; }
|
||||||
public DbSet<Warehouse> Warehouses { get; set; }
|
public DbSet<Warehouse> Warehouses { get; set; }
|
||||||
public DbSet<WarehouseProduct> WarehouseProducts { get; set; }
|
public DbSet<WarehouseProduct> WarehouseProducts { get; set; }
|
||||||
|
public DbSet<Login> Logins { get; set; }
|
||||||
|
|
||||||
// Database configuration
|
// Database configuration
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
Reference in New Issue
Block a user