RIP pAPI
This commit is contained in:
parent
17d7c1ebb5
commit
5c9ad96335
@ -14,7 +14,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
||||||
Authorize(AuthenticationSchemes = "Identity.Application")]
|
Authorize(AuthenticationSchemes = "Identity.Application", Roles ="Client, Administrateur")]
|
||||||
public class AddressController : Controller {
|
public class AddressController : Controller {
|
||||||
#region DI Fields
|
#region DI Fields
|
||||||
private readonly ILogger<AddressController> _logger;
|
private readonly ILogger<AddressController> _logger;
|
||||||
|
@ -16,7 +16,7 @@ using System.Linq;
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
||||||
Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")]
|
Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Client, Administrateur")]
|
||||||
public class InvoiceController : Controller {
|
public class InvoiceController : Controller {
|
||||||
#region DI Fields
|
#region DI Fields
|
||||||
private readonly ILogger<InvoiceController> _logger;
|
private readonly ILogger<InvoiceController> _logger;
|
||||||
@ -43,15 +43,14 @@ public class InvoiceController : Controller {
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region API Methods
|
#region API Methods
|
||||||
[HttpGet, Authorize(Roles = "Client, Administrateur")]
|
[HttpGet]
|
||||||
public async Task<ActionResult<List<InvoiceModel>>> Get(bool? all = false) {
|
public async Task<ActionResult<List<InvoiceModel>>> Get(bool? all = false) {
|
||||||
IList<string> roles;
|
IList<string> roles;
|
||||||
string id;
|
string id;
|
||||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||||
roles = await _userMan.GetRolesAsync(user);
|
roles = await _userMan.GetRolesAsync(user);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(10, e.Message);
|
_logger.LogError(10, e.Message);
|
||||||
roles = new List<string>();
|
roles = new List<string>();
|
||||||
}
|
}
|
||||||
@ -71,30 +70,27 @@ public class InvoiceController : Controller {
|
|||||||
.Include(x => x.Products)
|
.Include(x => x.Products)
|
||||||
.ThenInclude(y => y.Product)
|
.ThenInclude(y => y.Product)
|
||||||
.Where(x => x.LinkedAccount != null && x.LinkedAccount.Id == id).ToList());
|
.Where(x => x.LinkedAccount != null && x.LinkedAccount.Id == id).ToList());
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(10, e.Message);
|
_logger.LogError(10, e.Message);
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}"), Authorize(Roles = "Client, Administrateur")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<InvoiceModel>> Get(int id) {
|
public async Task<ActionResult<InvoiceModel>> Get(int id) {
|
||||||
IList<string> roles;
|
IList<string> roles;
|
||||||
InvoiceModel inv;
|
InvoiceModel inv;
|
||||||
|
|
||||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||||
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(10, e.Message);
|
_logger.LogError(10, e.Message);
|
||||||
roles = new List<string>();
|
roles = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
inv = _context.Invoices.Where(x => x.Id == id).Include("ShippingAddress").First();
|
inv = _context.Invoices.Where(x => x.Id == id).Include("ShippingAddress").First();
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(10, e.Message);
|
_logger.LogError(10, e.Message);
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
@ -119,7 +115,11 @@ public class InvoiceController : Controller {
|
|||||||
PhoneNumber = sinv.PhoneNumber,
|
PhoneNumber = sinv.PhoneNumber,
|
||||||
PurchaseDate = DateTime.Now
|
PurchaseDate = DateTime.Now
|
||||||
};
|
};
|
||||||
AddressModel ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber &&
|
AddressModel ad;
|
||||||
|
|
||||||
|
if (user is not null) {
|
||||||
|
inv.LinkedAccount = user;
|
||||||
|
ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber &&
|
||||||
x.Appartment == sinv.Appartment &&
|
x.Appartment == sinv.Appartment &&
|
||||||
x.Street == sinv.Street &&
|
x.Street == sinv.Street &&
|
||||||
x.City == sinv.City &&
|
x.City == sinv.City &&
|
||||||
@ -134,14 +134,22 @@ public class InvoiceController : Controller {
|
|||||||
Country = sinv.Country,
|
Country = sinv.Country,
|
||||||
PostalCode = sinv.PostalCode
|
PostalCode = sinv.PostalCode
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
else ad = new() {
|
||||||
|
CivicNumber = sinv.CivicNumber,
|
||||||
|
Appartment = sinv.Appartment,
|
||||||
|
Street = sinv.Street,
|
||||||
|
City = sinv.City,
|
||||||
|
Province = sinv.Province,
|
||||||
|
Country = sinv.Country,
|
||||||
|
PostalCode = sinv.PostalCode
|
||||||
|
};
|
||||||
|
|
||||||
inv.ShippingAddress = ad;
|
inv.ShippingAddress = ad;
|
||||||
if (user is not null)
|
|
||||||
inv.LinkedAccount = user;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
prods = _context.Products.Where(x => sinv.ProdQuant.Select(x => x.Key).Contains(x.Id)).ToList();
|
prods = _context.Products.Where(x => sinv.ProdQuant.Select(x => x.Key).Contains(x.Id)).ToList();
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(8, e.Message);
|
_logger.LogError(8, e.Message);
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
@ -158,8 +166,7 @@ public class InvoiceController : Controller {
|
|||||||
inventProd.Status = inventProd.Status == ProductModel.States.Clearance ?
|
inventProd.Status = inventProd.Status == ProductModel.States.Clearance ?
|
||||||
ProductModel.States.Discontinued :
|
ProductModel.States.Discontinued :
|
||||||
ProductModel.States.BackOrder;
|
ProductModel.States.BackOrder;
|
||||||
}
|
} else inventProd.Quantity -= prod.Value;
|
||||||
else inventProd.Quantity -= prod.Value;
|
|
||||||
inventProd.LastSale = DateTime.Now;
|
inventProd.LastSale = DateTime.Now;
|
||||||
inventProd.Sales += prod.Value;
|
inventProd.Sales += prod.Value;
|
||||||
}
|
}
|
||||||
@ -172,8 +179,7 @@ public class InvoiceController : Controller {
|
|||||||
_context.Invoices.Add(inv);
|
_context.Invoices.Add(inv);
|
||||||
_context.Products.UpdateRange(prods);
|
_context.Products.UpdateRange(prods);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(8, e.Message);
|
_logger.LogError(8, e.Message);
|
||||||
return BadRequest(e.InnerException.Message);
|
return BadRequest(e.InnerException.Message);
|
||||||
}
|
}
|
||||||
@ -190,16 +196,14 @@ public class InvoiceController : Controller {
|
|||||||
try { // Trouver la commande.
|
try { // Trouver la commande.
|
||||||
inv = _context.Invoices.Where(x => x.Id == id)
|
inv = _context.Invoices.Where(x => x.Id == id)
|
||||||
.Include("Product").First();
|
.Include("Product").First();
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(8, e.Message);
|
_logger.LogError(8, e.Message);
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||||
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(10, e.Message);
|
_logger.LogError(10, e.Message);
|
||||||
roles = new List<string>();
|
roles = new List<string>();
|
||||||
}
|
}
|
||||||
@ -231,8 +235,7 @@ public class InvoiceController : Controller {
|
|||||||
try {
|
try {
|
||||||
_context.Update(inv);
|
_context.Update(inv);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
_logger.LogError(8, e.Message);
|
_logger.LogError(8, e.Message);
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,9 @@ builder.Services.AddIdentityCore<InventoryUser>()
|
|||||||
.AddRoles<IdentityRole>()
|
.AddRoles<IdentityRole>()
|
||||||
.AddEntityFrameworkStores<InventoryContext>()
|
.AddEntityFrameworkStores<InventoryContext>()
|
||||||
.AddSignInManager();
|
.AddSignInManager();
|
||||||
|
builder.Services.Configure<IdentityOptions>(o =>
|
||||||
|
o.User.RequireUniqueEmail = true);
|
||||||
|
|
||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorization();
|
||||||
builder.Services.AddAuthentication().AddIdentityCookies();
|
builder.Services.AddAuthentication().AddIdentityCookies();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user