From eef167917a5f70edfed08f4df2a49b8f376061c1 Mon Sep 17 00:00:00 2001 From: Victor Turgeon Date: Tue, 18 Oct 2022 11:50:06 -0400 Subject: [PATCH 1/4] =?UTF-8?q?Basic=20foncitonnalit=C3=A9=20de=20POST=20e?= =?UTF-8?q?t=20DELETE=20pour=20Product?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ProductController.cs | 123 +++++------------- .../GrossesMitainesAPI.csproj | 1 + GrossesMitaines/GrossesMitainesAPI/Program.cs | 3 +- .../src/pages/Inventaire.js | 34 +++-- 4 files changed, 55 insertions(+), 106 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index ab2d9c4..183894b 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -1,16 +1,16 @@ -using Microsoft.AspNetCore.Mvc; -using GrossesMitainesAPI.Models; +using GrossesMitainesAPI.Models; using System.Linq; using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; namespace GrossesMitainesAPI.Controllers; [ApiController, Route("api/[controller]")] -public class ProductController : Controller { +public class ProductController : ControllerBase { private readonly ILogger _logger; private readonly InventoryContext _context; @@ -25,7 +25,8 @@ public class ProductController : Controller { Product prod; try { prod = _context.Products.Where(x => x.Id == id).First(); - } catch (Exception e) { + } + catch (Exception e) { _logger.LogError(8, e.Message); prod = new Product(); } @@ -34,38 +35,7 @@ public class ProductController : Controller { [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() { - Title = title, - Category = category, - Description = description, - Price = price.HasValue ? (decimal)price : 0.01M, - PromoPrice = promoprice.HasValue ? (decimal)promoprice : 0.01M, - Quantity = quantity.HasValue ? (uint)quantity : 0, - ImageName = imagename - }; - - switch (status) { - case "isAvailable": - prod.Status = Product.States.Available; - break; - case "isUnavailable": - prod.Status = Product.States.Unavailable; - break; - case "isBackOrder": - prod.Status = Product.States.BackOrder; - break; ; - case "isClearance": - prod.Status = Product.States.Clearance; - break; - case "isPromotion": - prod.Status = Product.States.Promotion; - break; - case "isDiscontinued": - prod.Status = Product.States.Discontinued; - break; - default: break; - } + public ActionResult Post(Product prod) { if (prod.Price <= prod.PromoPrice) prod.PromoPrice = prod.Price - 0.01M; @@ -73,67 +43,28 @@ public class ProductController : Controller { try { _context.Products.Add(prod); _context.SaveChanges(); - } catch (Exception e) { - _logger.LogError(8, e.Message); } + catch (Exception e) { + _logger.LogError(8, e.Message); + return BadRequest(); + } + + return prod; } [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(); + public ActionResult Put(Product prod) { + try { + _context.Products.Update(prod); + _context.SaveChanges(); + } + catch (Exception e) { + _logger.LogError(8, e.Message); + return BadRequest(); + } - if (prod == new Product()) - Post(title, category, description, price, promoprice, quantity, status, imagename); - else try { - if (title != null || title != "") - prod.Title = title; - - if (category != null || category != "") - prod.Category = category; - - if (description != null || description != "") - prod.Description = description; - - if (price.HasValue || price > 0) - prod.Price = (decimal)price; - - if (promoprice.HasValue || promoprice > prod.Price) - prod.PromoPrice = (decimal)promoprice; - - if (quantity.HasValue) - prod.Quantity = (uint)quantity; - - switch (status) { - case "isAvailable": - prod.Status = Product.States.Available; - break; - case "isUnavailable": - prod.Status = Product.States.Unavailable; - break; - case "isBackOrder": - prod.Status = Product.States.BackOrder; - break; ; - case "isClearance": - prod.Status = Product.States.Clearance; - break; - case "isPromotion": - prod.Status = Product.States.Promotion; - break; - case "isDiscontinued": - prod.Status = Product.States.Discontinued; - break; - default: break; - } - - if (imagename != null || imagename != "") - prod.ImageName = imagename; - - _context.Products.Update(prod); - _context.SaveChanges(); - } catch (Exception e) { - _logger.LogError(8, e.Message); - } + return prod; } [EnableCors("_myAllowSpecificOrigins")] @@ -187,19 +118,23 @@ public class ProductController : Controller { _context.Products.Update(prod); _context.SaveChanges(); - } catch (Exception e) { + } + catch (Exception e) { _logger.LogError(8, e.Message); } } [EnableCors("_myAllowSpecificOrigins")] [HttpDelete(Name = "Product")] - public void DeleteProduct(int id) { + public IActionResult DeleteProduct(int id) { try { _context.Products.Remove(_context.Products.Where(x => x.Id == id).First()); _context.SaveChanges(); - } catch (Exception e) { + } + catch (Exception e) { _logger.LogError(8, e.Message); } + + return Ok(); } } \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj index 0aa0a40..ce73fb6 100644 --- a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj +++ b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj @@ -7,6 +7,7 @@ + all diff --git a/GrossesMitaines/GrossesMitainesAPI/Program.cs b/GrossesMitaines/GrossesMitainesAPI/Program.cs index 8f0ca4d..e1921cd 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Program.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Program.cs @@ -10,7 +10,8 @@ builder.Services.AddCors(options => { policy => { policy.WithOrigins("http://localhost:3000", "http://localhost:3001") - .AllowAnyMethod(); + .AllowAnyMethod() + .AllowAnyHeader(); }); }); diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js index 381db0b..ed4c7d6 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Inventaire.js @@ -1,14 +1,9 @@ import { useEffect, useState } from "react"; -import axios from "axios"; import Button from '../components/Button' import SimpleItemList from "../components/SimpleItemList"; import Ajouter from "../components/Ajouter"; -const API_URL = 'https://localhost:7292/' -const INVENTAIRE_URL = API_URL + 'api/Inventory' -const PRODUIT_URL = API_URL + 'api/Product' - const Inventaire = () => { const [morceaux, setMorceaux] = useState([]); @@ -16,23 +11,40 @@ const Inventaire = () => { useEffect(() => { async function fetchData() { - const res = await axios.get(INVENTAIRE_URL); - setMorceaux(res.data); + const response = await fetch(`https://localhost:7292/api/Inventory?all=true`); + const json = await response.json(); + if (json.length > 0) + setMorceaux([...json]); } fetchData(); document.title = 'Inventaire'; }, []); const handleAddItem = async (morceau) => { - const res = await axios.post(PRODUIT_URL, morceau) - setMorceaux([...morceaux, { ...morceau, id: res.data.id }]); + console.log(morceau); + + const response = await fetch(`https://localhost:7292/api/Product`, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(morceau) + }) + + const newMorceau = response.json(); + + if (response.ok) + setMorceaux([...morceaux, { ...newMorceau }]); + else + console.log("Erreur de creation " + morceau); }; const handleDeleteItem = async (id) => { - const response = await fetch(`https://localhost:7292/Product?id=${id}`, { method: 'DELETE', mode: 'cors' }); - // const res = await axios.delete(`${INVENTAIRE_URL}/${id}`) + const response = await fetch(`https://localhost:7292/api/Product?id=${id}`, { method: 'DELETE', mode: 'cors' }); + if (response.ok) setMorceaux(morceaux.filter((morceau) => morceau.id !== id)) else From ecb39e3b549c83fc44c9c89dc9b8625c78a90440 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:37:18 -0700 Subject: [PATCH 2/4] Update SearchController.cs --- .../GrossesMitainesAPI/Controllers/SearchController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 3292fcb..52fe2ea 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -28,7 +28,7 @@ public class SearchController : Controller { [EnableCors("_myAllowSpecificOrigins")] [HttpPost(Name = "Search")] public IEnumerable Post(string query, bool? preview, bool? deep) { - const int PREVIEW = 3; + const int PREVIEW = 4; List products = new(); query = query.Trim(); From 7447b1ad401afad2f2868a61248e91b9b67852cd Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:48:34 -0700 Subject: [PATCH 3/4] lolllllllll --- .../GrossesMitainesAPI/Controllers/InventoryController.cs | 2 +- .../GrossesMitainesAPI/Controllers/SearchController.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 6aafcb5..4f8210e 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -118,7 +118,7 @@ public class InventoryController : Controller { if (prod.Quantity == 0) prod.Status = prod.Status == Product.States.Clearance? Product.States.Discontinued: - Product.States.Unavailable; + Product.States.BackOrder; } else { _logger.LogError(8, "Vente de produit pas en stock."); diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 52fe2ea..1ac8c5c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -41,8 +41,8 @@ public class SearchController : Controller { List title = new(), desc = new(), cat = new(); query = query.ToLower(); - foreach (Product prod in _searchCache) { - string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(), + foreach (Product prod in _searchCache) { + string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(), sCat = prod.Category.Replace(".", " ").Replace(",", " ").ToLower(), sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower(); From d031b0b998b1ed6d5b93ab418183f5af4a213f79 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:53:57 -0700 Subject: [PATCH 4/4] Update InventoryController.cs --- .../Controllers/InventoryController.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 4f8210e..40baa59 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -105,14 +105,16 @@ public class InventoryController : Controller { // Inventory/Delete => Décrémenter un produit. [EnableCors("_myAllowSpecificOrigins")] [HttpDelete(Name = "Inventory")] - public void Delete(int? id) { + public ActionResult Delete(int? id) { + int rid = 0; if (!id.HasValue) { _logger.LogError(8, "Delete sans Id."); - return; + return BadRequest(); } try { Product prod = _context.Products.First(x => x.Id == id); + rid = prod.Id; if (prod.Quantity > 0) { prod.Quantity = prod.Quantity - 1; if (prod.Quantity == 0) @@ -122,13 +124,15 @@ public class InventoryController : Controller { } else { _logger.LogError(8, "Vente de produit pas en stock."); - return; + return BadRequest(); } _context.Products.Update(prod); _context.SaveChanges(); } catch (Exception e) { _logger.LogError(8, e.Message); + return BadRequest(); } + return rid; } }