C'est un pAPI.
This commit is contained in:
parent
0b7b9689b1
commit
4608cb5de8
@ -0,0 +1,195 @@
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
|
||||
#region Dependencies
|
||||
using GrossesMitainesAPI.Data;
|
||||
using GrossesMitainesAPI.Models;
|
||||
using GrossesMitainesAPI.Services;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
#endregion
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
||||
Authorize(AuthenticationSchemes = "Identity.Application")]
|
||||
public class AddressController : Controller {
|
||||
#region DI Fields
|
||||
private readonly ILogger<AddressController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private readonly SignInManager<InventoryUser> _signInMan;
|
||||
private readonly Microsoft.AspNetCore.Identity.UserManager<InventoryUser> _userMan;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public AddressController(ILogger<AddressController> logger,
|
||||
InventoryContext context,
|
||||
SignInManager<InventoryUser> signInMan,
|
||||
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_userMan = userMan;
|
||||
_signInMan = signInMan;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region API Methods
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Addresses"), Route("ListAddresses")]
|
||||
public async Task<ActionResult<List<AddressModel>>> GetList(bool? all) {
|
||||
IList<string> roles;
|
||||
InventoryUser user;
|
||||
string id;
|
||||
|
||||
try {
|
||||
user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try {
|
||||
id = _signInMan.Context.User.Identity.GetUserId();
|
||||
if (all is not null && all == true && roles.Contains("Administrateur"))
|
||||
return Ok(_context.Addresses.ToList());
|
||||
else return Ok(user.Adresses.ToList());
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Address")]
|
||||
public async Task<ActionResult<AddressModel>> Get(int id) {
|
||||
IList<string> roles;
|
||||
string userId;
|
||||
AddressModel ad;
|
||||
InventoryUser user;
|
||||
|
||||
try {
|
||||
user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
try {
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try {
|
||||
ad = _context.Addresses.First(a => a.Id == id);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if (roles.Contains("Administrateur") ||
|
||||
(user.Adresses.Contains(ad)))
|
||||
return ad;
|
||||
else return Unauthorized();
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Address")]
|
||||
public async Task<ActionResult<AddressModel>> Post(AddressModel ad) {
|
||||
try {
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
user.Adresses.Add(ad);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
return ad;
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Address")]
|
||||
public async Task<ActionResult<AddressModel>> Patch(AddressModel ad) {
|
||||
IList<string> roles;
|
||||
InventoryUser user;
|
||||
|
||||
try {
|
||||
user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
try {
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
if (roles.Contains("Administrateur") ||
|
||||
user.Adresses.Contains(ad))
|
||||
try {
|
||||
_context.Addresses.Update(ad);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
else return Unauthorized();
|
||||
return ad;
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Address")]
|
||||
public async Task<ActionResult<int>> Delete(int id) {
|
||||
IList<string> roles;
|
||||
AddressModel ad;
|
||||
InventoryUser user;
|
||||
int adId = 0;
|
||||
|
||||
try {
|
||||
user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
try {
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try {
|
||||
ad = _context.Addresses.First(a => a.Id == id);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
adId = ad.Id;
|
||||
if (roles.Contains("Administrateur") ||
|
||||
user.Adresses.Contains(ad))
|
||||
try {
|
||||
user.Adresses.Remove(ad);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
else return Unauthorized();
|
||||
return adId;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@ -55,10 +55,10 @@ public class InvoiceController : Controller {
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try { // TODO: Débugger ça.
|
||||
try {
|
||||
id = _signInMan.Context.User.Identity.GetUserId();
|
||||
if (all is not null && all == true && roles.Contains("Administrateur"))
|
||||
return Ok(_context.Invoices/*.Include("LinkedAccount").Include("ShippingAddress"/*"LinkedAccount, ShippingAddress")*/.ToList());
|
||||
return Ok(_context.Invoices.ToList());
|
||||
else return Ok(_context.Invoices.Include("ShippingAddress").Where(x => x.LinkedAccount != null &&
|
||||
x.LinkedAccount.Id == id).ToList());
|
||||
} catch (Exception e) {
|
||||
|
@ -42,8 +42,13 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region API Methods
|
||||
[EnableCors("_myAllowSpecificOrigins"), Route("Quantity"), HttpGet(Name = "Product"), AllowAnonymous]
|
||||
public ActionResult<uint> ProdCount(int id) {
|
||||
return _context.Products.FirstOrDefault(x => x.Id == id).Quantity;
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
|
||||
public ActionResult<ProductViewModel> Get(int id) {
|
||||
ProductModel prod;
|
||||
@ -78,10 +83,20 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")]
|
||||
public ActionResult<ProductModel> Patch(ProductModel prod) {
|
||||
public async Task<ActionResult<ProductModel>> Patch([FromForm] ProductModel prod) {
|
||||
string? oldImage = "";
|
||||
try {
|
||||
if (prod.ImageFile is not null) {
|
||||
oldImage = _context.Products.FirstOrDefault(x => x.Id == prod.Id).ImageName;
|
||||
if (oldImage == prod.ImageName)
|
||||
oldImage = "";
|
||||
prod.ImageName = await SaveImage(prod.ImageFile);
|
||||
}
|
||||
|
||||
_context.Products.Update(prod);
|
||||
_context.SaveChanges();
|
||||
if (oldImage is not null and not "")
|
||||
DeleteImages(oldImage);
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
@ -92,10 +107,13 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Product")]
|
||||
public ActionResult<int> DeleteProduct(int id) {
|
||||
public ActionResult<int> Delete(int id) {
|
||||
try {
|
||||
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
|
||||
var prod = _context.Products.Where(x => x.Id == id).First();
|
||||
string imageName = prod.ImageName;
|
||||
_context.Products.Remove(prod);
|
||||
_context.SaveChanges();
|
||||
DeleteImages(imageName);
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
@ -137,5 +155,13 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteImages(string imageName) {
|
||||
var files = System.IO.Directory.GetFiles(_hostEnvironment.ContentRootPath + "/Images")
|
||||
.Where(x => x.Contains(imageName)).ToArray();
|
||||
|
||||
foreach (var file in files)
|
||||
System.IO.File.Delete(_hostEnvironment.ContentRootPath + "/Images/" + file);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@ -16,7 +16,7 @@ public class AddressModel {
|
||||
[Required, MinLength(4), MaxLength(30)]
|
||||
public string Country { get; set; }
|
||||
// Source pour regex: https://stackoverflow.com/questions/15774555/efficient-regex-for-canadian-postal-code-function
|
||||
//[Required, RegularExpression(@"/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i")]
|
||||
//[Required, RegularExpression(@"/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i")] TODO: REGEX
|
||||
public string PostalCode { get; set; }
|
||||
|
||||
public AddressModel() { }
|
||||
|
@ -22,7 +22,7 @@ public class ProductModel {
|
||||
[Required]
|
||||
public string Description { get; set; } = "Lorem Ipsum.";
|
||||
[Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
|
||||
public decimal Price { get; set; } = 0;
|
||||
public decimal Price { get; set; } = 0.01M;
|
||||
[Required, Range(0.00, (double)decimal.MaxValue)]
|
||||
public decimal PromoPrice { get; set; } = 0;
|
||||
public uint Quantity { get; set; } = 0;
|
||||
@ -31,7 +31,7 @@ public class ProductModel {
|
||||
public uint Sales { get; set; } = 0;
|
||||
public DateTime? LastSale { get; set; }
|
||||
public DateTime? LastHit { get; set; }
|
||||
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
|
||||
public string? ImageName { get; set; } = ""; // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
|
||||
|
||||
[NotMapped]
|
||||
public IFormFile? ImageFile { get; set; }
|
||||
|
Loading…
Reference in New Issue
Block a user