diff --git a/.gitignore b/.gitignore index c8aa4a0..8ad2a70 100644 --- a/.gitignore +++ b/.gitignore @@ -349,3 +349,8 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ + +# ignore les images pour ne pas avoir pleins d'image non-lié +GrossesMitaines/GrossesMitainesAPI/Images/* +!GrossesMitaines/GrossesMitainesAPI/Images/default.jpg +!GrossesMitaines/GrossesMitainesAPI/Images/default_thumbnail.jpg diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs new file mode 100644 index 0000000..b3e921a --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs @@ -0,0 +1,67 @@ +using GrossesMitainesAPI.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace GrossesMitainesAPI.Controllers { + [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), Authorize(AuthenticationSchemes = "Identity.Application")] + + public class ImageController : ControllerBase { + + + private readonly InventoryContext _context; + private readonly IWebHostEnvironment _hostEnvironment; + private readonly ILogger _logger; + + public ImageController(ILogger logger, InventoryContext context, IWebHostEnvironment hostEnvironment) { + _context = context; + _hostEnvironment = hostEnvironment; + _logger = logger; + } + + + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous] + public IActionResult Get(int id, bool? thumbnail = false) { + + string path; + string filename; + string filetype; + + try { + var prod = _context.Products.Where(x => x.Id == id).First(); + filename = prod.ImageName ?? throw new Exception("Unable to find product image name. Sending default.jpg instead..."); + } + catch (Exception e) { + _logger.LogError(8, e.Message); + filename = "default.jpg"; + } + + + path = thumbnail == true ? Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(filename) + "_thumbnail" + Path.GetExtension(filename)) + : Path.Combine(_hostEnvironment.ContentRootPath, "Images", filename); + + if (!System.IO.File.Exists(path)) { + _logger.LogError(8, "Unable to find image. Sending default image instead..."); + path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", "default.jpg"); + } + + + switch (Path.GetExtension(path)) { + case ".jpg": + case ".jpeg": + filetype = "image/jpeg"; + break; + case ".png": + filetype = "image/png"; + break; + default: + filetype = "image/jpeg"; + break; + } + + + byte[] imageData = System.IO.File.ReadAllBytes(path); + return File(imageData, filetype); + } + } +} diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 259468c..d30a784 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -9,10 +9,10 @@ using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Cors; using GrossesMitainesAPI.Services; using Microsoft.AspNetCore.Authorization; - +using Microsoft.Extensions.Hosting; #endregion -[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), Authorize(AuthenticationSchemes = "Identity.Application")] public class InventoryController : Controller { #region Constants @@ -24,14 +24,16 @@ public class InventoryController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; private readonly DatabaseCacheService _cache; + private readonly IWebHostEnvironment _hostEnvironment; #endregion #region Ctor - public InventoryController(ILogger logger, InventoryContext context, DatabaseCacheService cache) { + public InventoryController(ILogger logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) { _context = context; _logger = logger; _cache = cache; + _hostEnvironment = hostEnvironment; } #endregion @@ -41,7 +43,7 @@ public class InventoryController : Controller { public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { bool iscache = false; IQueryable ret; - if (_cache.isOk()) { + if (_cache.isOk()) { ret = _cache.queryCache().Select(x => new ProductViewModel(x)); iscache = true; } @@ -78,20 +80,20 @@ public class InventoryController : Controller { ret = ret.Where(x => x.Status == ProductModel.States.Discontinued); break; case "isPromoted": - ret = ret.Where(x => x.Status == ProductModel.States.Clearance || + ret = ret.Where(x => x.Status == ProductModel.States.Clearance || x.Status == ProductModel.States.Promotion); break; default: break; } switch (order) { case "Price": - ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion || - x.Status == ProductModel.States.Clearance ? + ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion || + x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price); break; case "PriceDesc": - ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion || - x.Status == ProductModel.States.Clearance ? + ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion || + x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price); break; case "Title": @@ -114,9 +116,10 @@ public class InventoryController : Controller { try { if (!lastId.HasValue || lastId == 0) yup = true; - + foreach (ProductViewModel prod in ret.ToList()) { if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) { + lst.Add(prod); add++; } @@ -124,13 +127,15 @@ public class InventoryController : Controller { yup = true; } return lst; - } catch (Exception e) { + } + catch (Exception e) { if (iscache) _logger.LogError(e, "Erreur d'appel de cache."); else _logger.LogError(e, "Erreur d'appel d'API."); return new List(); } } + // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. //[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous] //public ActionResult Delete(int? id) { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 1fdfd59..7da53d4 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -10,7 +10,9 @@ using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using GrossesMitainesAPI.Services; - +using System.Drawing; +using System.IO; +using Microsoft.EntityFrameworkCore.Storage; #endregion /// @@ -57,7 +59,7 @@ public class ProductController : ControllerBase { } [EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")] - public async Task> Post(ProductModel prod) { + public async Task> Post([FromForm] ProductModel prod) { if (prod.Price <= prod.PromoPrice) prod.PromoPrice = prod.Price - 0.01M; try { @@ -110,10 +112,33 @@ public class ProductController : ControllerBase { string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-'); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName); var imagePath = Path.Combine(_hostEnvironment.ContentRootPath, "Images", imageName); + using (var fileStream = new FileStream(imagePath, FileMode.Create)) { await imageFile.CopyToAsync(fileStream); + SaveImageThumbnail(fileStream, imageName); } return imageName; } + + private void SaveImageThumbnail(FileStream stream, string imageName) { + + try { + const float maxSize = 200f; + Image image = Image.FromStream(stream); + + //Choisi le bon ratio de division pour ne pas dépasser le 200px ni dans height ni dans width + float ratio = image.Width / (image.Height / maxSize) <= maxSize ? image.Height / maxSize : image.Width / maxSize; + + Bitmap resize = new Bitmap(image, new Size((int)(image.Width / ratio), (int)(image.Height / ratio))); + string path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(imageName) + "_thumbnail" + Path.GetExtension(imageName)); + resize.Save(path); + } + catch (Exception ex) { + _logger.LogError(8, ex.Message); + } + + + } + #endregion } \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj index 53a4756..5ac200e 100644 --- a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj +++ b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj @@ -22,6 +22,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/GrossesMitaines/GrossesMitainesAPI/Images/default.jpg b/GrossesMitaines/GrossesMitainesAPI/Images/default.jpg new file mode 100644 index 0000000..ae4d8c6 Binary files /dev/null and b/GrossesMitaines/GrossesMitainesAPI/Images/default.jpg differ diff --git a/GrossesMitaines/GrossesMitainesAPI/Images/default_thumbnail.jpg b/GrossesMitaines/GrossesMitainesAPI/Images/default_thumbnail.jpg new file mode 100644 index 0000000..8e87eb3 Binary files /dev/null and b/GrossesMitaines/GrossesMitainesAPI/Images/default_thumbnail.jpg differ diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/ProductViewModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/ProductViewModel.cs index 97e7071..8003088 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/ProductViewModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/ProductViewModel.cs @@ -13,6 +13,8 @@ public class ProductViewModel { public States Status { get; set; } = States.Available; public string? ImageName { get; set; } + public string? ImageData { get; set; } + public ProductViewModel(ProductModel prod) { this.Id = prod.Id; this.Title = prod.Title; diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/bibi.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/bibi.jpg deleted file mode 100644 index 2a82e19..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/bibi.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/bibi_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/bibi_thumbnail.jpg deleted file mode 100644 index f52ec07..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/bibi_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme.jpg deleted file mode 100644 index c7291c6..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme_thumbnail.jpg deleted file mode 100644 index 3913b6b..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/bonhomme_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee.jpg deleted file mode 100644 index cf3bfdf..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee_thumbnail.jpg deleted file mode 100644 index 75bf204..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/ceintureflechee_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine.jpg deleted file mode 100644 index e79ce13..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine_thumbnail.jpg deleted file mode 100644 index cbd757b..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/chandailquetaine_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/default_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/default_thumbnail.jpg new file mode 100644 index 0000000..8e87eb3 Binary files /dev/null and b/GrossesMitaines/grosses-mitaines-ui/public/images/default_thumbnail.jpg differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/doudou.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/doudou.jpg deleted file mode 100644 index 0b46a5f..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/doudou.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/doudou_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/doudou_thumbnail.jpg deleted file mode 100644 index 340a633..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/doudou_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/foulard.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/foulard.jpg deleted file mode 100644 index a4fa2a0..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/foulard.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/foulard_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/foulard_thumbnail.jpg deleted file mode 100644 index a292a4a..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/foulard_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/gauze.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/gauze.jpg deleted file mode 100644 index 665c2e5..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/gauze.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/gauze_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/gauze_thumbnail.jpg deleted file mode 100644 index 6dcf883..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/gauze_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain.jpg deleted file mode 100644 index 9554d32..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain_thumbnail.jpg deleted file mode 100644 index b6c24da..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/jeanlucmongrain_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/jesus.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/jesus.jpg deleted file mode 100644 index ec7bdcb..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/jesus.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/jesus_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/jesus_thumbnail.jpg deleted file mode 100644 index d789d69..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/jesus_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/kokin.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/kokin.jpg deleted file mode 100644 index 35bd3b5..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/kokin.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/kokin_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/kokin_thumbnail.jpg deleted file mode 100644 index 4ee96b6..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/kokin_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines.jpg deleted file mode 100644 index 55e47fd..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2.jpg deleted file mode 100644 index 65b3a18..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2_thumbnail.jpg deleted file mode 100644 index 5f9a0d9..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines2_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines_thumbnail.jpg deleted file mode 100644 index d8a8b1c..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/mitaines_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH.jpg deleted file mode 100644 index b0da00c..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH_thumbnail.jpg deleted file mode 100644 index aba4cca..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/pantouflesCH_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt.jpg deleted file mode 100644 index 267914a..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt_thumbnail.jpg deleted file mode 100644 index 155199f..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/tshirt_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/tuque.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/tuque.jpg deleted file mode 100644 index dec0f3b..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/tuque.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/tuque_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/tuque_thumbnail.jpg deleted file mode 100644 index 72dae4c..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/tuque_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/vhs.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/vhs.jpg deleted file mode 100644 index 6def063..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/vhs.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/public/images/vhs_thumbnail.jpg b/GrossesMitaines/grosses-mitaines-ui/public/images/vhs_thumbnail.jpg deleted file mode 100644 index 86ff476..0000000 Binary files a/GrossesMitaines/grosses-mitaines-ui/public/images/vhs_thumbnail.jpg and /dev/null differ diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedImage.js b/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedImage.js new file mode 100644 index 0000000..f259820 --- /dev/null +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedImage.js @@ -0,0 +1,26 @@ +import { useState, useEffect } from "react"; + +const FeaturedImage = ({ productId }) => { + + const [imageSrc, setImageSrc] = useState("/images/default_thumbnail.jpg"); + + useEffect(() => { + fetch(`https://localhost:7292/api/Image?id=${productId}`) + .then(response => response.blob()) + .then(blob => { + const imageUrl = URL.createObjectURL(blob); + setImageSrc(imageUrl); + }) + },[]); + + + return ( + {productId + ); +} + +export default FeaturedImage; \ No newline at end of file diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedList.js b/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedList.js index ba7a41a..ef532d3 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedList.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/FeaturedList.js @@ -1,5 +1,7 @@ import { Carousel } from "react-bootstrap"; import { Link } from "react-router-dom"; +import FeaturedImage from "./FeaturedImage"; + const FeaturedList = ({ products }) => { if (products === null) return (<>); @@ -9,10 +11,7 @@ const FeaturedList = ({ products }) => { - +

