Merge branch 'react-version' of https://github.com/MarcEricMartel/420-5DW-HY-TP into react-version
This commit is contained in:
commit
98a29cfb33
@ -22,14 +22,14 @@ public class ProductController : ControllerBase {
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins")]
|
||||
[HttpGet(Name = "Product"), AllowAnonymous]
|
||||
public Product Get(int id) {
|
||||
public ActionResult<Product> Get(int id) {
|
||||
Product prod;
|
||||
try {
|
||||
prod = _context.Products.Where(x => x.Id == id).First();
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
prod = new Product();
|
||||
return NotFound();
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
@ -47,7 +47,7 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
|
||||
return prod;
|
||||
@ -62,80 +62,24 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
|
||||
return prod;
|
||||
}
|
||||
|
||||
[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 {
|
||||
Product prod = _context.Products.Where(x => x.Id == id).First();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins")]
|
||||
[HttpDelete(Name = "Product")]
|
||||
public IActionResult DeleteProduct(int id) {
|
||||
public ActionResult<int> DeleteProduct(int id) {
|
||||
try {
|
||||
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
|
||||
_context.SaveChanges();
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
return id;
|
||||
}
|
||||
}
|
@ -45,7 +45,6 @@ public class SearchController : Controller {
|
||||
string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(),
|
||||
sCat = prod.Category.Replace(".", " ").Replace(",", " ").ToLower(),
|
||||
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower();
|
||||
|
||||
if (sTitle.StartsWith(query))
|
||||
products.Add(prod);
|
||||
else if (sTitle.Contains(" " + query + " "))
|
||||
|
@ -21,9 +21,6 @@ const Inventaire = () => {
|
||||
}, []);
|
||||
|
||||
const handleAddItem = async (morceau) => {
|
||||
|
||||
console.log(morceau);
|
||||
|
||||
const response = await fetch(`https://localhost:7292/api/Product`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@ -33,7 +30,8 @@ const Inventaire = () => {
|
||||
body: JSON.stringify(morceau)
|
||||
})
|
||||
|
||||
const newMorceau = response.json();
|
||||
const newMorceau = await response.json();
|
||||
console.log(newMorceau);
|
||||
|
||||
if (response.ok)
|
||||
setMorceaux([...morceaux, { ...newMorceau }]);
|
||||
@ -45,8 +43,12 @@ const Inventaire = () => {
|
||||
|
||||
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))
|
||||
const deletedId = await response.json();
|
||||
|
||||
if (response.ok){
|
||||
setMorceaux(morceaux.filter((morceau) => morceau.id !== deletedId));
|
||||
console.log("DELETE de: "+deletedId +" avec succès!");
|
||||
}
|
||||
else
|
||||
console.log("Erreur leur du DELETE de " + id);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user