2022-10-18 11:50:06 -04:00
|
|
|
|
using GrossesMitainesAPI.Models;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-10-16 10:45:18 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-10-17 17:33:45 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-10-18 10:08:23 -04:00
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-10-18 11:50:06 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
using GrossesMitainesAPI.Services;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
|
|
|
|
|
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
|
|
2022-10-18 11:32:06 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-08 15:04:29 -04:00
|
|
|
|
[ApiController, Route("api/[controller]")]
|
2022-10-18 11:50:06 -04:00
|
|
|
|
public class ProductController : ControllerBase {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
private readonly ILogger<ProductController> _logger;
|
|
|
|
|
private readonly InventoryContext _context;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
private readonly DatabaseCacheService _cache;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
|
2022-10-21 17:52:25 -04:00
|
|
|
|
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_context = context;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_cache = cache;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-16 10:45:18 -04:00
|
|
|
|
[HttpGet(Name = "Product"), AllowAnonymous]
|
2022-10-18 11:55:49 -04:00
|
|
|
|
public ActionResult<Product> Get(int id) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
Product prod;
|
|
|
|
|
try {
|
|
|
|
|
prod = _context.Products.Where(x => x.Id == id).First();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return NotFound();
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
|
|
|
|
return prod;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-08 15:04:29 -04:00
|
|
|
|
[HttpPost(Name = "Product")]
|
2022-10-18 11:50:06 -04:00
|
|
|
|
public ActionResult<Product> Post(Product prod) {
|
2022-10-17 17:33:45 -04:00
|
|
|
|
|
2022-10-16 20:33:58 -04:00
|
|
|
|
if (prod.Price <= prod.PromoPrice)
|
|
|
|
|
prod.PromoPrice = prod.Price - 0.01M;
|
|
|
|
|
|
2022-10-08 15:04:29 -04:00
|
|
|
|
try {
|
|
|
|
|
_context.Products.Add(prod);
|
2022-10-16 11:25:21 -04:00
|
|
|
|
_context.SaveChanges();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
2022-10-18 11:50:06 -04:00
|
|
|
|
|
|
|
|
|
return prod;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
2022-10-08 16:05:23 -04:00
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-08 16:05:23 -04:00
|
|
|
|
[HttpPut(Name = "Product")]
|
2022-10-18 11:50:06 -04:00
|
|
|
|
public ActionResult<Product> Put(Product prod) {
|
|
|
|
|
try {
|
|
|
|
|
_context.Products.Update(prod);
|
|
|
|
|
_context.SaveChanges();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return prod;
|
2022-10-17 17:33:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-18 11:01:16 -04:00
|
|
|
|
[HttpDelete(Name = "Product")]
|
2022-10-18 11:55:49 -04:00
|
|
|
|
public ActionResult<int> DeleteProduct(int id) {
|
2022-10-18 11:01:16 -04:00
|
|
|
|
try {
|
|
|
|
|
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
|
|
|
|
|
_context.SaveChanges();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-18 11:01:16 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-18 11:01:16 -04:00
|
|
|
|
}
|
2022-10-18 11:50:06 -04:00
|
|
|
|
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return id;
|
2022-10-18 11:01:16 -04:00
|
|
|
|
}
|
2022-10-17 17:33:45 -04:00
|
|
|
|
}
|