{product.title}

{product.description}

diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Item.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Item.js index ed1217a..23ec66d 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Item.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Item.js @@ -1,13 +1,7 @@ import { Card } from "react-bootstrap"; +import { useState, useEffect } from "react"; + -// public enum States { -// Available, -// BackOrder, -// Unavailable, -// Clearance, -// Promotion, -// Discontinued -// } function renderStatus(statusCode) { if (statusCode !== undefined) { @@ -62,7 +56,7 @@ function renderPrice(price, newPrice, status) { if (price !== undefined) { - if (status != 3 && status != 4) { + if (status !== 3 && status !== 4) { return ( @@ -87,12 +81,25 @@ function renderPrice(price, newPrice, status) { } -const Item = ({ imageUrl, name, price, newPrice, status }) => { +const Item = ({ productId, name, price, newPrice, status }) => { + + const [imageSrc, setImageSrc] = useState("/images/default_thumbnail.jpg"); + + useEffect(() => { + fetch(`https://localhost:7292/api/Image?id=${productId}&thumbnail=true`) + .then(response => response.blob()) + .then(blob => { + const imageUrl = URL.createObjectURL(blob); + setImageSrc(imageUrl); + }) + }, []); + + if (name !== undefined) { return ( - +
diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/ItemList.js b/GrossesMitaines/grosses-mitaines-ui/src/components/ItemList.js index 2417667..7e6167e 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/ItemList.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/ItemList.js @@ -9,7 +9,8 @@ const ItemList = ({ items }) => { {items.length <= 0 &&

Aucun morceaux à montrer...

} {items.map((item) => - { }, []); const handleAddItem = async (morceau) => { + + console.log(morceau); + + let formData = new FormData(); + Object.keys(morceau).map((k) => { + formData.set(k,morceau[k]); + }); + + console.log(formData); + const response = await fetch(`https://localhost:7292/api/Product`, { method: 'POST', credentials: 'include', + mode: 'cors', headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + 'accept': 'application/json', }, - body: JSON.stringify(morceau) + body: formData }) const newMorceau = await response.json(); @@ -38,7 +48,7 @@ const Inventaire = () => { console.log("Ajout de l'item avec succès: \r\n" + newMorceau); } else - console.log("Erreur de creation " + morceau); + console.log("Erreur de creation " + morceau.title); }; const handleDeleteItem = async (id) => { diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js index 8778daa..e7f8150 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/MorceauDetail.js @@ -7,6 +7,7 @@ const MorceauDetail = () => { const { id } = useParams(); const [item, setItem] = useState({}); const [isLoading, setIsLoading] = useState(false); + const [imageSrc, setImageSrc] = useState("/images/default_thumbnail.jpg"); useEffect(() => { document.title = 'Morceaux'; @@ -16,10 +17,25 @@ const MorceauDetail = () => { const json = await response.json(); setItem(json); } + + fetchData(); + + fetch(`https://localhost:7292/api/Image?id=${id}`) + .then(response => response.blob()) + .then(blob => { + const imageUrl = URL.createObjectURL(blob); + setImageSrc(imageUrl); + }) + setIsLoading(false); }); + + useEffect(() => { + + }, []); + function renderPrice(price, newPrice, status) { if (price !== undefined) { if (status !== 3 && status !== 4) { @@ -99,7 +115,7 @@ const MorceauDetail = () => {
- +

{item.description}