diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs new file mode 100644 index 0000000..f42c5b9 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/AddressController.cs @@ -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 _logger; + private readonly InventoryContext _context; + private readonly SignInManager _signInMan; + private readonly Microsoft.AspNetCore.Identity.UserManager _userMan; + + #endregion + + #region Ctor + public AddressController(ILogger logger, + InventoryContext context, + SignInManager signInMan, + Microsoft.AspNetCore.Identity.UserManager userMan) { + _logger = logger; + _context = context; + _userMan = userMan; + _signInMan = signInMan; + } + + #endregion + + #region API Methods + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Addresses"), Route("ListAddresses")] + public async Task>> GetList(bool? all) { + IList 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(); + } + + 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> Get(int id) { + IList 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(); + } + + 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> 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> Patch(AddressModel ad) { + IList 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(); + } + + 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> Delete(int id) { + IList 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(); + } + + 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 +} diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index 2931a81..03ed435 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -55,10 +55,10 @@ public class InvoiceController : Controller { roles = new List(); } - 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) { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index c645a27..2fa4bcd 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -42,8 +42,13 @@ public class ProductController : ControllerBase { } #endregion - + #region API Methods + [EnableCors("_myAllowSpecificOrigins"), Route("Quantity"), HttpGet(Name = "Product"), AllowAnonymous] + public ActionResult ProdCount(int id) { + return _context.Products.FirstOrDefault(x => x.Id == id).Quantity; + } + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous] public ActionResult Get(int id) { ProductModel prod; @@ -78,10 +83,20 @@ public class ProductController : ControllerBase { } [EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")] - public ActionResult Patch(ProductModel prod) { + public async Task> 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 DeleteProduct(int id) { + public ActionResult 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 } \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/AddressModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/AddressModel.cs index c438c68..9885ac3 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/AddressModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/AddressModel.cs @@ -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() { } diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/ProductModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/ProductModel.cs index 9365dd1..b703e61 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/ProductModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/ProductModel.cs @@ -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; }