GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs

50 lines
1.3 KiB
C#
Raw Normal View History

2022-10-08 15:04:29 -04:00
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
namespace GrossesMitainesAPI.Controllers;
[ApiController, Route("api/[controller]")]
public class ProductController : Controller {
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
public ProductController(ILogger<ProductController> logger, InventoryContext context) {
_logger = logger;
_context = context;
}
[HttpGet(Name = "Product")]
public 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 prod;
}
[HttpPost(Name = "Product")]
public void Post(Product prod) {
try {
_context.Products.Add(prod);
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-08 16:05:23 -04:00
[HttpPut(Name = "Product")]
public void Put(Product prod) {
try {
_context.Products.Update(prod);
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-08 15:04:29 -04:00
}