Merge branch 'react-version' of https://github.com/MarcEricMartel/420-5DW-HY-TP into react-version

This commit is contained in:
MarcEricMartel 2022-10-18 10:17:31 -07:00
commit 2d30c4e2b9
5 changed files with 21 additions and 3 deletions

View File

@ -3,9 +3,11 @@ using GrossesMitainesAPI.Models;
using System.Linq; using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Cors;
namespace GrossesMitainesAPI.Controllers; namespace GrossesMitainesAPI.Controllers;
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")] [ApiController, Route("api/[controller]")]
public class InventoryController : Controller { public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger; private readonly ILogger<InventoryController> _logger;
@ -16,6 +18,7 @@ public class InventoryController : Controller {
_logger = logger; _logger = logger;
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState) { public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState) {
const int AMOUNT = 5; const int AMOUNT = 5;
@ -98,6 +101,7 @@ public class InventoryController : Controller {
} }
// Inventory/Delete => Décrémenter un produit. // Inventory/Delete => Décrémenter un produit.
[EnableCors("_myAllowSpecificOrigins")]
[HttpDelete(Name = "Inventory")] [HttpDelete(Name = "Inventory")]
public void Delete(int? id) { public void Delete(int? id) {
if (!id.HasValue) { if (!id.HasValue) {

View File

@ -5,6 +5,7 @@ using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Cors;
namespace GrossesMitainesAPI.Controllers; namespace GrossesMitainesAPI.Controllers;
@ -18,6 +19,7 @@ public class ProductController : Controller {
_context = context; _context = context;
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpGet(Name = "Product"), AllowAnonymous] [HttpGet(Name = "Product"), AllowAnonymous]
public Product Get(int id) { public Product Get(int id) {
Product prod; Product prod;
@ -30,6 +32,7 @@ public class ProductController : Controller {
return prod; return prod;
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpPost(Name = "Product")] [HttpPost(Name = "Product")]
public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
Product prod = new() { Product prod = new() {
@ -75,6 +78,7 @@ public class ProductController : Controller {
} }
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpPut(Name = "Product")] [HttpPut(Name = "Product")]
public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { 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(); Product prod = _context.Products.Where(x => x.Id == id).FirstOrDefault();
@ -132,6 +136,7 @@ public class ProductController : Controller {
} }
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpPatch(Name = "Product")] [HttpPatch(Name = "Product")]
public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
try { try {
@ -187,6 +192,7 @@ public class ProductController : Controller {
} }
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpDelete(Name = "Product")] [HttpDelete(Name = "Product")]
public void DeleteProduct(int id) { public void DeleteProduct(int id) {
try { try {

View File

@ -4,9 +4,11 @@ using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
namespace GrossesMitainesAPI.Controllers; namespace GrossesMitainesAPI.Controllers;
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")] [ApiController, Route("api/[controller]")]
public class SearchController : Controller { public class SearchController : Controller {
private readonly ILogger<SearchController> _logger; private readonly ILogger<SearchController> _logger;
@ -17,6 +19,7 @@ public class SearchController : Controller {
_context = context; _context = context;
} }
[EnableCors("_myAllowSpecificOrigins")]
[HttpPost(Name = "Search")] [HttpPost(Name = "Search")]
public IEnumerable<Product> Post(string query, bool? preview) { public IEnumerable<Product> Post(string query, bool? preview) {
const int PREVIEW = 3; const int PREVIEW = 3;

View File

@ -9,7 +9,8 @@ builder.Services.AddCors(options => {
options.AddPolicy(name: MyAllowSpecificOrigins, options.AddPolicy(name: MyAllowSpecificOrigins,
policy => { policy => {
policy.WithOrigins("http://localhost:3000", policy.WithOrigins("http://localhost:3000",
"http://localhost:3001"); "http://localhost:3001")
.AllowAnyMethod();
}); });
}); });

View File

@ -30,9 +30,13 @@ const Inventaire = () => {
}; };
const handleDeleteItem = async (id) => { 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) => { const handleModifyItem = async (id) => {