react-version #1
@ -1,17 +1,17 @@
|
||||
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;
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins")]
|
||||
[ApiController, Route("api/[controller]")]
|
||||
public class ProductController : Controller {
|
||||
public class ProductController : ControllerBase {
|
||||
private readonly ILogger<ProductController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
|
||||
@ -26,7 +26,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();
|
||||
}
|
||||
@ -35,38 +36,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<Product> Post(Product prod) {
|
||||
|
||||
if (prod.Price <= prod.PromoPrice)
|
||||
prod.PromoPrice = prod.Price - 0.01M;
|
||||
@ -74,67 +44,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<Product> 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")]
|
||||
@ -188,19 +119,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();
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
@ -10,7 +10,8 @@ builder.Services.AddCors(options => {
|
||||
policy => {
|
||||
policy.WithOrigins("http://localhost:3000",
|
||||
"http://localhost:3001")
|
||||
.AllowAnyMethod();
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user