Merge branch 'react-version' of https://github.com/MarcEricMartel/420-5DW-HY-TP into react-version
This commit is contained in:
commit
bbff60ca54
@ -27,14 +27,16 @@ public class ProductController : ControllerBase {
|
|||||||
private readonly ILogger<ProductController> _logger;
|
private readonly ILogger<ProductController> _logger;
|
||||||
private readonly InventoryContext _context;
|
private readonly InventoryContext _context;
|
||||||
private readonly DatabaseCacheService _cache;
|
private readonly DatabaseCacheService _cache;
|
||||||
|
private readonly IWebHostEnvironment _hostEnvironment;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Ctor
|
#region Ctor
|
||||||
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
|
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_context = context;
|
_context = context;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
|
_hostEnvironment = hostEnvironment;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -55,10 +57,13 @@ public class ProductController : ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
|
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
|
||||||
public ActionResult<Product> Post(Product prod) {
|
public async Task<ActionResult<Product>> Post(Product prod) {
|
||||||
if (prod.Price <= prod.PromoPrice)
|
if (prod.Price <= prod.PromoPrice)
|
||||||
prod.PromoPrice = prod.Price - 0.01M;
|
prod.PromoPrice = prod.Price - 0.01M;
|
||||||
try {
|
try {
|
||||||
|
if (prod.ImageFile != null)
|
||||||
|
prod.ImageName = await SaveImage(prod.ImageFile);
|
||||||
|
|
||||||
_context.Products.Add(prod);
|
_context.Products.Add(prod);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
}
|
}
|
||||||
@ -99,4 +104,16 @@ public class ProductController : ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region UtilityMethods
|
||||||
|
private async Task<string> SaveImage(IFormFile imageFile) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return imageName;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Data.SqlClient.Server;
|
using Microsoft.Data.SqlClient.Server;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
namespace GrossesMitainesAPI.Models;
|
namespace GrossesMitainesAPI.Models;
|
||||||
|
|
||||||
@ -31,4 +32,7 @@ public class Product {
|
|||||||
public DateTime? LastSale { get; set; }
|
public DateTime? LastSale { get; set; }
|
||||||
public DateTime? LastHit { 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; }
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { Dropdown } from "react-bootstrap";
|
import { Dropdown } from "react-bootstrap";
|
||||||
import { Button } from "react-bootstrap";
|
import { Button } from "react-bootstrap";
|
||||||
|
|
||||||
@ -12,14 +12,36 @@ const Ajouter = ({ onCreation }) => {
|
|||||||
const [price, setPrice] = useState("");
|
const [price, setPrice] = useState("");
|
||||||
const [promoPrice, setPromoPrice] = useState("");
|
const [promoPrice, setPromoPrice] = useState("");
|
||||||
const [quantity, setQuantity] = useState("");
|
const [quantity, setQuantity] = useState("");
|
||||||
const [imageName, setImageName] = useState("sqdc.jpg");
|
|
||||||
const [status, setStatusValue] = useState(0);
|
const [status, setStatusValue] = useState(0);
|
||||||
|
|
||||||
|
const [imageFile, setImage] = useState(null);
|
||||||
|
const [imageUrl, setImageUrl] = useState(null);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
if (imageFile)
|
||||||
|
setImageUrl(URL.createObjectURL(imageFile));
|
||||||
|
else
|
||||||
|
setImageUrl(null);
|
||||||
|
|
||||||
|
}, [imageFile]);
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault(); // Empêcher de reloader la page au submit.
|
e.preventDefault(); // Empêcher de reloader la page au submit.
|
||||||
|
|
||||||
// Appeler le comportement onCreation
|
// Appeler le comportement onCreation
|
||||||
onCreation({ title, description, category, price, promoPrice, quantity, imageName, status });
|
onCreation({
|
||||||
|
title: title,
|
||||||
|
description: description,
|
||||||
|
category: category,
|
||||||
|
price: price,
|
||||||
|
promoPrice: promoPrice,
|
||||||
|
quantity: quantity,
|
||||||
|
imageFile: imageFile,
|
||||||
|
imageName: "noName.jpg",
|
||||||
|
status: status
|
||||||
|
});
|
||||||
|
|
||||||
// Reset les états du formulaire.
|
// Reset les états du formulaire.
|
||||||
setTitle("");
|
setTitle("");
|
||||||
@ -28,7 +50,15 @@ const Ajouter = ({ onCreation }) => {
|
|||||||
setPrice("");
|
setPrice("");
|
||||||
setPromoPrice("");
|
setPromoPrice("");
|
||||||
setQuantity("");
|
setQuantity("");
|
||||||
setImageName("sqdc.jpg");
|
setImage(null);
|
||||||
|
setImageUrl(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleImageChange = (e) => {
|
||||||
|
if (e.target.files && e.target.files[0])
|
||||||
|
setImage(e.target.files[0]);
|
||||||
|
else
|
||||||
|
setImage(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -78,11 +108,16 @@ const Ajouter = ({ onCreation }) => {
|
|||||||
onChange={(e) => setQuantity(e.target.value)} />
|
onChange={(e) => setQuantity(e.target.value)} />
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label>Nom Image: </label>
|
<label>Image: </label>
|
||||||
<input type='text' className="form-control form-input"
|
<input
|
||||||
placeholder="Nom de l'image..."
|
className="form-control form-input"
|
||||||
value={imageName}
|
type='file'
|
||||||
onChange={(e) => setImageName(e.target.value)} />
|
accept="image/*"
|
||||||
|
onChange={(e) => handleImageChange(e)} />
|
||||||
|
{imageUrl && <div className="img-preview-container">
|
||||||
|
<img className="img-preview" src={imageUrl} />
|
||||||
|
</div>}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label>Status: </label>
|
<label>Status: </label>
|
||||||
|
@ -543,6 +543,16 @@ html {
|
|||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.img-preview-container{
|
||||||
|
margin-top: 15px;
|
||||||
|
display:flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.img-preview{
|
||||||
|
margin:auto;
|
||||||
|
max-width: 90%;
|
||||||
|
max-height: 200px;
|
||||||
|
}
|
||||||
|
|
||||||
.sorting-container {
|
.sorting-container {
|
||||||
width: 20%;
|
width: 20%;
|
||||||
|
Loading…
Reference in New Issue
Block a user