diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 7758445..dc3be9a 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -3,9 +3,11 @@ using GrossesMitainesAPI.Models; using System.Linq; using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Cors; namespace GrossesMitainesAPI.Controllers; +[EnableCors("_myAllowSpecificOrigins")] [ApiController, Route("api/[controller]")] public class InventoryController : Controller { private readonly ILogger _logger; @@ -16,6 +18,7 @@ public class InventoryController : Controller { _logger = logger; } + [EnableCors("_myAllowSpecificOrigins")] [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState) { const int AMOUNT = 5; @@ -98,6 +101,7 @@ public class InventoryController : Controller { } // Inventory/Delete => Décrémenter un produit. + [EnableCors("_myAllowSpecificOrigins")] [HttpDelete(Name = "Inventory")] public void Delete(int? id) { if (!id.HasValue) { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index f19c82c..ab2d9c4 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -5,6 +5,7 @@ using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Cors; namespace GrossesMitainesAPI.Controllers; @@ -18,6 +19,7 @@ public class ProductController : Controller { _context = context; } + [EnableCors("_myAllowSpecificOrigins")] [HttpGet(Name = "Product"), AllowAnonymous] public Product Get(int id) { Product prod; @@ -30,6 +32,7 @@ public class ProductController : Controller { return prod; } + [EnableCors("_myAllowSpecificOrigins")] [HttpPost(Name = "Product")] public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { Product prod = new() { @@ -75,6 +78,7 @@ public class ProductController : Controller { } } + [EnableCors("_myAllowSpecificOrigins")] [HttpPut(Name = "Product")] public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { Product prod = _context.Products.Where(x => x.Id == id).FirstOrDefault(); @@ -132,6 +136,7 @@ public class ProductController : Controller { } } + [EnableCors("_myAllowSpecificOrigins")] [HttpPatch(Name = "Product")] public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { try { @@ -187,6 +192,7 @@ public class ProductController : Controller { } } + [EnableCors("_myAllowSpecificOrigins")] [HttpDelete(Name = "Product")] public void DeleteProduct(int id) { try { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 8f14534..56ccc74 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -4,9 +4,11 @@ using System.Linq; using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; namespace GrossesMitainesAPI.Controllers; +[EnableCors("_myAllowSpecificOrigins")] [ApiController, Route("api/[controller]")] public class SearchController : Controller { private readonly ILogger _logger; @@ -17,6 +19,7 @@ public class SearchController : Controller { _context = context; } + [EnableCors("_myAllowSpecificOrigins")] [HttpPost(Name = "Search")] public IEnumerable Post(string query, bool? preview) { HashSet products = new(); diff --git a/GrossesMitaines/GrossesMitainesAPI/Program.cs b/GrossesMitaines/GrossesMitainesAPI/Program.cs index 533b454..8f0ca4d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Program.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Program.cs @@ -9,7 +9,8 @@ builder.Services.AddCors(options => { options.AddPolicy(name: MyAllowSpecificOrigins, policy => { policy.WithOrigins("http://localhost:3000", - "http://localhost:3001"); + "http://localhost:3001") + .AllowAnyMethod(); }); }); diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js index d4ae3c7..381db0b 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js @@ -30,9 +30,13 @@ const Inventaire = () => { }; const handleDeleteItem = async (id) => { - const res = await axios.delete(`${INVENTAIRE_URL}/${id}`) - setMorceaux(morceaux.filter((morceau) => morceau.id !== id)) + const response = await fetch(`https://localhost:7292/Product?id=${id}`, { method: 'DELETE', mode: 'cors' }); + // const res = await axios.delete(`${INVENTAIRE_URL}/${id}`) + if (response.ok) + setMorceaux(morceaux.filter((morceau) => morceau.id !== id)) + else + console.log("Erreur leur du DELETE de " + id); }; const handleModifyItem = async (id) => {